Browse Source

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

zhouwang 2 weeks ago
parent
commit
3b0f4b9a3e

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

@@ -65,7 +65,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
65
                 .secret(passwordEncoder.encode("lqkj007"))
65
                 .secret(passwordEncoder.encode("lqkj007"))
66
                 .authorizedGrantTypes("password", "refresh_token")
66
                 .authorizedGrantTypes("password", "refresh_token")
67
                 .scopes("all","read","write")
67
                 .scopes("all","read","write")
68
-                .accessTokenValiditySeconds(7200)
68
+                .accessTokenValiditySeconds(720000000)
69
                 .refreshTokenValiditySeconds(60 * 60 * 24 * 7);
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
 import org.springframework.security.core.Authentication;
19
 import org.springframework.security.core.Authentication;
20
 import org.springframework.security.crypto.password.PasswordEncoder;
20
 import org.springframework.security.crypto.password.PasswordEncoder;
21
 import org.springframework.security.oauth2.common.OAuth2AccessToken;
21
 import org.springframework.security.oauth2.common.OAuth2AccessToken;
22
+import org.springframework.security.oauth2.common.OAuth2RefreshToken;
22
 import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
23
 import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
23
 import org.springframework.security.oauth2.provider.*;
24
 import org.springframework.security.oauth2.provider.*;
24
 import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
25
 import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
26
+import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
25
 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
27
 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
26
 import org.springframework.stereotype.Component;
28
 import org.springframework.stereotype.Component;
27
 
29
 
30
+import javax.annotation.Resource;
28
 import javax.servlet.ServletException;
31
 import javax.servlet.ServletException;
29
 import javax.servlet.http.HttpServletRequest;
32
 import javax.servlet.http.HttpServletRequest;
30
 import javax.servlet.http.HttpServletResponse;
33
 import javax.servlet.http.HttpServletResponse;
@@ -54,6 +57,8 @@ public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthent
54
     private SysConfigService configService;
57
     private SysConfigService configService;
55
     @Autowired
58
     @Autowired
56
     private RedisCache redisCache;
59
     private RedisCache redisCache;
60
+    @Resource
61
+    private RedisTokenStore redisTokenStore;
57
 
62
 
58
     @Override
63
     @Override
59
     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
64
     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
@@ -84,6 +89,12 @@ public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthent
84
         TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, clientId, clientDetails.getScope(), "custom");
89
         TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, clientId, clientDetails.getScope(), "custom");
85
         OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
90
         OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
86
         OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
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
         OAuth2AccessToken token = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
98
         OAuth2AccessToken token = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);
88
 
99
 
89
         AsyncManager.me().execute(AsyncFactory.recordLogininfor(request, Constants.LOGIN_SUCCESS, "登录成功"));
100
         AsyncManager.me().execute(AsyncFactory.recordLogininfor(request, Constants.LOGIN_SUCCESS, "登录成功"));