From 1d8fd46beecdfb2ddd88bbd41002e22c5f4ad66d Mon Sep 17 00:00:00 2001 From: "jiakun.lin" Date: Thu, 3 Jun 2021 17:24:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E5=8F=B0=E7=99=BB=E5=BD=95=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=8B=A6=E6=88=AA=E5=99=A8=EF=BC=8Ctoken=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../com/yipin/liuwanr/config/AuthConfig.java | 29 ++++++++++ .../com/yipin/liuwanr/entity/UserInfo.java | 3 + .../yipin/liuwanr/filter/AuthInterceptor.java | 58 +++++++++++++++++++ .../liuwanr/service/UserInfoService.java | 13 +++++ 5 files changed, 109 insertions(+) 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 1b0714e..e5da5f8 100644 --- a/pom.xml +++ b/pom.xml @@ -271,6 +271,12 @@ 2.3.28 compile + + org.springframework.boot + spring-boot-devtools + runtime + true + root 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..376825b --- /dev/null +++ b/src/main/java/com/yipin/liuwanr/config/AuthConfig.java @@ -0,0 +1,29 @@ +package com.yipin.liuwanr.config; + +/** + * @description + * @author: Mr.JK + * @create: 2021-06-03 16:24 + **/ + +import com.yipin.liuwanr.filter.AuthInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class AuthConfig implements WebMvcConfigurer { + + @Bean + public AuthInterceptor initAuthInterceptor(){ + return new AuthInterceptor(); + } + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(initAuthInterceptor()).addPathPatterns("/**") + .excludePathPatterns("/userInfo/adminLogins/**","/userInfo/loginSchoolClient/**","/userInfo/logins/**","/userInfo/updateLogInNumber/**"); + } + +} \ No newline at end of file diff --git a/src/main/java/com/yipin/liuwanr/entity/UserInfo.java b/src/main/java/com/yipin/liuwanr/entity/UserInfo.java index 6949a8c..b1c0922 100644 --- a/src/main/java/com/yipin/liuwanr/entity/UserInfo.java +++ b/src/main/java/com/yipin/liuwanr/entity/UserInfo.java @@ -101,4 +101,7 @@ public class UserInfo { private Integer organizationRelationshipId; //登陆时间 private String dataTime; + + //鉴权token + private String loginToken; } 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..414a255 --- /dev/null +++ b/src/main/java/com/yipin/liuwanr/filter/AuthInterceptor.java @@ -0,0 +1,58 @@ +package com.yipin.liuwanr.filter; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +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; +import java.util.concurrent.TimeUnit; + +/** + * @description + * @author: Mr.JK + * @create: 2021-06-03 16:19 + **/ +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)){ + response.getWriter().print("1");//token错误 + 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/java/com/yipin/liuwanr/service/UserInfoService.java b/src/main/java/com/yipin/liuwanr/service/UserInfoService.java index fa1c87f..b8409b6 100644 --- a/src/main/java/com/yipin/liuwanr/service/UserInfoService.java +++ b/src/main/java/com/yipin/liuwanr/service/UserInfoService.java @@ -437,6 +437,13 @@ public class UserInfoService { // List studentList = userInfoMapper.loginsGetStudentSchoolInfo(userId,schoolId); List studentList = userInfoMapper.loginsGetStudentSchoolInfo(userId,schoolId); obj.put("studentList",studentList); + + //鉴权,登录后将信息存入redis + ValueOperations redis = stringRedisTemplate.opsForValue(); + String loginToken = UUID.randomUUID().toString(); + redis.set(loginToken,user.getUserName(),2, TimeUnit.HOURS); + obj.put("loginToken",loginToken); + resp.put("retcode", 200); resp.put("retvalue", obj); }else { @@ -543,6 +550,12 @@ public class UserInfoService { ValueOperations redis = stringRedisTemplate.opsForValue(); redis.set(token,lastLoginTime, 24, TimeUnit.HOURS); user.setDataTime(lastLoginTime); + + //鉴权,登录后将信息存入redis + String loginToken = UUID.randomUUID().toString(); + redis.set(loginToken,user.getUserName(),2, TimeUnit.HOURS); + user.setLoginToken(loginToken); + resp.put("retvalue",user); resp.put("retcode", 200); }