From cf08a71890efe9e8c413d7bc5cfa5acbd07686d4 Mon Sep 17 00:00:00 2001 From: "jiakun.lin" Date: Fri, 4 Jun 2021 11:36:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8B=A6=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 9 +++ .../liuwanr/ProjectMaagementApplication.java | 1 + .../com/yipin/liuwanr/config/AuthConfig.java | 50 ++++++++++++++++ .../yipin/liuwanr/filter/AuthInterceptor.java | 58 +++++++++++++++++++ .../resources/application-keda.properties | 29 +++++++--- .../application-occupationlab.properties | 5 ++ 6 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/yipin/liuwanr/config/AuthConfig.java create mode 100644 src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java diff --git a/pom.xml b/pom.xml index 597bede..99994d3 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,15 @@ + + org.springframework.boot + spring-boot-starter-redis + 1.4.7.RELEASE + + + org.springframework.boot + spring-boot-starter-data-redis + io.springfox springfox-swagger-ui diff --git a/src/main/java/com/yipin/liuwanr/ProjectMaagementApplication.java b/src/main/java/com/yipin/liuwanr/ProjectMaagementApplication.java index c1113d6..87042bc 100644 --- a/src/main/java/com/yipin/liuwanr/ProjectMaagementApplication.java +++ b/src/main/java/com/yipin/liuwanr/ProjectMaagementApplication.java @@ -3,6 +3,7 @@ package com.yipin.liuwanr; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 diff --git a/src/main/java/com/yipin/liuwanr/config/AuthConfig.java b/src/main/java/com/yipin/liuwanr/config/AuthConfig.java new file mode 100644 index 0000000..970cd09 --- /dev/null +++ b/src/main/java/com/yipin/liuwanr/config/AuthConfig.java @@ -0,0 +1,50 @@ +package com.yipin.liuwanr.config; + +import com.yipin.liuwanr.filter.AuthInterceptor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.List; + +/** + * @description + * @author: Mr.JK + * @create: 2021-06-03 17:26 + **/ +@Configuration +@ConfigurationProperties(prefix = "auth") +public class AuthConfig implements WebMvcConfigurer { + + //开启拦截字符串 + public static String openInterceptStr; + + //放行url + public static List permitUrl; + + @Value("${auth.openInterceptStr}") + public void setOpenInterceptStr(String openInterceptStr) { + AuthConfig.openInterceptStr = openInterceptStr; + } + + @Value("${auth.permitUrl}") + public void setPermitUrl(List permitUrl) { + AuthConfig.permitUrl = permitUrl; + } + + @Bean + public AuthInterceptor initAuthInterceptor(){ + return new AuthInterceptor(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(initAuthInterceptor()).addPathPatterns(openInterceptStr) + .excludePathPatterns(permitUrl); + } + +} diff --git a/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java b/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java new file mode 100644 index 0000000..91e1952 --- /dev/null +++ b/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java @@ -0,0 +1,58 @@ +package com.yipin.liuwanr.filter; + +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Objects; + +/** + * @description + * @author: Mr.JK + * @create: 2021-06-03 17:29 + **/ +public class AuthInterceptor implements HandlerInterceptor { + + @Resource + StringRedisTemplate stringRedisTemplate; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + response.setCharacterEncoding("UTF-8"); + response.setContentType("text/html;charset=utf-8"); + String token = request.getHeader("token"); + if (StringUtils.isEmpty(token)) { + //用户未登录,请登录后操作! + response.getWriter().print("0"); + return false; + } + + ValueOperations ops = stringRedisTemplate.opsForValue(); + Object loginStatus = ops.get(token); + + + if( Objects.isNull(loginStatus)){ + //token错误 + response.getWriter().print("1"); + return false; + } + return true; + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + + } + +} diff --git a/src/main/resources/application-keda.properties b/src/main/resources/application-keda.properties index 51e03d5..012e340 100644 --- a/src/main/resources/application-keda.properties +++ b/src/main/resources/application-keda.properties @@ -3,16 +3,16 @@ server.port=1010 server.servlet.context-path=/project #\u79D1\u5927\u6570\u636E\u5E93\u8FDE\u63A5 -#spring.datasource.url=jdbc:mysql://rm-wz9y13wf7u8q8610fwo.mysql.rds.aliyuncs.com:3306/keda?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai -#spring.datasource.username=super -#spring.datasource.password=huoran888 -#spring.datasource.driver-class-name=com.mysql.jdbc.Driver +spring.datasource.url=jdbc:mysql://rm-wz9y13wf7u8q8610fwo.mysql.rds.aliyuncs.com:3306/keda?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai +spring.datasource.username=super +spring.datasource.password=huoran888 +spring.datasource.driver-class-name=com.mysql.jdbc.Driver #\u79D1\u5927\u6B63\u5F0F\u6570\u636E\u5E93\u8FDE\u63A5 -spring.datasource.url=jdbc:mysql://192.168.0.99:3306/keda?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai -spring.datasource.username=root -spring.datasource.password=mssdk@028 -spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +#spring.datasource.url=jdbc:mysql://192.168.0.99:3306/keda?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai +#spring.datasource.username=root +#spring.datasource.password=mssdk@028 +#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jackson.default-property-inclusion=non_null @@ -23,3 +23,16 @@ logging.level.jdbc.resultsettable=DEBUG logging.level.com.yipin.liuwar.mapper=DEBUG logging.level.com.yipin.liuwanr.mapper=DEBUG + +spring.redis.database=0 +spring.redis.host=127.0.0.1 +spring.redis.password=msdw@2021 +spring.redis.port=6379 +spring.redis.timeout=3000 + +#ȫ,Ϊղ +#auth.openInterceptStr= +auth.openInterceptStr=/** +#Url +auth.permitUrl= + diff --git a/src/main/resources/application-occupationlab.properties b/src/main/resources/application-occupationlab.properties index 5b993ec..e28e685 100644 --- a/src/main/resources/application-occupationlab.properties +++ b/src/main/resources/application-occupationlab.properties @@ -23,3 +23,8 @@ logging.level.jdbc.resultsettable=DEBUG logging.level.com.yipin.liuwar.mapper=DEBUG logging.level.com.yipin.liuwanr.mapper=DEBUG +#ȫ,Ϊղ +auth.openInterceptStr= +#auth.openInterceptStr=/** +#Url +auth.permitUrl= \ No newline at end of file