Ver código fonte

try fix OAuth2.0 token error bugs

zhou-hao 7 anos atrás
pai
commit
fb6fbb58ce

+ 15 - 11
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2SessionBuilder.java

@@ -68,9 +68,11 @@ public class SimpleOAuth2SessionBuilder implements OAuth2SessionBuilder {
 
 
     protected AccessTokenInfo getClientCredentialsToken() {
-        List<AccessTokenInfo> list = oAuth2UserTokenRepository
-                .findByServerIdAndGrantType(serverConfig.getId(), GrantType.client_credentials);
-        return list.isEmpty() ? null : list.get(0);
+        return oAuth2UserTokenRepository
+                .findByServerIdAndGrantType(serverConfig.getId(), GrantType.client_credentials)
+                .stream()
+                .findAny()
+                .orElse(null);
     }
 
     protected Consumer<AccessTokenInfo> createOnTokenChanged(Supplier<AccessTokenInfo> tokenGetter, String grantType) {
@@ -107,17 +109,19 @@ public class SimpleOAuth2SessionBuilder implements OAuth2SessionBuilder {
     }
 
 
+    Supplier<AccessTokenInfo> tokenGetter = () -> {
+        readWriteLock.readLock().lock();
+        try {
+            return getClientCredentialsToken();
+        } finally {
+            readWriteLock.readLock().unlock();
+        }
+    };
+
     @Override
     public OAuth2Session byClientCredentials() {
         DefaultOAuth2Session session;
-        Supplier<AccessTokenInfo> tokenGetter = () -> {
-            readWriteLock.readLock().lock();
-            try {
-                return getClientCredentialsToken();
-            } finally {
-                readWriteLock.readLock().unlock();
-            }
-        };
+
         AccessTokenInfo info = tokenGetter.get();
         if (null != info) {
             session = new CachedOAuth2Session(info);

+ 3 - 0
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/DefaultOAuth2Session.java

@@ -113,6 +113,9 @@ public class DefaultOAuth2Session implements OAuth2Session {
         if (accessTokenInfo == null) {
             authorize();
         }
+        if(accessTokenInfo.isExpire()){
+            refreshToken();
+        }
         OAuth2Request request = createRequest(getRealUrl(uriOrUrl));
         request.onTokenExpired(retry -> {
             refreshToken(); //刷新token

+ 4 - 0
hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-simple/src/main/java/org/hswebframework/web/service/oauth2/client/simple/SimpleOAuth2UserTokenService.java

@@ -29,6 +29,8 @@ import org.springframework.cache.annotation.CachePut;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
 
 import java.util.List;
@@ -107,6 +109,7 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2Use
             },
             evict = @CacheEvict(cacheNames = "oauth2-user-token-list", allEntries = true)
     )
+    @Transactional(propagation = Propagation.NOT_SUPPORTED)
     public AccessTokenInfo update(String id, AccessTokenInfo tokenInfo) {
         OAuth2UserTokenEntity entity = entityTokenInfoMapping().apply(tokenInfo);
         entity.setUpdateTime(System.currentTimeMillis());
@@ -121,6 +124,7 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2Use
             },
             evict = @CacheEvict(cacheNames = "oauth2-user-token-list", allEntries = true)
     )
+    @Transactional(propagation = Propagation.NOT_SUPPORTED)
     public AccessTokenInfo insert(AccessTokenInfo tokenInfo) {
         if (tokenInfo.getId() == null) {
             tokenInfo.setId(getIDGenerator().generate());