diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/pom.xml b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/pom.xml index df0753354..d4c707b6c 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/pom.xml +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/pom.xml @@ -41,5 +41,19 @@ hsweb-authorization-oauth2-core ${project.version} + + org.springframework.boot + spring-boot-starter + provided + + + commons-codec + commons-codec + + + org.hswebframework + hsweb-expands-request + ${hsweb.expands.version} + \ No newline at end of file diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/AccessTokenInfo.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/AccessTokenInfo.java index 637220fff..912bbadbf 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/AccessTokenInfo.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/AccessTokenInfo.java @@ -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; + } } 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 new file mode 100644 index 000000000..6a4cc4d7d --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ClientAutoConfiguration.java @@ -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(); + } +} diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ServerConfig.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ServerConfig.java new file mode 100644 index 000000000..994e59630 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/OAuth2ServerConfig.java @@ -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; + } +} diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-starter/src/main/java/org/hswebframework/web/service/oauth2/client/starter/DefaultResponseJudge.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/DefaultResponseJudge.java similarity index 90% rename from hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-starter/src/main/java/org/hswebframework/web/service/oauth2/client/starter/DefaultResponseJudge.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/DefaultResponseJudge.java index 9fd9ed47a..897f560df 100644 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-starter/src/main/java/org/hswebframework/web/service/oauth2/client/starter/DefaultResponseJudge.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/DefaultResponseJudge.java @@ -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 */ diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ResponseConvertHandler.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ResponseConvertHandler.java similarity index 91% rename from hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ResponseConvertHandler.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ResponseConvertHandler.java index f591dcd6a..cf12b33e4 100644 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ResponseConvertHandler.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ResponseConvertHandler.java @@ -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 { diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ResponseJudge.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ResponseJudge.java similarity index 92% rename from hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ResponseJudge.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ResponseJudge.java index b08c9c958..e78d3a502 100644 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ResponseJudge.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ResponseJudge.java @@ -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; diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseConvertForProviderDefinition.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseConvertForProviderDefinition.java similarity index 76% rename from hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseConvertForProviderDefinition.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseConvertForProviderDefinition.java index 429ff574b..6ee19c1f1 100644 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseConvertForProviderDefinition.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseConvertForProviderDefinition.java @@ -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(); } diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseConvertForServerIdDefinition.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseConvertForServerIdDefinition.java similarity index 81% rename from hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseConvertForServerIdDefinition.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseConvertForServerIdDefinition.java index 75ade64dc..2849695ad 100644 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseConvertForServerIdDefinition.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseConvertForServerIdDefinition.java @@ -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 { diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseJudgeForProviderDefinition.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseJudgeForProviderDefinition.java similarity index 82% rename from hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseJudgeForProviderDefinition.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseJudgeForProviderDefinition.java index 9fb2d9d66..82fa8d4e2 100644 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseJudgeForProviderDefinition.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseJudgeForProviderDefinition.java @@ -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 { diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseJudgeForServerIdDefinition.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseJudgeForServerIdDefinition.java similarity index 81% rename from hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseJudgeForServerIdDefinition.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseJudgeForServerIdDefinition.java index 1600dcff4..12b64d53f 100644 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/definition/ResponseJudgeForServerIdDefinition.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/definition/ResponseJudgeForServerIdDefinition.java @@ -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 { 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 new file mode 100644 index 000000000..410905495 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/MemoryOAuth2ServerConfigRepository.java @@ -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 list = new HashMap<>(); + + @Override + public OAuth2ServerConfig findById(String id) { + return list.get(id); + } + + public void setList(Map list) { + this.list = list; + } + + public Map getList() { + return list; + } +} diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/MemoryOAuth2UserTokenRepository.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/MemoryOAuth2UserTokenRepository.java new file mode 100644 index 000000000..24e6a8032 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/MemoryOAuth2UserTokenRepository.java @@ -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 accessTokenInfoRepo = new ConcurrentHashMap<>(); + + @Override + public AccessTokenInfo createToken() { + AccessTokenInfo tokenInfo = new AccessTokenInfo(); + tokenInfo.setId(IDGenerator.MD5.generate()); + return tokenInfo; + } + + @Override + public List 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; + } +} diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/OAuth2ServerConfigRepository.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/OAuth2ServerConfigRepository.java new file mode 100644 index 000000000..65ffb7b2c --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/OAuth2ServerConfigRepository.java @@ -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); +} diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/OAuth2UserTokenRepository.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/OAuth2UserTokenRepository.java new file mode 100644 index 000000000..4f4d87ece --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/OAuth2UserTokenRepository.java @@ -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 findByServerIdAndGrantType(String serverId, String grantType); + + AccessTokenInfo findByAccessToken(String accessToken); + + AccessTokenInfo update(String id, AccessTokenInfo tokenInfo); + + AccessTokenInfo insert(AccessTokenInfo accessTokenInfo); +} 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/SimpleOAuth2RequestService.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2RequestService.java similarity index 67% rename from 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/SimpleOAuth2RequestService.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2RequestService.java index 401512338..7b0990680 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/SimpleOAuth2RequestService.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2RequestService.java @@ -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>> 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; - } } diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2SessionBuilder.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2SessionBuilder.java new file mode 100644 index 000000000..4fede4b9d --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2SessionBuilder.java @@ -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 list = oAuth2UserTokenRepository + .findByServerIdAndGrantType(serverConfig.getId(), GrantType.client_credentials); + return list.isEmpty() ? null : list.get(0); + } + + protected Consumer createOnTokenChanged(Supplier 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 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 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; + } + + +} diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2UserTokenService.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2UserTokenService.java new file mode 100644 index 000000000..15087865b --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/SimpleOAuth2UserTokenService.java @@ -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 + implements OAuth2UserTokenService { + @Autowired + private OAuth2UserTokenDao oAuth2UserTokenDao; + + @Override + protected IDGenerator getIDGenerator() { + return IDGenerator.MD5; + } + + @Override + public OAuth2UserTokenDao getDao() { + return oAuth2UserTokenDao; + } + + @Override + @Cacheable(cacheNames = "oauth2-user-token", key = "'s-g-t:'+#serverId+':'+#grantType") + public List 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(); + } +} 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/provider/HswebResponseConvertSupport.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/provider/HswebResponseConvertSupport.java similarity index 80% rename from 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/provider/HswebResponseConvertSupport.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/provider/HswebResponseConvertSupport.java index c120e896d..36806fdd7 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/provider/HswebResponseConvertSupport.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/provider/HswebResponseConvertSupport.java @@ -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"; } } 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/provider/HswebResponseJudgeSupport.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/provider/HswebResponseJudgeSupport.java similarity index 83% rename from 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/provider/HswebResponseJudgeSupport.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/provider/HswebResponseJudgeSupport.java index 4149be2ea..cb76f23cb 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/provider/HswebResponseJudgeSupport.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/provider/HswebResponseJudgeSupport.java @@ -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 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/request/SimpleOAuth2Request.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Request.java similarity index 94% rename from 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/request/SimpleOAuth2Request.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Request.java index 6c559a6fe..64dffd0b7 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/request/SimpleOAuth2Request.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Request.java @@ -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; 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/request/SimpleOAuth2Response.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Response.java similarity index 93% rename from 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/request/SimpleOAuth2Response.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Response.java index 2ed1411b0..d6b0830b3 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/request/SimpleOAuth2Response.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Response.java @@ -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; 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/request/UnCheck.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/UnCheck.java similarity index 92% rename from 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/request/UnCheck.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/UnCheck.java index e6ea60a8e..bb7617219 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/request/UnCheck.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/UnCheck.java @@ -16,7 +16,7 @@ * */ -package org.hswebframework.web.service.oauth2.client.simple.request; +package org.hswebframework.web.authorization.oauth2.client.simple.request; interface UnCheck { T call() throws Exception; 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/request/builder/SimpleOAuth2RequestBuilder.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/builder/SimpleOAuth2RequestBuilder.java similarity index 85% rename from 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/request/builder/SimpleOAuth2RequestBuilder.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/builder/SimpleOAuth2RequestBuilder.java index 34b058a06..4decd7e36 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/request/builder/SimpleOAuth2RequestBuilder.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/builder/SimpleOAuth2RequestBuilder.java @@ -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 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/request/builder/SimpleOAuth2RequestBuilderFactory.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/builder/SimpleOAuth2RequestBuilderFactory.java similarity index 87% rename from 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/request/builder/SimpleOAuth2RequestBuilderFactory.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/builder/SimpleOAuth2RequestBuilderFactory.java index e640c59fa..c435c8578 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/request/builder/SimpleOAuth2RequestBuilderFactory.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/builder/SimpleOAuth2RequestBuilderFactory.java @@ -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 judgeMap = new HashMap<>(); + private final Map convertHandlerMap = new HashMap<>(); ResponseConvertHandler defaultConvertHandler; 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/session/AuthorizationCodeSession.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/AuthorizationCodeSession.java similarity index 95% rename from 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/session/AuthorizationCodeSession.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/AuthorizationCodeSession.java index 9a7c9cf08..cd8490432 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/session/AuthorizationCodeSession.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/AuthorizationCodeSession.java @@ -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; 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/session/CachedOAuth2Session.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/CachedOAuth2Session.java similarity index 93% rename from 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/session/CachedOAuth2Session.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/CachedOAuth2Session.java index 76f901ce1..60cc6c3a9 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/session/CachedOAuth2Session.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/CachedOAuth2Session.java @@ -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; 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/session/DefaultOAuth2Session.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/DefaultOAuth2Session.java similarity index 78% rename from 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/session/DefaultOAuth2Session.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/DefaultOAuth2Session.java index 40e31a4c6..dbba12f84 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/session/DefaultOAuth2Session.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/DefaultOAuth2Session.java @@ -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) 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/session/PasswordSession.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/PasswordSession.java similarity index 90% rename from 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/session/PasswordSession.java rename to hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/PasswordSession.java index 3e80c0792..c148ffa61 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/session/PasswordSession.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/session/PasswordSession.java @@ -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))); } } diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/resources/META-INF/spring.factories b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..b9567546e --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +# Auto Configure +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.hswebframework.web.authorization.oauth2.client.OAuth2ClientAutoConfiguration \ No newline at end of file diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/OAuth2UserTokenService.java b/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/OAuth2UserTokenService.java index f2681f1dd..936101da3 100644 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/OAuth2UserTokenService.java +++ b/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/OAuth2UserTokenService.java @@ -27,7 +27,4 @@ import java.util.List; * @author hsweb-generator-online */ public interface OAuth2UserTokenService extends CrudService { - List selectByServerIdAndGrantType(String serverId, String grantType); - - OAuth2UserTokenEntity selectByAccessToken(String accessToken); } diff --git a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ProviderSupport.java b/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ProviderSupport.java deleted file mode 100644 index 0af3213f1..000000000 --- a/hsweb-system/hsweb-system-oauth2-client/hsweb-system-oauth2-client-service/hsweb-system-oauth2-client-service-api/src/main/java/org/hswebframework/web/service/oauth2/client/request/ProviderSupport.java +++ /dev/null @@ -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"; -} 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/SimpleOAuth2ServerConfigService.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/SimpleOAuth2ServerConfigService.java index 0693e118c..22b824514 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/SimpleOAuth2ServerConfigService.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/SimpleOAuth2ServerConfigService.java @@ -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 - implements OAuth2ServerConfigService { + implements OAuth2ServerConfigService, OAuth2ServerConfigRepository { @Autowired private OAuth2ServerConfigDao oAuth2ServerConfigDao; - @Override + + @Override protected IDGenerator getIDGenerator() { return IDGenerator.MD5; } @@ -44,4 +47,12 @@ public class SimpleOAuth2ServerConfigService extends GenericEntityService list = oAuth2UserTokenService - .selectByServerIdAndGrantType(configEntity.getId(), GrantType.client_credentials); - return list.isEmpty() ? null : list.get(0); - } - - protected Consumer createOnTokenChanged(Supplier 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 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 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; - } - - -} 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 dc23c305b..bc5b0806a 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 @@ -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 - implements OAuth2UserTokenService { + implements OAuth2UserTokenService, OAuth2UserTokenRepository { @Autowired private OAuth2UserTokenDao oAuth2UserTokenDao; @@ -49,8 +54,40 @@ public class SimpleOAuth2UserTokenService extends GenericEntityService 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 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 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