细节优化、文档优化

This commit is contained in:
click33
2024-08-23 16:18:24 +08:00
parent 3345e3aaf9
commit 419ca3797c
30 changed files with 226 additions and 199 deletions

View File

@@ -15,7 +15,7 @@
*/
package cn.dev33.satoken.oauth2;
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig;
import cn.dev33.satoken.oauth2.dao.SaOAuth2Dao;
import cn.dev33.satoken.oauth2.dao.SaOAuth2DaoDefaultImpl;
import cn.dev33.satoken.oauth2.data.convert.SaOAuth2DataConverter;
@@ -41,19 +41,19 @@ public class SaOAuth2Manager {
/**
* OAuth2 配置 Bean
*/
private static volatile SaOAuth2Config config;
public static SaOAuth2Config getConfig() {
private static volatile SaOAuth2ServerConfig config;
public static SaOAuth2ServerConfig getConfig() {
if (config == null) {
// 初始化默认值
synchronized (SaOAuth2Manager.class) {
if (config == null) {
setConfig(new SaOAuth2Config());
setConfig(new SaOAuth2ServerConfig());
}
}
}
return config;
}
public static void setConfig(SaOAuth2Config config) {
public static void setConfig(SaOAuth2ServerConfig config) {
SaOAuth2Manager.config = config;
}

View File

@@ -24,17 +24,17 @@ import cn.dev33.satoken.util.SaResult;
import java.io.Serializable;
/**
* Sa-Token-OAuth2 配置类 Model
* Sa-Token OAuth2 Server 配置类 Model
*
* @author click33
* @since 1.19.0
*/
public class SaOAuth2Config implements Serializable {
public class SaOAuth2ServerConfig implements Serializable {
private static final long serialVersionUID = -6541180061782004705L;
/** 是否打开模式授权码Authorization Code */
public Boolean enableCode = true;
public Boolean enableAuthorizationCode = true;
/** 是否打开模式隐藏式Implicit */
public Boolean enableImplicit = true;
@@ -43,7 +43,7 @@ public class SaOAuth2Config implements Serializable {
public Boolean enablePassword = true;
/** 是否打开模式凭证式Client Credentials */
public Boolean enableClient = true;
public Boolean enableClientCredentials = true;
/** 是否在每次 Refresh-Token 刷新 Access-Token 时,产生一个新的 Refresh-Token */
public Boolean isNewRefresh = false;
@@ -75,16 +75,16 @@ public class SaOAuth2Config implements Serializable {
/**
* @return enableCode
*/
public Boolean getEnableCode() {
return enableCode;
public Boolean getEnableAuthorizationCode() {
return enableAuthorizationCode;
}
/**
* @param enableCode 要设置的 enableCode
* @param enableAuthorizationCode 要设置的 enableAuthorizationCode
* @return /
*/
public SaOAuth2Config setEnableCode(Boolean enableCode) {
this.enableCode = enableCode;
public SaOAuth2ServerConfig setEnableAuthorizationCode(Boolean enableAuthorizationCode) {
this.enableAuthorizationCode = enableAuthorizationCode;
return this;
}
@@ -99,7 +99,7 @@ public class SaOAuth2Config implements Serializable {
* @param enableImplicit 要设置的 enableImplicit
* @return /
*/
public SaOAuth2Config setEnableImplicit(Boolean enableImplicit) {
public SaOAuth2ServerConfig setEnableImplicit(Boolean enableImplicit) {
this.enableImplicit = enableImplicit;
return this;
}
@@ -114,24 +114,24 @@ public class SaOAuth2Config implements Serializable {
/**
* @param enablePassword 要设置的 enablePassword
*/
public SaOAuth2Config setEnablePassword(Boolean enablePassword) {
public SaOAuth2ServerConfig setEnablePassword(Boolean enablePassword) {
this.enablePassword = enablePassword;
return this;
}
/**
* @return enableClient
* @return enableClientCredentials
*/
public Boolean getEnableClient() {
return enableClient;
public Boolean getEnableClientCredentials() {
return enableClientCredentials;
}
/**
* @param enableClient 要设置的 enableClient
* @param enableClientCredentials 要设置的 enableClientCredentials
* @return /
*/
public SaOAuth2Config setEnableClient(Boolean enableClient) {
this.enableClient = enableClient;
public SaOAuth2ServerConfig setEnableClientCredentials(Boolean enableClientCredentials) {
this.enableClientCredentials = enableClientCredentials;
return this;
}
@@ -146,7 +146,7 @@ public class SaOAuth2Config implements Serializable {
* @param isNewRefresh 要设置的 isNewRefresh
* @return /
*/
public SaOAuth2Config setIsNewRefresh(Boolean isNewRefresh) {
public SaOAuth2ServerConfig setIsNewRefresh(Boolean isNewRefresh) {
this.isNewRefresh = isNewRefresh;
return this;
}
@@ -162,7 +162,7 @@ public class SaOAuth2Config implements Serializable {
* @param codeTimeout 要设置的 codeTimeout
* @return 对象自身
*/
public SaOAuth2Config setCodeTimeout(long codeTimeout) {
public SaOAuth2ServerConfig setCodeTimeout(long codeTimeout) {
this.codeTimeout = codeTimeout;
return this;
}
@@ -178,7 +178,7 @@ public class SaOAuth2Config implements Serializable {
* @param accessTokenTimeout 要设置的 accessTokenTimeout
* @return 对象自身
*/
public SaOAuth2Config setAccessTokenTimeout(long accessTokenTimeout) {
public SaOAuth2ServerConfig setAccessTokenTimeout(long accessTokenTimeout) {
this.accessTokenTimeout = accessTokenTimeout;
return this;
}
@@ -194,7 +194,7 @@ public class SaOAuth2Config implements Serializable {
* @param refreshTokenTimeout 要设置的 refreshTokenTimeout
* @return 对象自身
*/
public SaOAuth2Config setRefreshTokenTimeout(long refreshTokenTimeout) {
public SaOAuth2ServerConfig setRefreshTokenTimeout(long refreshTokenTimeout) {
this.refreshTokenTimeout = refreshTokenTimeout;
return this;
}
@@ -210,7 +210,7 @@ public class SaOAuth2Config implements Serializable {
* @param clientTokenTimeout 要设置的 clientTokenTimeout
* @return 对象自身
*/
public SaOAuth2Config setClientTokenTimeout(long clientTokenTimeout) {
public SaOAuth2ServerConfig setClientTokenTimeout(long clientTokenTimeout) {
this.clientTokenTimeout = clientTokenTimeout;
return this;
}
@@ -226,7 +226,7 @@ public class SaOAuth2Config implements Serializable {
* @param pastClientTokenTimeout 要设置的 pastClientTokenTimeout
* @return 对象自身
*/
public SaOAuth2Config setPastClientTokenTimeout(long pastClientTokenTimeout) {
public SaOAuth2ServerConfig setPastClientTokenTimeout(long pastClientTokenTimeout) {
this.pastClientTokenTimeout = pastClientTokenTimeout;
return this;
}
@@ -242,7 +242,7 @@ public class SaOAuth2Config implements Serializable {
* @param openidDigestPrefix 要设置的 openidDigestPrefix
* @return 对象自身
*/
public SaOAuth2Config setOpenidDigestPrefix(String openidDigestPrefix) {
public SaOAuth2ServerConfig setOpenidDigestPrefix(String openidDigestPrefix) {
this.openidDigestPrefix = openidDigestPrefix;
return this;
}
@@ -262,7 +262,7 @@ public class SaOAuth2Config implements Serializable {
* @param higherScope 指定高级权限多个用逗号隔开
* @return /
*/
public SaOAuth2Config setHigherScope(String higherScope) {
public SaOAuth2ServerConfig setHigherScope(String higherScope) {
this.higherScope = higherScope;
return this;
}
@@ -282,7 +282,7 @@ public class SaOAuth2Config implements Serializable {
* @param lowerScope 指定低级权限多个用逗号隔开
* @return /
*/
public SaOAuth2Config setLowerScope(String lowerScope) {
public SaOAuth2ServerConfig setLowerScope(String lowerScope) {
this.lowerScope = lowerScope;
return this;
}
@@ -307,11 +307,11 @@ public class SaOAuth2Config implements Serializable {
@Override
public String toString() {
return "SaOAuth2Config{" +
"enableCode=" + enableCode +
return "SaOAuth2ServerConfig{" +
"enableAuthorizationCode=" + enableAuthorizationCode +
", enableImplicit=" + enableImplicit +
", enablePassword=" + enablePassword +
", enableClient=" + enableClient +
", enableClientCredentials=" + enableClientCredentials +
", isNewRefresh=" + isNewRefresh +
", codeTimeout=" + codeTimeout +
", accessTokenTimeout=" + accessTokenTimeout +

View File

@@ -23,7 +23,7 @@ import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
import cn.dev33.satoken.oauth2.strategy.SaOAuth2Strategy;
import cn.dev33.satoken.util.SaFoxUtil;
import java.util.Collections;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@@ -41,7 +41,7 @@ public class SaOAuth2DataConverterDefaultImpl implements SaOAuth2DataConverter {
@Override
public List<String> convertScopeStringToList(String scopeString) {
if(SaFoxUtil.isEmpty(scopeString)) {
return Collections.emptyList();
return new ArrayList<>();
}
// 兼容以下三种分隔符:空格、逗号、%20
scopeString = scopeString.replaceAll(" ", ",");
@@ -63,7 +63,7 @@ public class SaOAuth2DataConverterDefaultImpl implements SaOAuth2DataConverter {
@Override
public List<String> convertAllowUrlStringToList(String allowUrl) {
if(SaFoxUtil.isEmpty(allowUrl)) {
return Collections.emptyList();
return new ArrayList<>();
}
return SaFoxUtil.convertStringToList(allowUrl);
}

View File

@@ -16,7 +16,7 @@
package cn.dev33.satoken.oauth2.data.model.loader;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig;
import java.io.Serializable;
import java.util.ArrayList;
@@ -49,9 +49,9 @@ public class SaClientModel implements Serializable {
public List<String> contractScopes;
/**
* 应用允许授权的所有URL
* 应用允许授权的所有 redirect_uri
*/
public List<String> allowUrls;
public List<String> allowRedirectUris;
/**
* 应用允许的所有 grant_type
@@ -75,19 +75,19 @@ public class SaClientModel implements Serializable {
public SaClientModel() {
SaOAuth2Config config = SaOAuth2Manager.getConfig();
SaOAuth2ServerConfig config = SaOAuth2Manager.getConfig();
this.isNewRefresh = config.getIsNewRefresh();
this.accessTokenTimeout = config.getAccessTokenTimeout();
this.refreshTokenTimeout = config.getRefreshTokenTimeout();
this.clientTokenTimeout = config.getClientTokenTimeout();
this.pastClientTokenTimeout = config.getPastClientTokenTimeout();
}
public SaClientModel(String clientId, String clientSecret, List<String> contractScopes, List<String> allowUrls) {
public SaClientModel(String clientId, String clientSecret, List<String> contractScopes, List<String> allowRedirectUris) {
super();
this.clientId = clientId;
this.clientSecret = clientSecret;
this.contractScopes = contractScopes;
this.allowUrls = allowUrls;
this.allowRedirectUris = allowRedirectUris;
}
/**
@@ -139,18 +139,18 @@ public class SaClientModel implements Serializable {
}
/**
* @return 应用允许授权的所有URL
* @return 应用允许授权的所有 redirect_uri
*/
public List<String> getAllowUrls() {
return allowUrls;
public List<String> getAllowRedirectUris() {
return allowRedirectUris;
}
/**
* @param allowUrls 应用允许授权的所有URL
* @param allowRedirectUris 应用允许授权的所有 redirect_uri
* @return 对象自身
*/
public SaClientModel setAllowUrls(List<String> allowUrls) {
this.allowUrls = allowUrls;
public SaClientModel setAllowRedirectUris(List<String> allowRedirectUris) {
this.allowRedirectUris = allowRedirectUris;
return this;
}
@@ -259,7 +259,7 @@ public class SaClientModel implements Serializable {
"clientId='" + clientId + '\'' +
", clientSecret='" + clientSecret + '\'' +
", contractScopes=" + contractScopes +
", allowUrls=" + allowUrls +
", allowRedirectUris=" + allowRedirectUris +
", allowGrantTypes=" + allowGrantTypes +
", isNewRefresh=" + isNewRefresh +
", accessTokenTimeout=" + accessTokenTimeout +
@@ -285,14 +285,14 @@ public class SaClientModel implements Serializable {
}
/**
* @param urls 添加应用允许授权的所有URL
* @param redirectUris 添加应用允许授权的所有 redirect_uri
* @return 对象自身
*/
public SaClientModel addAllowUrls(String... urls) {
if(this.allowUrls == null) {
this.allowUrls = new ArrayList<>();
public SaClientModel addAllowRedirectUris(String... redirectUris) {
if(this.allowRedirectUris == null) {
this.allowRedirectUris = new ArrayList<>();
}
this.allowUrls.addAll(Arrays.asList(urls));
this.allowRedirectUris.addAll(Arrays.asList(redirectUris));
return this;
}

View File

@@ -38,7 +38,7 @@ public class AuthorizationCodeGrantTypeHandler implements SaOAuth2GrantTypeHandl
}
@Override
public AccessTokenModel getAccessTokenModel(SaRequest req, String clientId, List<String> scopes) {
public AccessTokenModel getAccessToken(SaRequest req, String clientId, List<String> scopes) {
// 获取参数
ClientIdAndSecretModel clientIdAndSecret = SaOAuth2Manager.getDataResolver().readClientIdAndSecret(req);
// String clientId = clientIdAndSecret.clientId;

View File

@@ -40,7 +40,7 @@ public class PasswordGrantTypeHandler implements SaOAuth2GrantTypeHandlerInterfa
}
@Override
public AccessTokenModel getAccessTokenModel(SaRequest req, String clientId, List<String> scopes) {
public AccessTokenModel getAccessToken(SaRequest req, String clientId, List<String> scopes) {
// 1、获取请求参数
String username = req.getParamNotNull(SaOAuth2Consts.Param.username);

View File

@@ -40,7 +40,7 @@ public class RefreshTokenGrantTypeHandler implements SaOAuth2GrantTypeHandlerInt
}
@Override
public AccessTokenModel getAccessTokenModel(SaRequest req, String clientId, List<String> scopes) {
public AccessTokenModel getAccessToken(SaRequest req, String clientId, List<String> scopes) {
// 获取参数
String refreshToken = req.getParamNotNull(SaOAuth2Consts.Param.refresh_token);

View File

@@ -41,6 +41,6 @@ public interface SaOAuth2GrantTypeHandlerInterface {
* @param req /
* @return /
*/
AccessTokenModel getAccessTokenModel(SaRequest req, String clientId, List<String> scopes);
AccessTokenModel getAccessToken(SaRequest req, String clientId, List<String> scopes);
}

View File

@@ -19,7 +19,7 @@ import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig;
import cn.dev33.satoken.oauth2.consts.GrantType;
import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts;
import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts.Api;
@@ -113,7 +113,7 @@ public class SaOAuth2ServerProcessor {
// 获取变量
SaRequest req = SaHolder.getRequest();
SaResponse res = SaHolder.getResponse();
SaOAuth2Config cfg = SaOAuth2Manager.getConfig();
SaOAuth2ServerConfig cfg = SaOAuth2Manager.getConfig();
SaOAuth2DataGenerate dataGenerate = SaOAuth2Manager.getDataGenerate();
SaOAuth2Template oauth2Template = SaOAuth2Manager.getTemplate();
String responseType = req.getParamNotNull(Param.response_type);
@@ -218,7 +218,7 @@ public class SaOAuth2ServerProcessor {
public Object doLogin() {
// 获取变量
SaRequest req = SaHolder.getRequest();
SaOAuth2Config cfg = SaOAuth2Manager.getConfig();
SaOAuth2ServerConfig cfg = SaOAuth2Manager.getConfig();
return cfg.doLoginHandle.apply(req.getParam(Param.name), req.getParam(Param.pwd));
}
@@ -285,14 +285,14 @@ public class SaOAuth2ServerProcessor {
public Object clientToken() {
// 获取变量
SaRequest req = SaHolder.getRequest();
SaOAuth2Config cfg = SaOAuth2Manager.getConfig();
SaOAuth2ServerConfig cfg = SaOAuth2Manager.getConfig();
SaOAuth2Template oauth2Template = SaOAuth2Manager.getTemplate();
String grantType = req.getParamNotNull(Param.grant_type);
if(!grantType.equals(GrantType.client_credentials)) {
throw new SaOAuth2Exception("无效 grant_type" + grantType).setCode(SaOAuth2ErrorCode.CODE_30126);
}
if(!cfg.enableClient) {
if(!cfg.enableClientCredentials) {
throwErrorSystemNotEnableModel();
}
if(!currClientModel().getAllowGrantTypes().contains(GrantType.client_credentials)) {
@@ -335,10 +335,10 @@ public class SaOAuth2ServerProcessor {
/**
* 校验 authorize 路由的 ResponseType 参数
*/
public void checkAuthorizeResponseType(String responseType, SaRequest req, SaOAuth2Config cfg) {
public void checkAuthorizeResponseType(String responseType, SaRequest req, SaOAuth2ServerConfig cfg) {
// 模式一Code授权码
if(responseType.equals(ResponseType.code)) {
if(!cfg.enableCode) {
if(!cfg.enableAuthorizationCode) {
throwErrorSystemNotEnableModel();
}
if(!currClientModel().getAllowGrantTypes().contains(GrantType.authorization_code)) {

View File

@@ -17,7 +17,7 @@ package cn.dev33.satoken.oauth2.strategy;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig;
import cn.dev33.satoken.oauth2.consts.GrantType;
import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts;
import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
@@ -170,8 +170,8 @@ public final class SaOAuth2Strategy {
}
// 看看全局是否开启了此 grantType
SaOAuth2Config config = SaOAuth2Manager.getConfig();
if(grantType.equals(GrantType.authorization_code) && !config.getEnableCode() ) {
SaOAuth2ServerConfig config = SaOAuth2Manager.getConfig();
if(grantType.equals(GrantType.authorization_code) && !config.getEnableAuthorizationCode() ) {
throw new SaOAuth2Exception("系统未开放的 grant_type: " + grantType);
}
if(grantType.equals(GrantType.password) && !config.getEnablePassword() ) {
@@ -189,7 +189,7 @@ public final class SaOAuth2Strategy {
}
// 调用 处理器
return grantTypeHandler.getAccessTokenModel(req, clientIdAndSecretModel.getClientId(), scopes);
return grantTypeHandler.getAccessToken(req, clientIdAndSecretModel.getClientId(), scopes);
};

View File

@@ -224,8 +224,8 @@ public class SaOAuth2Template {
// 4、是否在[允许地址列表]之中
SaClientModel clientModel = checkClientModel(clientId);
checkAllowUrlList(clientModel.allowUrls);
if( ! SaStrategy.instance.hasElement.apply(clientModel.allowUrls, url)) {
checkAllowUrlList(clientModel.allowRedirectUris);
if( ! SaStrategy.instance.hasElement.apply(clientModel.allowRedirectUris, url)) {
throw new SaOAuth2Exception("非法 redirect_url: " + url).setCode(SaOAuth2ErrorCode.CODE_30114);
}
}