优化OAuth2 client

This commit is contained in:
zhouhao
2017-10-24 20:39:24 +08:00
parent c4feb4ce69
commit 9de27498e5
3 changed files with 64 additions and 13 deletions

View File

@@ -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();
}

View File

@@ -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<String, OAuth2ServerConfig> list = new HashMap<>();
private Map<String, OAuth2ServerConfig> repo = new HashMap<>();
private List<OAuth2ServerConfig> servers;
@Override
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;
}
}

View File

@@ -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<OAuth2Use
}
@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) {
return selectByServerIdAndGrantType(serverId, grantType).stream()
.map(tokenInfoMapping())
@@ -74,18 +77,58 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2Use
}
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
@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<OAuth2UserTokenEntity> selectByServerIdAndGrantType(String serverId, String grantType) {