Explorar o código

修改token存入redis

xiewenjie %!s(int64=3) %!d(string=hai) anos
pai
achega
62dc84beb6

+ 10 - 10
sxz-application/src/main/resources/application-dev.properties

@@ -18,9 +18,9 @@ spring.servlet.multipart.enabled=true
 spring.mvc.async.request-timeout=600000
 spring.web.static-locations=classpath:/META-INF/resources/,classpath:/data/, classpath:/static/, file:${server.file.location}
 #数据库配置
-spring.datasource.url=jdbc:mysql://192.168.0.26:3306/change_clothes?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
-spring.datasource.username=test
-spring.datasource.password=123456
+spring.datasource.url=jdbc:mysql://192.168.0.47:3306/change_clothes_x?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
+spring.datasource.username=root
+spring.datasource.password=123123
 # Hikari will use the above plus the following to setup connection pooling
 spring.datasource.type=com.zaxxer.hikari.HikariDataSource
 spring.datasource.hikari.minimum-idle=5
@@ -35,6 +35,10 @@ spring.rabbitmq.host=127.0.0.1
 spring.rabbitmq.port=5672
 spring.rabbitmq.username=guest
 spring.rabbitmq.password=guest
+# 开启重试
+spring.rabbitmq.listener.simple.retry.enabled=true
+# 重试次数,默认为3次
+spring.rabbitmq.listener.simple.retry.max-attempts=5
 ##mybatis-plus配置 ##
 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
 mybatis-plus.configuration.map-underscore-to-camel-case=true
@@ -63,13 +67,9 @@ j2cache.open-spring-cache=true
 j2cache.j2CacheConfig.serialization=fastjson
 j2cache.cache-clean-mode=active
 j2cache.redis-client=lettuce
-j2cache.j2CacheConfig.hosts = 127.0.0.1:6379
-j2cache.j2CacheConfig.password = 1234
-j2cache.j2CacheConfig.database =4
-
-
-
-
+j2cache.j2CacheConfig.hosts=127.0.0.1:6379
+j2cache.j2CacheConfig.password=1234
+j2cache.j2CacheConfig.database=4
 oss.point=http://oss-cn-shenzhen.aliyuncs.com
 oss.key=LTAIUrvuHqj8pvry
 oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4

+ 7 - 4
sxz-application/src/main/resources/application-prod.properties

@@ -35,6 +35,10 @@ spring.rabbitmq.host=127.0.0.1
 spring.rabbitmq.port=5672
 spring.rabbitmq.username=guest
 spring.rabbitmq.password=guest
+# 开启重试
+spring.rabbitmq.listener.simple.retry.enabled=true
+# 重试次数,默认为3次
+spring.rabbitmq.listener.simple.retry.max-attempts=5
 ##mybatis-plus配置 ##
 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
 mybatis-plus.configuration.map-underscore-to-camel-case=true
@@ -63,10 +67,9 @@ j2cache.open-spring-cache=true
 j2cache.j2CacheConfig.serialization=fastjson
 j2cache.cache-clean-mode=active
 j2cache.redis-client=lettuce
-j2cache.j2CacheConfig.hosts = 127.0.0.1:6379
-j2cache.j2CacheConfig.password =
-j2cache.j2CacheConfig.database =15
-
+j2cache.j2CacheConfig.hosts=127.0.0.1:6379
+j2cache.j2CacheConfig.password=
+j2cache.j2CacheConfig.database=15
 oss.point=http://oss-cn-shenzhen.aliyuncs.com
 oss.key=LTAIUrvuHqj8pvry
 oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4

+ 7 - 5
sxz-application/src/main/resources/application-uat.properties

@@ -35,6 +35,10 @@ spring.rabbitmq.host=192.168.0.47
 spring.rabbitmq.port=5672
 spring.rabbitmq.username=guest
 spring.rabbitmq.password=guest
+# 开启重试
+spring.rabbitmq.listener.simple.retry.enabled=true
+# 重试次数,默认为3次
+spring.rabbitmq.listener.simple.retry.max-attempts=5
 ##mybatis-plus配置 ##
 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl
 mybatis-plus.configuration.map-underscore-to-camel-case=true
@@ -64,11 +68,9 @@ j2cache.open-spring-cache=true
 j2cache.j2CacheConfig.serialization=fastjson
 j2cache.cache-clean-mode=active
 j2cache.redis-client=lettuce
-j2cache.j2CacheConfig.hosts = 192.168.0.47:6379
-j2cache.j2CacheConfig.password =
-j2cache.j2CacheConfig.database =15
-
-
+j2cache.j2CacheConfig.hosts=192.168.0.47:6379
+j2cache.j2CacheConfig.password=
+j2cache.j2CacheConfig.database=15
 oss.point=http://oss-cn-shenzhen.aliyuncs.com
 oss.key=LTAIUrvuHqj8pvry
 oss.secrey=JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4

+ 23 - 11
sxz-core/src/main/java/com/fdkk/sxz/other/shiro/UserRealm.java

@@ -3,6 +3,7 @@ package com.fdkk.sxz.other.shiro;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.fdkk.sxz.config.SpringContextBean;
+import com.fdkk.sxz.constant.Constant;
 import com.fdkk.sxz.entity.system.Resource;
 import com.fdkk.sxz.entity.system.Role;
 import com.fdkk.sxz.entity.system.User;
@@ -11,6 +12,8 @@ import com.fdkk.sxz.other.shiro.conponent.JwtComponent;
 import com.fdkk.sxz.system.service.IRoleResourceService;
 import com.fdkk.sxz.system.service.IUserRoleService;
 import com.fdkk.sxz.system.service.IUserService;
+import com.fdkk.sxz.util.RedisUtil;
+import org.apache.shiro.authc.AuthenticationException;
 import org.apache.shiro.authc.AuthenticationInfo;
 import org.apache.shiro.authc.AuthenticationToken;
 import org.apache.shiro.authc.SimpleAuthenticationInfo;
@@ -19,18 +22,17 @@ import org.apache.shiro.authz.SimpleAuthorizationInfo;
 import org.apache.shiro.realm.AuthorizingRealm;
 import org.apache.shiro.subject.PrincipalCollection;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 /**
+ * @author Xiewj
  * @ClassName: MyRealm
  * @Description: TODO
- * @author Xiewj
  */
 public class UserRealm extends AuthorizingRealm {
 
@@ -45,6 +47,14 @@ public class UserRealm extends AuthorizingRealm {
 
     @Autowired
     private IRoleResourceService roleResourceService;
+    @Autowired
+    private RedisUtil redisUtils;
+    /**
+     * 过期时间
+     */
+    @Value("#{${system.expireTime}}")
+    private Map<String, Long> expireTime;
+
 
     @Override
     public boolean supports(AuthenticationToken token) {
@@ -79,8 +89,7 @@ public class UserRealm extends AuthorizingRealm {
                 }
             }
             List<Resource> resourceList = roleResourceService.queryResourceByRoleId(role.getId());
-            if(!CollectionUtils.isEmpty(resourceList))
-            {
+            if (!CollectionUtils.isEmpty(resourceList)) {
                 for (Resource res : resourceList) {
                     if (!StringUtils.isEmpty(res.getResourceKey())) {
                         if (!StringUtils.isEmpty(res.getResourceKey().replace(" ", ""))) {
@@ -109,15 +118,18 @@ public class UserRealm extends AuthorizingRealm {
         // 解密token获得username,用于和数据库进行对比
         String userAccount = jwtComponent.getUserAccount(token);
         if (userAccount == null) {
-            throw new UnauthorizedException("token invalid");
+            throw new UnauthorizedException("令牌无效");
         }
-        User userBean = userService.queryUserByAccount(userAccount);
+
+        User userBean = JSON.parseObject(redisUtils.get("userLoginToken:" + token), User.class);
         if (userBean == null) {
-            throw new UnauthorizedException("User didn't existed!");
+            throw new AuthenticationException("令牌已过期");
         }
         if (!jwtComponent.verify(token, userAccount, userBean.getUserPassword())) {
-            throw new UnauthorizedException("Username or password error");
+            throw new UnauthorizedException("用户名或密码错误");
         }
+        long expTime = expireTime.get(Constant.ExpTimeType.WEB);
+        redisUtils.expire("userLoginToken:" + token, expTime, TimeUnit.MILLISECONDS);
         String userString = JSONObject.toJSONString(userBean);
         return new SimpleAuthenticationInfo(userString, token, this.getName());
     }

+ 28 - 9
sxz-core/src/main/java/com/fdkk/sxz/system/controller/SysLoginController.java

@@ -1,22 +1,25 @@
 package com.fdkk.sxz.system.controller;
 
 import cn.hutool.captcha.GifCaptcha;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.fdkk.sxz.annotation.auth.CurrentUser;
 import com.fdkk.sxz.annotation.auth.NoAuthentication;
-import com.fdkk.sxz.system.dto.UserInfo;
-import com.fdkk.sxz.entity.system.Resource;
-import com.fdkk.sxz.entity.system.Role;
+import com.fdkk.sxz.base.BaseController;
+import com.fdkk.sxz.base.Result;
 import com.fdkk.sxz.constant.Constant;
 import com.fdkk.sxz.constant.ResponseConstant;
-import com.fdkk.sxz.base.BaseController;
-import com.fdkk.sxz.system.dto.LoginUser;
+import com.fdkk.sxz.entity.system.Resource;
+import com.fdkk.sxz.entity.system.Role;
 import com.fdkk.sxz.entity.system.User;
 import com.fdkk.sxz.other.shiro.conponent.JwtComponent;
+import com.fdkk.sxz.system.dto.LoginUser;
+import com.fdkk.sxz.system.dto.UserInfo;
 import com.fdkk.sxz.system.service.IResourceService;
 import com.fdkk.sxz.system.service.IUserRoleService;
 import com.fdkk.sxz.system.service.IUserService;
-import com.fdkk.sxz.base.Result;
+import com.fdkk.sxz.util.RedisUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiOperation;
@@ -36,11 +39,13 @@ import javax.servlet.http.HttpServletResponse;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 /**
+ * @author Xiewj
  * @ClassName: LoginController
  * @Description: 登录相关前端控制器
- * @author Xiewj
  * @date 2018年5月18日 下午4:08:26
  */
 @RestController
@@ -62,10 +67,17 @@ public class SysLoginController extends BaseController {
 
 //    @Autowired
 //    ISmsService iSmsService;
+    /**
+     * 过期时间
+     */
+    @Value("#{${system.expireTime}}")
+    private Map<String, Long> expireTime;
 
     @Value("${system.smsTimes}")
     private int smsTimes;
 
+    @Autowired
+    private RedisUtil redisUtil;
 
     @GetMapping("/vcode")
     @NoAuthentication
@@ -107,6 +119,8 @@ public class SysLoginController extends BaseController {
             return error(ResponseConstant.INVALID_USERNAME_PASSWORD);
         }
         String token = jwtComponent.sign(user.getUserAccount(), user.getUserPassword(), Constant.ExpTimeType.WEB);
+        long expTime = expireTime.get(Constant.ExpTimeType.WEB);
+        redisUtil.setEx("userLoginToken:" + token, JSON.toJSONString(user), expTime, TimeUnit.MILLISECONDS);
         return successPut(token);
     }
 
@@ -145,10 +159,12 @@ public class SysLoginController extends BaseController {
     @GetMapping("/token/refresh")
     @RequiresAuthentication
     @ApiOperation(value = "刷新token")
-    public Result<String> refreshToken( @ApiIgnore @CurrentUser User currentUser) {
+    public Result<String> refreshToken(@ApiIgnore @CurrentUser User currentUser) {
         Long userId = currentUser.getId();
         User user = userService.getById(userId);
         String token = jwtComponent.sign(user.getUserAccount(), user.getUserPassword(), Constant.ExpTimeType.WEB);
+        long expTime = expireTime.get(Constant.ExpTimeType.WEB);
+        redisUtil.setEx("userLoginToken:" + token, JSON.toJSONString(user), expTime, TimeUnit.MILLISECONDS);
         return successPut(token);
     }
 
@@ -440,7 +456,10 @@ public class SysLoginController extends BaseController {
     @PostMapping("/logout")
     @NoAuthentication
     @ApiOperation(value = "退出登录")
-    public Result<?> logOut(HttpServletRequest request) throws Exception {
+    public Result<?> logOut(@RequestBody JSONObject json) throws Exception {
+        if (json.containsKey("keys")) {
+            redisUtil.delete("userLoginToken:" + json.get("keys"));
+        }
         return success();
     }