新增OAuth2客户端模块
This commit is contained in:
parent
076ec2fba4
commit
20f821798d
@ -0,0 +1,36 @@
|
|||||||
|
package ink.wgink.properties;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName: ApiUrlProperties
|
||||||
|
* @Description: api
|
||||||
|
* @Author: WangGeng
|
||||||
|
* @Date: 2019/4/22 9:42 PM
|
||||||
|
* @Version: 1.0
|
||||||
|
**/
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "api-path")
|
||||||
|
public class ApiPathProperties {
|
||||||
|
|
||||||
|
private String userCenter;
|
||||||
|
|
||||||
|
public String getUserCenter() {
|
||||||
|
return userCenter == null ? "" : userCenter.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserCenter(String userCenter) {
|
||||||
|
this.userCenter = userCenter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final StringBuilder sb = new StringBuilder("{");
|
||||||
|
sb.append("\"userCenter\":")
|
||||||
|
.append("\"").append(userCenter).append("\"");
|
||||||
|
|
||||||
|
sb.append('}');
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -44,7 +44,7 @@ public class OAuth2ClientProperties {
|
|||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ConfigurationProperties(prefix = "security.oauth2.client")
|
@ConfigurationProperties(prefix = "security.oauth2.client")
|
||||||
private static class ClientProperties {
|
public static class ClientProperties {
|
||||||
private String clientId;
|
private String clientId;
|
||||||
private String clientSecret;
|
private String clientSecret;
|
||||||
private String userAuthorizationUri;
|
private String userAuthorizationUri;
|
||||||
|
23
module-oauth2-client/pom.xml
Normal file
23
module-oauth2-client/pom.xml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>wg-basic</artifactId>
|
||||||
|
<groupId>ink.wgink</groupId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>module-oauth2-client</artifactId>
|
||||||
|
<description>oauth2客户端</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>ink.wgink</groupId>
|
||||||
|
<artifactId>common</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,68 @@
|
|||||||
|
package ink.wgink.module.oauth2.manager;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import ink.wgink.module.oauth2.pojo.AccessToken;
|
||||||
|
import ink.wgink.module.oauth2.remote.IAccessTokenRemoteService;
|
||||||
|
import ink.wgink.properties.ApiPathProperties;
|
||||||
|
import ink.wgink.properties.oauth2.client.OAuth2ClientProperties;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When you feel like quitting. Think about why you started
|
||||||
|
* 当你想要放弃的时候,想想当初你为何开始
|
||||||
|
*
|
||||||
|
* @ClassName: ClientTokenManager
|
||||||
|
* @Description: 客户单token
|
||||||
|
* @Author: WangGeng
|
||||||
|
* @Date: 2019/10/16 12:22 上午
|
||||||
|
* @Version: 1.0
|
||||||
|
**/
|
||||||
|
public class OAuth2ClientTokenManager {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(OAuth2ClientTokenManager.class);
|
||||||
|
private static OAuth2ClientTokenManager oAuth2ClientTokenManager = OAuth2ClientTokenBuilder.oAuth2ClientTokenManager;
|
||||||
|
private AccessToken accessToken;
|
||||||
|
private IAccessTokenRemoteService accessTokenRemoteService;
|
||||||
|
private OAuth2ClientProperties oAuth2ClientProperties;
|
||||||
|
private ApiPathProperties apiPathProperties;
|
||||||
|
|
||||||
|
private OAuth2ClientTokenManager() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OAuth2ClientTokenManager getInstance() {
|
||||||
|
return oAuth2ClientTokenManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AccessToken getAccessToken() {
|
||||||
|
if (accessToken == null || (System.currentTimeMillis() - accessToken.getCreateTime()) / 1000 >= accessToken.getExpiresIn()) {
|
||||||
|
LOG.debug("客户端Token失效,获取token");
|
||||||
|
JSONObject accessTokenJsonObject = accessTokenRemoteService.getAccessToken(apiPathProperties.getUserCenter(), "client_credentials", oAuth2ClientProperties.getClient().getClientId(), oAuth2ClientProperties.getClient().getClientSecret(), "all");
|
||||||
|
accessToken = new AccessToken();
|
||||||
|
accessToken.setAccessToken(accessTokenJsonObject.getString("access_token"));
|
||||||
|
accessToken.setTokenType(accessTokenJsonObject.getString("token_type"));
|
||||||
|
accessToken.setExpiresIn(accessTokenJsonObject.getInteger("expires_in"));
|
||||||
|
accessToken.setScope(accessTokenJsonObject.getString("scope"));
|
||||||
|
accessToken.setJti(accessTokenJsonObject.getString("jti"));
|
||||||
|
accessToken.setCreateTime(System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccessTokenRemoteService(IAccessTokenRemoteService accessTokenRemoteService) {
|
||||||
|
this.accessTokenRemoteService = accessTokenRemoteService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOAuth2ClientProperties(OAuth2ClientProperties oAuth2ClientProperties) {
|
||||||
|
this.oAuth2ClientProperties = oAuth2ClientProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiPathProperties(ApiPathProperties apiPathProperties) {
|
||||||
|
this.apiPathProperties = apiPathProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class OAuth2ClientTokenBuilder {
|
||||||
|
public static OAuth2ClientTokenManager oAuth2ClientTokenManager = new OAuth2ClientTokenManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package ink.wgink.module.oauth2.pojo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When you feel like quitting. Think about why you started
|
||||||
|
* 当你想要放弃的时候,想想当初你为何开始
|
||||||
|
*
|
||||||
|
* @ClassName: AccessToken
|
||||||
|
* @Description: AccessToken
|
||||||
|
* @Author: WangGeng
|
||||||
|
* @Date: 2021/9/21 12:43
|
||||||
|
* @Version: 1.0
|
||||||
|
**/
|
||||||
|
public class AccessToken {
|
||||||
|
|
||||||
|
private String accessToken;
|
||||||
|
private String tokenType;
|
||||||
|
private int expiresIn;
|
||||||
|
private String scope;
|
||||||
|
private String jti;
|
||||||
|
private long createTime;
|
||||||
|
|
||||||
|
public String getAccessToken() {
|
||||||
|
return accessToken == null ? "" : accessToken.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccessToken(String accessToken) {
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTokenType() {
|
||||||
|
return tokenType == null ? "" : tokenType.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTokenType(String tokenType) {
|
||||||
|
this.tokenType = tokenType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getExpiresIn() {
|
||||||
|
return expiresIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExpiresIn(int expiresIn) {
|
||||||
|
this.expiresIn = expiresIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getScope() {
|
||||||
|
return scope == null ? "" : scope.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScope(String scope) {
|
||||||
|
this.scope = scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getJti() {
|
||||||
|
return jti == null ? "" : jti.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJti(String jti) {
|
||||||
|
this.jti = jti;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCreateTime() {
|
||||||
|
return createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateTime(long createTime) {
|
||||||
|
this.createTime = createTime;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package ink.wgink.module.oauth2.remote;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import ink.wgink.annotation.rpc.rest.RemoteService;
|
||||||
|
import ink.wgink.annotation.rpc.rest.method.RemotePostMethod;
|
||||||
|
import ink.wgink.annotation.rpc.rest.params.RemoteQueryParams;
|
||||||
|
import ink.wgink.annotation.rpc.rest.params.RemoteServerParams;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When you feel like quitting. Think about why you started
|
||||||
|
* 当你想要放弃的时候,想想当初你为何开始
|
||||||
|
*
|
||||||
|
* @ClassName: IAccessTokenRemoteService
|
||||||
|
* @Description: accessToken
|
||||||
|
* @Author: WangGeng
|
||||||
|
* @Date: 2021/9/21 12:44
|
||||||
|
* @Version: 1.0
|
||||||
|
**/
|
||||||
|
@RemoteService
|
||||||
|
public interface IAccessTokenRemoteService {
|
||||||
|
|
||||||
|
@RemotePostMethod("/oauth_client/token")
|
||||||
|
JSONObject getAccessToken(@RemoteServerParams String server,
|
||||||
|
@RemoteQueryParams("grant_type") String grantType,
|
||||||
|
@RemoteQueryParams("client_id") String clientId,
|
||||||
|
@RemoteQueryParams("client_secret") String clientSecret,
|
||||||
|
@RemoteQueryParams("scope") String scope);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
package ink.wgink.module.oauth2.startup;
|
||||||
|
|
||||||
|
import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
|
||||||
|
import ink.wgink.module.oauth2.remote.IAccessTokenRemoteService;
|
||||||
|
import ink.wgink.properties.ApiPathProperties;
|
||||||
|
import ink.wgink.properties.oauth2.client.OAuth2ClientProperties;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When you feel like quitting. Think about why you started
|
||||||
|
* 当你想要放弃的时候,想想当初你为何开始
|
||||||
|
*
|
||||||
|
* @ClassName: OAuth2ClientStartUp
|
||||||
|
* @Description: OAuth2Client启动
|
||||||
|
* @Author: WangGeng
|
||||||
|
* @Date: 2021/9/21 14:07
|
||||||
|
* @Version: 1.0
|
||||||
|
**/
|
||||||
|
@Component
|
||||||
|
public class OAuth2ClientStartUp implements ApplicationRunner {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IAccessTokenRemoteService accessTokenRemoteService;
|
||||||
|
@Autowired
|
||||||
|
private OAuth2ClientProperties oAuth2ClientProperties;
|
||||||
|
@Autowired
|
||||||
|
private ApiPathProperties apiPathProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
OAuth2ClientTokenManager oAuth2ClientTokenManager = OAuth2ClientTokenManager.getInstance();
|
||||||
|
oAuth2ClientTokenManager.setAccessTokenRemoteService(accessTokenRemoteService);
|
||||||
|
oAuth2ClientTokenManager.setOAuth2ClientProperties(oAuth2ClientProperties);
|
||||||
|
oAuth2ClientTokenManager.setApiPathProperties(apiPathProperties);
|
||||||
|
refreshAccessToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 0/15 * * * ?")
|
||||||
|
public void refreshAccessToken() {
|
||||||
|
OAuth2ClientTokenManager.getInstance().getAccessToken();
|
||||||
|
}
|
||||||
|
}
|
1
pom.xml
1
pom.xml
@ -39,6 +39,7 @@
|
|||||||
<module>module-activiti</module>
|
<module>module-activiti</module>
|
||||||
<module>module-instant-message</module>
|
<module>module-instant-message</module>
|
||||||
<module>login-oauth2-client</module>
|
<module>login-oauth2-client</module>
|
||||||
|
<module>module-oauth2-client</module>
|
||||||
</modules>
|
</modules>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user