优化OAuth2 client

This commit is contained in:
zhouhao
2017-10-23 18:42:39 +08:00
parent 64e56ad61e
commit 84d291d8b5
36 changed files with 696 additions and 344 deletions

View File

@@ -41,5 +41,19 @@
<artifactId>hsweb-authorization-oauth2-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.hswebframework</groupId>
<artifactId>hsweb-expands-request</artifactId>
<version>${hsweb.expands.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -25,6 +25,7 @@ import com.alibaba.fastjson.annotation.JSONField;
* @author zhouhao
*/
public class AccessTokenInfo {
private String id;
//授权码
@JSONField(name = "access_token")
private String accessToken;
@@ -44,6 +45,10 @@ public class AccessTokenInfo {
@JSONField(name = "token_type")
private String tokenType;
private String grantType;
private String serverId;
public boolean isExpire() {
return updateTime != null && System.currentTimeMillis() - updateTime > expiresIn * 1000;
}
@@ -127,4 +132,28 @@ public class AccessTokenInfo {
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setGrantType(String grantType) {
this.grantType = grantType;
}
public String getGrantType() {
return grantType;
}
public String getServerId() {
return serverId;
}
public void setServerId(String serverId) {
this.serverId = serverId;
}
}

View File

@@ -0,0 +1,51 @@
package org.hswebframework.web.authorization.oauth2.client;
import org.hswebframework.expands.request.RequestBuilder;
import org.hswebframework.expands.request.SimpleRequestBuilder;
import org.hswebframework.web.authorization.oauth2.client.request.DefaultResponseJudge;
import org.hswebframework.web.authorization.oauth2.client.simple.*;
import org.hswebframework.web.authorization.oauth2.client.simple.request.builder.SimpleOAuth2RequestBuilderFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* @author zhouhao
* @since 3.0
*/
public class OAuth2ClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean(RequestBuilder.class)
public RequestBuilder requestBuilder() {
return new SimpleRequestBuilder();
}
@Bean
@ConditionalOnMissingBean(OAuth2RequestBuilderFactory.class)
public SimpleOAuth2RequestBuilderFactory simpleOAuth2RequestBuilderFactory(RequestBuilder requestBuilder) {
SimpleOAuth2RequestBuilderFactory builderFactory = new SimpleOAuth2RequestBuilderFactory();
builderFactory.setRequestBuilder(requestBuilder);
builderFactory.setDefaultResponseJudge(new DefaultResponseJudge());
return builderFactory;
}
@ConditionalOnMissingBean(OAuth2RequestService.class)
@Bean
public SimpleOAuth2RequestService simpleOAuth2RequestService(OAuth2ServerConfigRepository configRepository, OAuth2UserTokenRepository userTokenRepository, OAuth2RequestBuilderFactory builderFactory) {
return new SimpleOAuth2RequestService(configRepository, userTokenRepository, builderFactory);
}
@ConditionalOnMissingBean(OAuth2ServerConfigRepository.class)
@Bean
@ConfigurationProperties(prefix = "hsweb.oauth2.server")
public MemoryOAuth2ServerConfigRepository memoryOAuth2ServerConfigRepository() {
return new MemoryOAuth2ServerConfigRepository();
}
@ConditionalOnMissingBean(OAuth2UserTokenRepository.class)
@Bean
public MemoryOAuth2UserTokenRepository memoryOAuth2UserTokenRepository() {
return new MemoryOAuth2UserTokenRepository();
}
}

View File

@@ -0,0 +1,150 @@
package org.hswebframework.web.authorization.oauth2.client;
/**
* @author zhouhao
* @since
*/
public class OAuth2ServerConfig {
private String id;
//服务名称
private String name;
//api根地址
private String apiBaseUrl;
//认证地址
private String authUrl;
//token获取地址
private String accessTokenUrl;
//客户端id
private String clientId;
//客户端密钥
private String clientSecret;
//是否启用
private Byte status;
//重定向地址
private String redirectUri;
//服务提供商
private String provider;
public String getProvider() {
return provider;
}
public void setProvider(String provider) {
this.provider = provider;
}
public String getRedirectUri() {
return redirectUri;
}
public void setRedirectUri(String redirectUri) {
this.redirectUri = redirectUri;
}
/**
* @return 服务名称
*/
public String getName() {
return this.name;
}
/**
* 设置 服务名称
*/
public void setName(String name) {
this.name = name;
}
/**
* @return api根地址
*/
public String getApiBaseUrl() {
return this.apiBaseUrl;
}
/**
* 设置 api根地址
*/
public void setApiBaseUrl(String apiBaseUrl) {
this.apiBaseUrl = apiBaseUrl;
}
/**
* @return 认证地址
*/
public String getAuthUrl() {
return this.authUrl;
}
/**
* 设置 认证地址
*/
public void setAuthUrl(String authUrl) {
this.authUrl = authUrl;
}
/**
* @return token获取地址
*/
public String getAccessTokenUrl() {
return this.accessTokenUrl;
}
/**
* 设置 token获取地址
*/
public void setAccessTokenUrl(String accessTokenUrl) {
this.accessTokenUrl = accessTokenUrl;
}
/**
* @return 客户端id
*/
public String getClientId() {
return this.clientId;
}
/**
* 设置 客户端id
*/
public void setClientId(String clientId) {
this.clientId = clientId;
}
/**
* @return 客户端密钥
*/
public String getClientSecret() {
return this.clientSecret;
}
/**
* 设置 客户端密钥
*/
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
/**
* @return 是否启用
*/
public Byte getStatus() {
return this.status;
}
/**
* 设置 是否启用
*/
public void setStatus(Byte status) {
this.status = status;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}

View File

@@ -16,18 +16,16 @@
*
*/
package org.hswebframework.web.service.oauth2.client.starter;
package org.hswebframework.web.authorization.oauth2.client.request;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import org.hswebframework.web.oauth2.core.ErrorType;
import org.hswebframework.web.service.oauth2.client.request.ResponseJudge;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* TODO 完成注释
*
* @author zhouhao
*/

View File

@@ -16,15 +16,13 @@
*
*/
package org.hswebframework.web.service.oauth2.client.request;
package org.hswebframework.web.authorization.oauth2.client.request;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import java.util.List;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public interface ResponseConvertHandler {

View File

@@ -16,7 +16,7 @@
*
*/
package org.hswebframework.web.service.oauth2.client.request;
package org.hswebframework.web.authorization.oauth2.client.request;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import org.hswebframework.web.oauth2.core.ErrorType;

View File

@@ -16,10 +16,9 @@
*
*/
package org.hswebframework.web.service.oauth2.client.request.definition;
package org.hswebframework.web.authorization.oauth2.client.request.definition;
import org.hswebframework.web.service.oauth2.client.request.ProviderSupport;
import org.hswebframework.web.service.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseConvertHandler;
/**
* TODO 完成注释
@@ -30,7 +29,6 @@ public interface ResponseConvertForProviderDefinition extends ResponseConvertHan
/**
* @return 支持的厂商标识
* @see ProviderSupport
*/
String getProvider();
}

View File

@@ -16,13 +16,12 @@
*
*/
package org.hswebframework.web.service.oauth2.client.request.definition;
package org.hswebframework.web.authorization.oauth2.client.request.definition;
import org.hswebframework.web.service.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseConvertHandler;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public interface ResponseConvertForServerIdDefinition extends ResponseConvertHandler {

View File

@@ -16,12 +16,12 @@
*
*/
package org.hswebframework.web.service.oauth2.client.request.definition;
package org.hswebframework.web.authorization.oauth2.client.request.definition;
import org.hswebframework.web.service.oauth2.client.request.ResponseJudge;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseJudge;
/**
*
* @author zhouhao
*/
public interface ResponseJudgeForProviderDefinition extends ResponseJudge {

View File

@@ -16,13 +16,12 @@
*
*/
package org.hswebframework.web.service.oauth2.client.request.definition;
package org.hswebframework.web.authorization.oauth2.client.request.definition;
import org.hswebframework.web.service.oauth2.client.request.ResponseJudge;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseJudge;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public interface ResponseJudgeForServerIdDefinition extends ResponseJudge {

View File

@@ -0,0 +1,27 @@
package org.hswebframework.web.authorization.oauth2.client.simple;
import org.hswebframework.web.authorization.oauth2.client.OAuth2ServerConfig;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhouhao
* @since 3.0
*/
public class MemoryOAuth2ServerConfigRepository implements OAuth2ServerConfigRepository {
private Map<String, OAuth2ServerConfig> list = new HashMap<>();
@Override
public OAuth2ServerConfig findById(String id) {
return list.get(id);
}
public void setList(Map<String, OAuth2ServerConfig> list) {
this.list = list;
}
public Map<String, OAuth2ServerConfig> getList() {
return list;
}
}

View File

@@ -0,0 +1,55 @@
package org.hswebframework.web.authorization.oauth2.client.simple;
import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
import org.hswebframework.web.authorization.oauth2.client.simple.OAuth2UserTokenRepository;
import org.hswebframework.web.id.IDGenerator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
* @author zhouhao
* @since
*/
public class MemoryOAuth2UserTokenRepository implements OAuth2UserTokenRepository {
private Map<String, AccessTokenInfo> accessTokenInfoRepo = new ConcurrentHashMap<>();
@Override
public AccessTokenInfo createToken() {
AccessTokenInfo tokenInfo = new AccessTokenInfo();
tokenInfo.setId(IDGenerator.MD5.generate());
return tokenInfo;
}
@Override
public List<AccessTokenInfo> findByServerIdAndGrantType(String serverId, String grantType) {
return accessTokenInfoRepo.values().stream().filter(token ->
token.getServerId().equals(serverId) && token.getGrantType().equals(grantType)
).collect(Collectors.toList());
}
@Override
public AccessTokenInfo findByAccessToken(String accessToken) {
return accessTokenInfoRepo.values().stream().filter(token ->
token.getAccessToken().equals(accessToken)
).findFirst().orElse(null);
}
@Override
public AccessTokenInfo update(String id, AccessTokenInfo tokenInfo) {
accessTokenInfoRepo.put(id, tokenInfo);
return tokenInfo;
}
@Override
public AccessTokenInfo insert(AccessTokenInfo accessTokenInfo) {
if (accessTokenInfo.getId() == null) {
accessTokenInfo.setId(IDGenerator.MD5.generate());
}
accessTokenInfoRepo.put(accessTokenInfo.getId(), accessTokenInfo);
return accessTokenInfo;
}
}

View File

@@ -0,0 +1,11 @@
package org.hswebframework.web.authorization.oauth2.client.simple;
import org.hswebframework.web.authorization.oauth2.client.OAuth2ServerConfig;
/**
* @author zhouhao
* @since 3.0
*/
public interface OAuth2ServerConfigRepository {
OAuth2ServerConfig findById(String id);
}

View File

@@ -0,0 +1,21 @@
package org.hswebframework.web.authorization.oauth2.client.simple;
import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
import java.util.List;
/**
* @author zhouhao
* @since
*/
public interface OAuth2UserTokenRepository {
AccessTokenInfo createToken();
List<AccessTokenInfo> findByServerIdAndGrantType(String serverId, String grantType);
AccessTokenInfo findByAccessToken(String accessToken);
AccessTokenInfo update(String id, AccessTokenInfo tokenInfo);
AccessTokenInfo insert(AccessTokenInfo accessTokenInfo);
}

View File

@@ -16,45 +16,42 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple;
package org.hswebframework.web.authorization.oauth2.client.simple;
import org.hswebframework.utils.ClassUtils;
import org.hswebframework.web.NotFoundException;
import org.hswebframework.web.authorization.listener.AuthorizationListener;
import org.hswebframework.web.authorization.listener.event.AuthorizationEvent;
import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestBuilderFactory;
import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestService;
import org.hswebframework.web.authorization.oauth2.client.OAuth2ServerConfig;
import org.hswebframework.web.authorization.oauth2.client.OAuth2SessionBuilder;
import org.hswebframework.web.authorization.oauth2.client.listener.OAuth2Event;
import org.hswebframework.web.authorization.oauth2.client.listener.OAuth2Listener;
import org.hswebframework.web.commons.entity.DataStatus;
import org.hswebframework.web.entity.oauth2.client.OAuth2ServerConfigEntity;
import org.hswebframework.web.service.oauth2.client.OAuth2ServerConfigService;
import org.hswebframework.web.service.oauth2.client.OAuth2UserTokenService;
import org.hswebframework.utils.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* TODO 完成注释
*
* @author zhouhao
*/
@Service("oAuth2RequestService")
public class SimpleOAuth2RequestService implements OAuth2RequestService {
private OAuth2ServerConfigService oAuth2ServerConfigService;
private OAuth2ServerConfigRepository oAuth2ServerConfigService;
private OAuth2UserTokenService oAuth2UserTokenService;
private OAuth2UserTokenRepository oAuth2UserTokenService;
private OAuth2RequestBuilderFactory oAuth2RequestBuilderFactory;
private Map<String, Map<Class, List<OAuth2Listener>>> listenerStore = new HashMap<>();
public SimpleOAuth2RequestService(OAuth2ServerConfigRepository oAuth2ServerConfigService, OAuth2UserTokenRepository oAuth2UserTokenService, OAuth2RequestBuilderFactory oAuth2RequestBuilderFactory) {
this.oAuth2ServerConfigService = oAuth2ServerConfigService;
this.oAuth2UserTokenService = oAuth2UserTokenService;
this.oAuth2RequestBuilderFactory = oAuth2RequestBuilderFactory;
}
@Override
public OAuth2SessionBuilder create(String serverId) {
OAuth2ServerConfigEntity configEntity = oAuth2ServerConfigService.selectByPk(serverId);
if (null == configEntity || !DataStatus.STATUS_ENABLED.equals(configEntity.getStatus())) {
OAuth2ServerConfig configEntity = oAuth2ServerConfigService.findById(serverId);
if (null == configEntity || !Byte.valueOf((byte) 1).equals(configEntity.getStatus())) {
throw new NotFoundException("server not found!");
}
return new SimpleOAuth2SessionBuilder(oAuth2UserTokenService, configEntity, oAuth2RequestBuilderFactory);
@@ -81,18 +78,4 @@ public class SimpleOAuth2RequestService implements OAuth2RequestService {
.forEach(listener -> listener.on(event));
}
@Autowired
public void setoAuth2ServerConfigService(OAuth2ServerConfigService oAuth2ServerConfigService) {
this.oAuth2ServerConfigService = oAuth2ServerConfigService;
}
@Autowired
public void setoAuth2UserTokenService(OAuth2UserTokenService oAuth2UserTokenService) {
this.oAuth2UserTokenService = oAuth2UserTokenService;
}
@Autowired
public void setoAuth2RequestBuilderFactory(OAuth2RequestBuilderFactory oAuth2RequestBuilderFactory) {
this.oAuth2RequestBuilderFactory = oAuth2RequestBuilderFactory;
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.authorization.oauth2.client.simple;
import org.hswebframework.web.NotFoundException;
import org.hswebframework.web.authorization.oauth2.client.*;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session;
import org.hswebframework.web.authorization.oauth2.client.simple.session.AuthorizationCodeSession;
import org.hswebframework.web.authorization.oauth2.client.simple.session.CachedOAuth2Session;
import org.hswebframework.web.authorization.oauth2.client.simple.session.DefaultOAuth2Session;
import org.hswebframework.web.authorization.oauth2.client.simple.session.PasswordSession;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* @author zhouhao
*/
public class SimpleOAuth2SessionBuilder implements OAuth2SessionBuilder {
private OAuth2UserTokenRepository oAuth2UserTokenRepository;
private OAuth2ServerConfig serverConfig;
private OAuth2RequestBuilderFactory requestBuilderFactory;
public SimpleOAuth2SessionBuilder(OAuth2UserTokenRepository oAuth2UserTokenRepository,
OAuth2ServerConfig oAuth2ServerConfig,
OAuth2RequestBuilderFactory requestBuilderFactory) {
this.oAuth2UserTokenRepository = oAuth2UserTokenRepository;
this.serverConfig = oAuth2ServerConfig;
this.requestBuilderFactory = requestBuilderFactory;
}
protected String getRealUrl(String url) {
if (url.startsWith("http")) {
return url;
}
if (!serverConfig.getApiBaseUrl().endsWith("/") && !url.startsWith("/")) {
return serverConfig.getApiBaseUrl().concat("/").concat(url);
}
return serverConfig.getApiBaseUrl() + url;
}
protected AccessTokenInfo getClientCredentialsToken() {
List<AccessTokenInfo> list = oAuth2UserTokenRepository
.findByServerIdAndGrantType(serverConfig.getId(), GrantType.client_credentials);
return list.isEmpty() ? null : list.get(0);
}
protected Consumer<AccessTokenInfo> createOnTokenChanged(Supplier<AccessTokenInfo> tokenGetter, String grantType) {
return token -> {
AccessTokenInfo tokenInfo = tokenGetter.get();
if (tokenInfo != null) {
tokenInfo.setUpdateTime(System.currentTimeMillis());
oAuth2UserTokenRepository.update(tokenInfo.getId(), tokenInfo);
} else {
tokenInfo = oAuth2UserTokenRepository.createToken();
tokenInfo.setGrantType(grantType);
tokenInfo.setCreateTime(System.currentTimeMillis());
tokenInfo.setServerId(serverConfig.getId());
oAuth2UserTokenRepository.insert(tokenInfo);
}
};
}
private final Consumer<AccessTokenInfo> onClientCredentialsTokenChanged = createOnTokenChanged(this::getClientCredentialsToken, GrantType.client_credentials);
@Override
public OAuth2Session byAuthorizationCode(String code) {
AuthorizationCodeSession authorizationCodeSession = new AuthorizationCodeSession();
authorizationCodeSession.setCode(code);
authorizationCodeSession.setRequestBuilderFactory(requestBuilderFactory);
authorizationCodeSession.setServerConfig(serverConfig);
authorizationCodeSession.init();
return authorizationCodeSession;
}
@Override
public OAuth2Session byClientCredentials() {
AccessTokenInfo tokenInfo = getClientCredentialsToken();
DefaultOAuth2Session session;
if (null != tokenInfo) {
session = new CachedOAuth2Session(tokenInfo);
} else {
session = new DefaultOAuth2Session();
}
session.setServerConfig(serverConfig);
session.setRequestBuilderFactory(requestBuilderFactory);
session.onTokenChanged(onClientCredentialsTokenChanged);
session.init();
session.param(OAuth2Constants.grant_type, GrantType.client_credentials);
return session;
}
@Override
public OAuth2Session byPassword(String username, String password) {
PasswordSession session = new PasswordSession(username, password);
session.setServerConfig(serverConfig);
session.setRequestBuilderFactory(requestBuilderFactory);
session.init();
return session;
}
@Override
public OAuth2Session byAccessToken(String accessToken) {
Supplier<AccessTokenInfo> supplier = () -> oAuth2UserTokenRepository.findByAccessToken(accessToken);
AccessTokenInfo tokenEntity = supplier.get();
if (tokenEntity == null) {
throw new NotFoundException("access_token not found");
}
AccessTokenInfo tokenInfo = new AccessTokenInfo();
CachedOAuth2Session session = new CachedOAuth2Session(tokenInfo);
session.setServerConfig(serverConfig);
session.setRequestBuilderFactory(requestBuilderFactory);
session.onTokenChanged(createOnTokenChanged(supplier, null));
session.init();
return session;
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.hswebframework.web.authorization.oauth2.client.simple;
import org.hswebframework.web.dao.oauth2.client.OAuth2UserTokenDao;
import org.hswebframework.web.entity.oauth2.client.OAuth2UserTokenEntity;
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.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.List;
/**
* 默认的服务实现
*
* @author hsweb-generator-online
*/
@Service("oAuth2UserTokenService")
public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2UserTokenEntity, String>
implements OAuth2UserTokenService {
@Autowired
private OAuth2UserTokenDao oAuth2UserTokenDao;
@Override
protected IDGenerator<String> getIDGenerator() {
return IDGenerator.MD5;
}
@Override
public OAuth2UserTokenDao getDao() {
return oAuth2UserTokenDao;
}
@Override
@Cacheable(cacheNames = "oauth2-user-token", key = "'s-g-t:'+#serverId+':'+#grantType")
public List<OAuth2UserTokenEntity> selectByServerIdAndGrantType(String serverId, String grantType) {
Assert.notNull(serverId, "serverId can not be null!");
Assert.notNull(grantType, "grantType can not be null!");
return createQuery()
.where(OAuth2UserTokenEntity.serverId, serverId)
.is(OAuth2UserTokenEntity.grantType, grantType)
.listNoPaging();
}
@Override
@Cacheable(cacheNames = "oauth2-user-token", key = "'a-t:'+#serverId+':'+#grantType")
public OAuth2UserTokenEntity selectByAccessToken(String accessToken) {
Assert.notNull(accessToken, "token can not be null!");
return createQuery().where(OAuth2UserTokenEntity.accessToken, accessToken)
.single();
}
}

View File

@@ -16,27 +16,19 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.provider;
package org.hswebframework.web.authorization.oauth2.client.simple.provider;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.builder.AuthenticationBuilderFactory;
import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
import org.hswebframework.web.authorization.oauth2.client.request.definition.ResponseConvertForProviderDefinition;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import org.hswebframework.web.service.oauth2.client.request.ProviderSupport;
import org.hswebframework.web.service.oauth2.client.request.definition.ResponseConvertForProviderDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* TODO 完成注释
*
* @author zhouhao
*/
@Component
@@ -70,6 +62,6 @@ public class HswebResponseConvertSupport implements ResponseConvertForProviderDe
@Override
public String getProvider() {
return ProviderSupport.hsweb;
return "hsweb";
}
}

View File

@@ -16,22 +16,16 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.provider;
package org.hswebframework.web.authorization.oauth2.client.simple.provider;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.hswebframework.web.authorization.oauth2.client.request.definition.ResponseJudgeForProviderDefinition;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import org.hswebframework.web.oauth2.core.ErrorType;
import org.hswebframework.web.service.oauth2.client.request.ProviderSupport;
import org.hswebframework.web.service.oauth2.client.request.definition.ResponseJudgeForProviderDefinition;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* TODO 完成注释
*
* @author zhouhao
*/
@Component
@@ -39,7 +33,7 @@ public class HswebResponseJudgeSupport implements ResponseJudgeForProviderDefini
@Override
public String getProvider() {
return ProviderSupport.hsweb;
return "hsweb";
}
@Override

View File

@@ -16,15 +16,15 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.request;
package org.hswebframework.web.authorization.oauth2.client.simple.request;
import org.hswebframework.expands.request.http.HttpRequest;
import org.hswebframework.expands.request.http.Response;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Request;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseJudge;
import org.hswebframework.web.authorization.oauth2.client.request.TokenExpiredCallBack;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import org.hswebframework.web.service.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.service.oauth2.client.request.ResponseJudge;
import java.util.function.Consumer;
import java.util.function.Supplier;

View File

@@ -16,14 +16,14 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.request;
package org.hswebframework.web.authorization.oauth2.client.simple.request;
import org.hswebframework.expands.request.http.Response;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseJudge;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import org.hswebframework.web.authorization.oauth2.client.response.ResponseConvert;
import org.hswebframework.web.oauth2.core.ErrorType;
import org.hswebframework.web.service.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.service.oauth2.client.request.ResponseJudge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@@ -16,14 +16,14 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.request.builder;
package org.hswebframework.web.authorization.oauth2.client.simple.request.builder;
import org.hswebframework.expands.request.RequestBuilder;
import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestBuilder;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Request;
import org.hswebframework.web.service.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.service.oauth2.client.request.ResponseJudge;
import org.hswebframework.web.service.oauth2.client.simple.request.SimpleOAuth2Request;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseJudge;
import org.hswebframework.web.authorization.oauth2.client.simple.request.SimpleOAuth2Request;
/**
* @author zhouhao

View File

@@ -16,17 +16,17 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.request.builder;
package org.hswebframework.web.authorization.oauth2.client.simple.request.builder;
import org.hswebframework.expands.request.RequestBuilder;
import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestBuilder;
import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestBuilderFactory;
import org.hswebframework.web.service.oauth2.client.request.definition.ResponseConvertForProviderDefinition;
import org.hswebframework.web.service.oauth2.client.request.definition.ResponseConvertForServerIdDefinition;
import org.hswebframework.web.service.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.service.oauth2.client.request.ResponseJudge;
import org.hswebframework.web.service.oauth2.client.request.definition.ResponseJudgeForProviderDefinition;
import org.hswebframework.web.service.oauth2.client.request.definition.ResponseJudgeForServerIdDefinition;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseConvertHandler;
import org.hswebframework.web.authorization.oauth2.client.request.ResponseJudge;
import org.hswebframework.web.authorization.oauth2.client.request.definition.ResponseConvertForProviderDefinition;
import org.hswebframework.web.authorization.oauth2.client.request.definition.ResponseConvertForServerIdDefinition;
import org.hswebframework.web.authorization.oauth2.client.request.definition.ResponseJudgeForProviderDefinition;
import org.hswebframework.web.authorization.oauth2.client.request.definition.ResponseJudgeForServerIdDefinition;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
@@ -39,6 +39,7 @@ import java.util.Map;
public class SimpleOAuth2RequestBuilderFactory implements OAuth2RequestBuilderFactory, BeanPostProcessor {
private final Map<String, ResponseJudge> judgeMap = new HashMap<>();
private final Map<String, ResponseConvertHandler> convertHandlerMap = new HashMap<>();
ResponseConvertHandler defaultConvertHandler;

View File

@@ -16,7 +16,7 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.session;
package org.hswebframework.web.authorization.oauth2.client.simple.session;
import org.hswebframework.web.authorization.oauth2.client.GrantType;
import org.hswebframework.web.authorization.oauth2.client.OAuth2Constants;

View File

@@ -16,7 +16,7 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.session;
package org.hswebframework.web.authorization.oauth2.client.simple.session;
import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session;

View File

@@ -16,17 +16,13 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.session;
package org.hswebframework.web.authorization.oauth2.client.simple.session;
import org.apache.commons.codec.binary.Base64;
import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
import org.hswebframework.web.authorization.oauth2.client.GrantType;
import org.hswebframework.web.authorization.oauth2.client.OAuth2Constants;
import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestBuilderFactory;
import org.hswebframework.web.authorization.oauth2.client.*;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Request;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import org.hswebframework.web.entity.oauth2.client.OAuth2ServerConfigEntity;
import org.springframework.util.Assert;
import java.util.function.Consumer;
@@ -40,7 +36,7 @@ public class DefaultOAuth2Session implements OAuth2Session {
protected OAuth2RequestBuilderFactory requestBuilderFactory;
protected OAuth2ServerConfigEntity configEntity;
protected OAuth2ServerConfig serverConfig;
protected boolean closed = false;
@@ -56,20 +52,20 @@ public class DefaultOAuth2Session implements OAuth2Session {
this.requestBuilderFactory = requestBuilderFactory;
}
public void setConfigEntity(OAuth2ServerConfigEntity configEntity) {
this.configEntity = configEntity;
public void setServerConfig(OAuth2ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}
public void init() {
Assert.notNull(requestBuilderFactory, "requestBuilderFactory can not be null!");
Assert.notNull(configEntity, "configEntity can not be null!");
accessTokenRequest = createRequest(configEntity.getAccessTokenUrl());
Assert.notNull(serverConfig, "configEntity can not be null!");
accessTokenRequest = createRequest(serverConfig.getAccessTokenUrl());
applyBasicAuthParam(accessTokenRequest);
}
protected OAuth2Request createRequest(String uriOrUrl) {
return requestBuilderFactory
.create(configEntity.getId(), configEntity.getProvider())
.create(serverConfig.getId(), serverConfig.getProvider())
.url(getRealUrl(uriOrUrl))
.build();
}
@@ -83,10 +79,10 @@ public class DefaultOAuth2Session implements OAuth2Session {
}
protected void applyBasicAuthParam(OAuth2Request request) {
request.param(client_id, configEntity.getClientId());
request.param(client_secret, configEntity.getClientSecret());
request.param(redirect_uri, configEntity.getRedirectUri());
request.header(authorization, encodeAuthorization(configEntity.getClientId().concat(":").concat(configEntity.getClientSecret())));
request.param(client_id, serverConfig.getClientId());
request.param(client_secret, serverConfig.getClientSecret());
request.param(redirect_uri, serverConfig.getRedirectUri());
request.header(authorization, encodeAuthorization(serverConfig.getClientId().concat(":").concat(serverConfig.getClientSecret())));
}
protected void applyTokenParam(OAuth2Request request) {
@@ -98,10 +94,10 @@ public class DefaultOAuth2Session implements OAuth2Session {
if (url.startsWith("http")) {
return url;
}
if (!configEntity.getApiBaseUrl().endsWith("/") && !url.startsWith("/")) {
return configEntity.getApiBaseUrl().concat("/").concat(url);
if (!serverConfig.getApiBaseUrl().endsWith("/") && !url.startsWith("/")) {
return serverConfig.getApiBaseUrl().concat("/").concat(url);
}
return configEntity.getApiBaseUrl() + url;
return serverConfig.getApiBaseUrl() + url;
}
@Override
@@ -140,7 +136,7 @@ public class DefaultOAuth2Session implements OAuth2Session {
if (accessTokenInfo == null) {
return;
}
OAuth2Request request = createRequest(getRealUrl(configEntity.getAccessTokenUrl()));
OAuth2Request request = createRequest(getRealUrl(serverConfig.getAccessTokenUrl()));
applyBasicAuthParam(request);
AccessTokenInfo tokenInfo = request
.param(OAuth2Constants.scope, scope)

View File

@@ -16,15 +16,13 @@
*
*/
package org.hswebframework.web.service.oauth2.client.simple.session;
package org.hswebframework.web.authorization.oauth2.client.simple.session;
import org.hswebframework.web.authorization.oauth2.client.GrantType;
import org.hswebframework.web.authorization.oauth2.client.OAuth2Constants;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Request;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public class PasswordSession extends DefaultOAuth2Session {
@@ -40,7 +38,7 @@ public class PasswordSession extends DefaultOAuth2Session {
protected void applyBasicAuthParam(OAuth2Request request) {
request.param(OAuth2Constants.grant_type, GrantType.password);
request.param("username", username);
request.param("password", configEntity.getClientSecret());
request.param("password", serverConfig.getClientSecret());
request.header(OAuth2Constants.authorization, encodeAuthorization(username.concat(":").concat(password)));
}
}

View File

@@ -0,0 +1,3 @@
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.hswebframework.web.authorization.oauth2.client.OAuth2ClientAutoConfiguration

View File

@@ -27,7 +27,4 @@ import java.util.List;
* @author hsweb-generator-online
*/
public interface OAuth2UserTokenService extends CrudService<OAuth2UserTokenEntity, String> {
List<OAuth2UserTokenEntity> selectByServerIdAndGrantType(String serverId, String grantType);
OAuth2UserTokenEntity selectByAccessToken(String accessToken);
}

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.service.oauth2.client.request;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public interface ProviderSupport {
String hsweb = "hsweb";
String tencent_qq = "QQ";
String sina = "sina";
}

View File

@@ -16,6 +16,8 @@
*/
package org.hswebframework.web.service.oauth2.client.simple;
import org.hswebframework.web.authorization.oauth2.client.OAuth2ServerConfig;
import org.hswebframework.web.authorization.oauth2.client.simple.OAuth2ServerConfigRepository;
import org.hswebframework.web.dao.oauth2.client.OAuth2ServerConfigDao;
import org.hswebframework.web.entity.oauth2.client.OAuth2ServerConfigEntity;
import org.hswebframework.web.id.IDGenerator;
@@ -31,10 +33,11 @@ import org.springframework.stereotype.Service;
*/
@Service("oAuth2ServerConfigService")
public class SimpleOAuth2ServerConfigService extends GenericEntityService<OAuth2ServerConfigEntity, String>
implements OAuth2ServerConfigService {
implements OAuth2ServerConfigService, OAuth2ServerConfigRepository {
@Autowired
private OAuth2ServerConfigDao oAuth2ServerConfigDao;
@Override
@Override
protected IDGenerator<String> getIDGenerator() {
return IDGenerator.MD5;
}
@@ -44,4 +47,12 @@ public class SimpleOAuth2ServerConfigService extends GenericEntityService<OAuth2
return oAuth2ServerConfigDao;
}
@Override
public OAuth2ServerConfig findById(String id) {
OAuth2ServerConfigEntity entity = selectByPk(id);
if (null == entity) {
return null;
}
return entityFactory.newInstance(OAuth2ServerConfig.class, entity);
}
}

View File

@@ -1,168 +0,0 @@
/*
* Copyright 2016 http://www.hswebframework.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.hswebframework.web.service.oauth2.client.simple;
import org.hswebframework.web.NotFoundException;
import org.hswebframework.web.authorization.oauth2.client.*;
import org.hswebframework.web.authorization.oauth2.client.request.OAuth2Session;
import org.hswebframework.web.entity.oauth2.client.OAuth2ServerConfigEntity;
import org.hswebframework.web.entity.oauth2.client.OAuth2UserTokenEntity;
import org.hswebframework.web.service.oauth2.client.OAuth2UserTokenService;
import org.hswebframework.web.service.oauth2.client.simple.session.AuthorizationCodeSession;
import org.hswebframework.web.service.oauth2.client.simple.session.CachedOAuth2Session;
import org.hswebframework.web.service.oauth2.client.simple.session.DefaultOAuth2Session;
import org.hswebframework.web.service.oauth2.client.simple.session.PasswordSession;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* @author zhouhao
*/
public class SimpleOAuth2SessionBuilder implements OAuth2SessionBuilder {
private OAuth2UserTokenService oAuth2UserTokenService;
private OAuth2ServerConfigEntity configEntity;
private OAuth2RequestBuilderFactory requestBuilderFactory;
public SimpleOAuth2SessionBuilder(OAuth2UserTokenService oAuth2UserTokenService,
OAuth2ServerConfigEntity oAuth2ServerConfig,
OAuth2RequestBuilderFactory requestBuilderFactory) {
this.oAuth2UserTokenService = oAuth2UserTokenService;
this.configEntity = oAuth2ServerConfig;
this.requestBuilderFactory = requestBuilderFactory;
}
protected String getRealUrl(String url) {
if (url.startsWith("http")) {
return url;
}
if (!configEntity.getApiBaseUrl().endsWith("/") && !url.startsWith("/")) {
return configEntity.getApiBaseUrl().concat("/").concat(url);
}
return configEntity.getApiBaseUrl() + url;
}
private void token2entity(AccessTokenInfo token, OAuth2UserTokenEntity entity) {
entity.setAccessToken(token.getAccessToken());
entity.setRefreshToken(token.getRefreshToken());
entity.setExpiresIn(token.getExpiresIn());
entity.setScope(token.getScope());
entity.setCreateTime(token.getCreateTime());
entity.setUpdateTime(token.getUpdateTime());
}
private void entity2token(OAuth2UserTokenEntity entity, AccessTokenInfo token) {
token.setAccessToken(entity.getAccessToken());
token.setRefreshToken(entity.getRefreshToken());
token.setExpiresIn(entity.getExpiresIn());
token.setScope(entity.getScope());
token.setCreateTime(entity.getCreateTime());
token.setUpdateTime(entity.getUpdateTime());
}
protected OAuth2UserTokenEntity getClientCredentialsToken() {
List<OAuth2UserTokenEntity> list = oAuth2UserTokenService
.selectByServerIdAndGrantType(configEntity.getId(), GrantType.client_credentials);
return list.isEmpty() ? null : list.get(0);
}
protected Consumer<AccessTokenInfo> createOnTokenChanged(Supplier<OAuth2UserTokenEntity> tokenGetter, String grantType) {
return token -> {
OAuth2UserTokenEntity tokenEntity = tokenGetter.get();
if (tokenEntity != null) {
tokenEntity.setUpdateTime(System.currentTimeMillis());
token2entity(token, tokenEntity);
oAuth2UserTokenService.updateByPk(tokenEntity.getId(), tokenEntity);
} else {
tokenEntity = oAuth2UserTokenService.createEntity();
tokenEntity.setGrantType(grantType);
tokenEntity.setCreateTime(System.currentTimeMillis());
tokenEntity.setServerId(configEntity.getId());
token2entity(token, tokenEntity);
oAuth2UserTokenService.insert(tokenEntity);
}
};
}
private final Consumer<AccessTokenInfo> onClientCredentialsTokenChanged = createOnTokenChanged(this::getClientCredentialsToken, GrantType.client_credentials);
@Override
public OAuth2Session byAuthorizationCode(String code) {
AuthorizationCodeSession authorizationCodeSession = new AuthorizationCodeSession();
authorizationCodeSession.setCode(code);
authorizationCodeSession.setRequestBuilderFactory(requestBuilderFactory);
authorizationCodeSession.setConfigEntity(configEntity);
authorizationCodeSession.init();
return authorizationCodeSession;
}
@Override
public OAuth2Session byClientCredentials() {
OAuth2UserTokenEntity entity = getClientCredentialsToken();
DefaultOAuth2Session session;
if (null != entity) {
AccessTokenInfo tokenInfo = new AccessTokenInfo();
entity2token(entity, tokenInfo);
session = new CachedOAuth2Session(tokenInfo);
} else {
session = new DefaultOAuth2Session();
}
session.setConfigEntity(configEntity);
session.setRequestBuilderFactory(requestBuilderFactory);
session.onTokenChanged(onClientCredentialsTokenChanged);
session.init();
session.param(OAuth2Constants.grant_type, GrantType.client_credentials);
return session;
}
@Override
public OAuth2Session byPassword(String username, String password) {
PasswordSession session = new PasswordSession(username, password);
session.setConfigEntity(configEntity);
session.setRequestBuilderFactory(requestBuilderFactory);
session.init();
return session;
}
@Override
public OAuth2Session byAccessToken(String accessToken) {
Supplier<OAuth2UserTokenEntity> supplier = () -> oAuth2UserTokenService.selectByAccessToken(accessToken);
OAuth2UserTokenEntity tokenEntity = supplier.get();
if (tokenEntity == null) {
throw new NotFoundException("access_token not found");
}
AccessTokenInfo tokenInfo = new AccessTokenInfo();
entity2token(tokenEntity, tokenInfo);
CachedOAuth2Session session = new CachedOAuth2Session(tokenInfo);
session.setConfigEntity(configEntity);
session.setRequestBuilderFactory(requestBuilderFactory);
session.onTokenChanged(createOnTokenChanged(supplier, null));
session.init();
return session;
}
}

View File

@@ -16,6 +16,8 @@
*/
package org.hswebframework.web.service.oauth2.client.simple;
import org.hswebframework.web.authorization.oauth2.client.AccessTokenInfo;
import org.hswebframework.web.authorization.oauth2.client.simple.OAuth2UserTokenRepository;
import org.hswebframework.web.dao.oauth2.client.OAuth2UserTokenDao;
import org.hswebframework.web.entity.oauth2.client.OAuth2UserTokenEntity;
import org.hswebframework.web.id.IDGenerator;
@@ -27,6 +29,9 @@ import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 默认的服务实现
@@ -35,7 +40,7 @@ import java.util.List;
*/
@Service("oAuth2UserTokenService")
public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2UserTokenEntity, String>
implements OAuth2UserTokenService {
implements OAuth2UserTokenService, OAuth2UserTokenRepository {
@Autowired
private OAuth2UserTokenDao oAuth2UserTokenDao;
@@ -49,8 +54,40 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2Use
return oAuth2UserTokenDao;
}
@Override
public AccessTokenInfo createToken() {
return entityFactory.newInstance(AccessTokenInfo.class);
}
@Override
@Cacheable(cacheNames = "oauth2-user-token", key = "'s-g-t:'+#serverId+':'+#grantType")
public List<AccessTokenInfo> findByServerIdAndGrantType(String serverId, String grantType) {
return selectByServerIdAndGrantType(serverId, grantType).stream()
.map(tokenInfoMapping())
.collect(Collectors.toList());
}
@Override
@Cacheable(cacheNames = "oauth2-user-token", key = "'a-t:'+#accessToken")
public AccessTokenInfo findByAccessToken(String accessToken) {
return Optional.ofNullable(selectByAccessToken(accessToken)).map(tokenInfoMapping()).orElse(null);
}
protected Function<OAuth2UserTokenEntity, AccessTokenInfo> tokenInfoMapping() {
return entity ->
entityFactory.newInstance(AccessTokenInfo.class, entity);
}
@Override
public AccessTokenInfo update(String id, AccessTokenInfo tokenInfo) {
return null;
}
@Override
public AccessTokenInfo insert(AccessTokenInfo accessTokenInfo) {
return null;
}
public List<OAuth2UserTokenEntity> selectByServerIdAndGrantType(String serverId, String grantType) {
Assert.notNull(serverId, "serverId can not be null!");
Assert.notNull(grantType, "grantType can not be null!");
@@ -60,8 +97,6 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService<OAuth2Use
.listNoPaging();
}
@Override
@Cacheable(cacheNames = "oauth2-user-token", key = "'a-t:'+#serverId+':'+#grantType")
public OAuth2UserTokenEntity selectByAccessToken(String accessToken) {
Assert.notNull(accessToken, "token can not be null!");
return createQuery().where(OAuth2UserTokenEntity.accessToken, accessToken)

View File

@@ -18,16 +18,9 @@
package org.hswebframework.web.service.oauth2.client.starter;
import org.hswebframework.expands.request.RequestBuilder;
import org.hswebframework.expands.request.SimpleRequestBuilder;
import org.hswebframework.web.authorization.oauth2.client.OAuth2RequestBuilderFactory;
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
import org.hswebframework.web.service.oauth2.client.simple.request.builder.SimpleOAuth2RequestBuilderFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
/**
@@ -39,17 +32,4 @@ import org.springframework.context.annotation.Configuration;
, "org.hswebframework.web.authorization.oauth2.controller"})
public class OAuth2ClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean(RequestBuilder.class)
public RequestBuilder requestBuilder() {
return new SimpleRequestBuilder();
}
@Bean
public SimpleOAuth2RequestBuilderFactory simpleOAuth2RequestBuilderFactory(RequestBuilder requestBuilder) {
SimpleOAuth2RequestBuilderFactory builderFactory = new SimpleOAuth2RequestBuilderFactory();
builderFactory.setRequestBuilder(requestBuilder);
builderFactory.setDefaultResponseJudge(new DefaultResponseJudge());
return builderFactory;
}
}