diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ClientAutoConfiguration.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ClientAutoConfiguration.java index 03598b9f7..dace9df4a 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ClientAutoConfiguration.java +++ b/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) @Bean - @ConfigurationProperties(prefix = "hsweb.oauth2.server") + @ConfigurationProperties(prefix = "hsweb.oauth2") public MemoryOAuth2ServerConfigRepository memoryOAuth2ServerConfigRepository() { return new MemoryOAuth2ServerConfigRepository(); } diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/MemoryOAuth2ServerConfigRepository.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/MemoryOAuth2ServerConfigRepository.java index 410905495..b9395f7bb 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/MemoryOAuth2ServerConfigRepository.java +++ b/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 java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; /** * @author zhouhao * @since 3.0 */ public class MemoryOAuth2ServerConfigRepository implements OAuth2ServerConfigRepository { - private Map list = new HashMap<>(); + + private Map repo = new HashMap<>(); + + private List servers; @Override public OAuth2ServerConfig findById(String id) { - return list.get(id); + return repo.get(id); } - public void setList(Map list) { - this.list = list; + public void setServers(List servers) { + this.servers = servers; + repo = servers.stream() + .collect(Collectors.toMap(OAuth2ServerConfig::getId, Function.identity())); } - public Map getList() { - return list; + public List getServers() { + return servers; } } diff --git a/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 b/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 index bc5b0806a..2ebaa97a7 100644 --- a/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 +++ b/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.oauth2.client.OAuth2UserTokenService; 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.Caching; import org.springframework.stereotype.Service; import org.springframework.util.Assert; @@ -60,7 +63,7 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService findByServerIdAndGrantType(String serverId, String grantType) { return selectByServerIdAndGrantType(serverId, grantType).stream() .map(tokenInfoMapping()) @@ -74,18 +77,58 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService 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 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 + @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) { - return null; + OAuth2UserTokenEntity entity = entityTokenInfoMapping().apply(tokenInfo); + entity.setUpdateTime(System.currentTimeMillis()); + updateByPk(id, entity); + return tokenInfo; } @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 selectByServerIdAndGrantType(String serverId, String grantType) {