Browse Source

优化OAuth2 client

zhouhao 7 years ago
parent
commit
9de27498e5

+ 1 - 1
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ClientAutoConfiguration.java

@@ -43,7 +43,7 @@ public class OAuth2ClientAutoConfiguration {
 
 
     @ConditionalOnMissingBean(OAuth2ServerConfigRepository.class)
     @ConditionalOnMissingBean(OAuth2ServerConfigRepository.class)
     @Bean
     @Bean
-    @ConfigurationProperties(prefix = "hsweb.oauth2.server")
+    @ConfigurationProperties(prefix = "hsweb.oauth2")
     public MemoryOAuth2ServerConfigRepository memoryOAuth2ServerConfigRepository() {
     public MemoryOAuth2ServerConfigRepository memoryOAuth2ServerConfigRepository() {
         return new MemoryOAuth2ServerConfigRepository();
         return new MemoryOAuth2ServerConfigRepository();
     }
     }

+ 14 - 6
hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/MemoryOAuth2ServerConfigRepository.java

@@ -3,25 +3,33 @@ package org.hswebframework.web.authorization.oauth2.client.simple;
 import org.hswebframework.web.authorization.oauth2.client.OAuth2ServerConfig;
 import org.hswebframework.web.authorization.oauth2.client.OAuth2ServerConfig;
 
 
 import java.util.HashMap;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 
 /**
 /**
  * @author zhouhao
  * @author zhouhao
  * @since 3.0
  * @since 3.0
  */
  */
 public class MemoryOAuth2ServerConfigRepository implements OAuth2ServerConfigRepository {
 public class MemoryOAuth2ServerConfigRepository implements OAuth2ServerConfigRepository {
-    private Map<String, OAuth2ServerConfig> list = new HashMap<>();
+
+    private Map<String, OAuth2ServerConfig> repo = new HashMap<>();
+
+    private List<OAuth2ServerConfig> servers;
 
 
     @Override
     @Override
     public OAuth2ServerConfig findById(String id) {
     public OAuth2ServerConfig findById(String id) {
-        return list.get(id);
+        return repo.get(id);
     }
     }
 
 
-    public void setList(Map<String, OAuth2ServerConfig> list) {
-        this.list = list;
+    public void setServers(List<OAuth2ServerConfig> servers) {
+        this.servers = servers;
+        repo = servers.stream()
+                .collect(Collectors.toMap(OAuth2ServerConfig::getId, Function.identity()));
     }
     }
 
 
-    public Map<String, OAuth2ServerConfig> getList() {
-        return list;
+    public List<OAuth2ServerConfig> getServers() {
+        return servers;
     }
     }
 }
 }

+ 49 - 6
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

@@ -24,7 +24,10 @@ import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.service.oauth2.client.OAuth2UserTokenService;
 import org.hswebframework.web.service.oauth2.client.OAuth2UserTokenService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.CachePut;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 import org.springframework.util.Assert;
 
 
@@ -60,7 +63,7 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2Use
     }
     }
 
 
     @Override
     @Override
-    @Cacheable(cacheNames = "oauth2-user-token", key = "'s-g-t:'+#serverId+':'+#grantType")
+    @Cacheable(cacheNames = "oauth2-user-token-list", key = "'s-g-t:'+#serverId+':'+#grantType")
     public List<AccessTokenInfo> findByServerIdAndGrantType(String serverId, String grantType) {
     public List<AccessTokenInfo> findByServerIdAndGrantType(String serverId, String grantType) {
         return selectByServerIdAndGrantType(serverId, grantType).stream()
         return selectByServerIdAndGrantType(serverId, grantType).stream()
                 .map(tokenInfoMapping())
                 .map(tokenInfoMapping())
@@ -74,18 +77,58 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2Use
     }
     }
 
 
     protected Function<OAuth2UserTokenEntity, AccessTokenInfo> tokenInfoMapping() {
     protected Function<OAuth2UserTokenEntity, AccessTokenInfo> tokenInfoMapping() {
-        return entity ->
-                entityFactory.newInstance(AccessTokenInfo.class, entity);
+        return entity -> {
+
+            AccessTokenInfo info = entityFactory.newInstance(AccessTokenInfo.class, entity);
+            info.setExpiresIn(entity.getExpiresIn());
+            info.setAccessToken(entity.getAccessToken());
+            info.setRefreshToken(entity.getRefreshToken());
+            return info;
+        };
+    }
+
+    protected Function<AccessTokenInfo, OAuth2UserTokenEntity> entityTokenInfoMapping() {
+        return info ->
+        {
+            OAuth2UserTokenEntity entity = entityFactory.newInstance(OAuth2UserTokenEntity.class, info);
+            entity.setExpiresIn(info.getExpiresIn());
+            entity.setAccessToken(info.getAccessToken());
+            entity.setRefreshToken(info.getRefreshToken());
+            return entity;
+        };
     }
     }
 
 
     @Override
     @Override
+    @Caching(
+            put = {
+                    @CachePut(cacheNames = "oauth2-user-token", key = "'a-t:'+#tokenInfo.accessToken"),
+            },
+            evict = @CacheEvict(cacheNames = "oauth2-user-token-list", allEntries = true)
+    )
     public AccessTokenInfo update(String id, AccessTokenInfo tokenInfo) {
     public AccessTokenInfo update(String id, AccessTokenInfo tokenInfo) {
-        return null;
+        OAuth2UserTokenEntity entity = entityTokenInfoMapping().apply(tokenInfo);
+        entity.setUpdateTime(System.currentTimeMillis());
+        updateByPk(id, entity);
+        return tokenInfo;
     }
     }
 
 
     @Override
     @Override
-    public AccessTokenInfo insert(AccessTokenInfo accessTokenInfo) {
-        return null;
+    @Caching(
+            put = {
+                    @CachePut(cacheNames = "oauth2-user-token", key = "'a-t:'+#tokenInfo.accessToken"),
+            },
+            evict = @CacheEvict(cacheNames = "oauth2-user-token-list", allEntries = true)
+    )
+    public AccessTokenInfo insert(AccessTokenInfo tokenInfo) {
+        if (tokenInfo.getId() == null) {
+            tokenInfo.setId(getIDGenerator().generate());
+        }
+        OAuth2UserTokenEntity entity = entityTokenInfoMapping().apply(tokenInfo);
+
+        entity.setUpdateTime(System.currentTimeMillis());
+
+        insert(entity);
+        return tokenInfo;
     }
     }
 
 
     public List<OAuth2UserTokenEntity> selectByServerIdAndGrantType(String serverId, String grantType) {
     public List<OAuth2UserTokenEntity> selectByServerIdAndGrantType(String serverId, String grantType) {