Просмотр исходного кода

每次登录token重置 过期时间设置为720000000

zhouwang недель назад: 2
Родитель
Сommit
3b0f4b9a3e

+ 1 - 1
src/main/java/com/lqkj/framework/security/AuthorizationServerConfig.java

@@ -65,7 +65,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
65 65
                 .secret(passwordEncoder.encode("lqkj007"))
66 66
                 .authorizedGrantTypes("password", "refresh_token")
67 67
                 .scopes("all","read","write")
68
-                .accessTokenValiditySeconds(7200)
68
+                .accessTokenValiditySeconds(720000000)
69 69
                 .refreshTokenValiditySeconds(60 * 60 * 24 * 7);
70 70
     }
71 71
 }

+ 11 - 0
src/main/java/com/lqkj/framework/security/handler/CustomAuthenticationSuccessHandler.java

@@ -19,12 +19,15 @@ import org.springframework.security.authentication.BadCredentialsException;
19 19
 import org.springframework.security.core.Authentication;
20 20
 import org.springframework.security.crypto.password.PasswordEncoder;
21 21
 import org.springframework.security.oauth2.common.OAuth2AccessToken;
22
+import org.springframework.security.oauth2.common.OAuth2RefreshToken;
22 23
 import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
23 24
 import org.springframework.security.oauth2.provider.*;
24 25
 import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
26
+import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
25 27
 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
26 28
 import org.springframework.stereotype.Component;
27 29
 
30
+import javax.annotation.Resource;
28 31
 import javax.servlet.ServletException;
29 32
 import javax.servlet.http.HttpServletRequest;
30 33
 import javax.servlet.http.HttpServletResponse;
@@ -54,6 +57,8 @@ public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthent
54 57
     private SysConfigService configService;
55 58
     @Autowired
56 59
     private RedisCache redisCache;
60
+    @Resource
61
+    private RedisTokenStore redisTokenStore;
57 62
 
58 63
     @Override
59 64
     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
@@ -84,6 +89,12 @@ public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthent
84 89
         TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, clientId, clientDetails.getScope(), "custom");
85 90
         OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
86 91
         OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
92
+        OAuth2AccessToken oldToken =  authorizationServerTokenServices.getAccessToken(oAuth2Authentication);
93
+        if (oldToken != null){
94
+            OAuth2RefreshToken auth2RefreshToken = oldToken.getRefreshToken();
95
+            redisTokenStore.removeAccessToken(oldToken);
96
+            redisTokenStore.removeRefreshToken(auth2RefreshToken);
97
+        }
87 98
         OAuth2AccessToken token = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
88 99
 
89 100
         AsyncManager.me().execute(AsyncFactory.recordLogininfor(request, Constants.LOGIN_SUCCESS, "登录成功"));