修复 sa-token-oauth2 模块代码警告

This commit is contained in:
click33
2023-05-16 16:26:26 +08:00
parent cbde6c830e
commit 3b19111f12
6 changed files with 27 additions and 28 deletions

View File

@@ -28,7 +28,7 @@ public class SaOAuth2Manager {
/**
* OAuth2 配置 Bean
*/
private static SaOAuth2Config config;
private static volatile SaOAuth2Config config;
public static SaOAuth2Config getConfig() {
if (config == null) {
// 初始化默认值

View File

@@ -130,7 +130,7 @@ public class SaOAuth2Handle {
public static Object authorize(SaRequest req, SaResponse res, SaOAuth2Config cfg) {
// 1、如果尚未登录, 则先去登录
if(StpUtil.isLogin() == false) {
if( ! StpUtil.isLogin()) {
return cfg.getNotLoginView().get();
}
@@ -145,7 +145,7 @@ public class SaOAuth2Handle {
// 5、判断如果此次申请的Scope该用户尚未授权则转到授权页面
boolean isGrant = SaOAuth2Util.isGrant(ra.loginId, ra.clientId, ra.scope);
if(isGrant == false) {
if( ! isGrant) {
return cfg.getConfirmView().apply(ra.clientId, ra.scope);
}
@@ -283,7 +283,7 @@ public class SaOAuth2Handle {
// 3、调用API 开始登录,如果没能成功登录,则直接退出
Object retObj = cfg.getDoLoginHandle().apply(username, password);
if(StpUtil.isLogin() == false) {
if( ! StpUtil.isLogin()) {
return retObj;
}

View File

@@ -112,7 +112,7 @@ public class SaOAuth2Template {
AccessTokenModel at = checkAccessToken(accessToken);
List<String> scopeList = SaFoxUtil.convertStringToList(at.scope);
for (String scope : scopes) {
SaOAuth2Exception.throwBy(scopeList.contains(scope) == false, "该 Access-Token 不具备 Scope" + scope, SaOAuth2ErrorCode.CODE_30108);
SaOAuth2Exception.throwBy( ! scopeList.contains(scope), "该 Access-Token 不具备 Scope" + scope, SaOAuth2ErrorCode.CODE_30108);
}
}
/**
@@ -127,7 +127,7 @@ public class SaOAuth2Template {
ClientTokenModel ct = checkClientToken(clientToken);
List<String> scopeList = SaFoxUtil.convertStringToList(ct.scope);
for (String scope : scopes) {
SaOAuth2Exception.throwBy(scopeList.contains(scope) == false, "该 Client-Token 不具备 Scope" + scope, SaOAuth2ErrorCode.CODE_30109);
SaOAuth2Exception.throwBy( ! scopeList.contains(scope), "该 Client-Token 不具备 Scope" + scope, SaOAuth2ErrorCode.CODE_30109);
}
}
@@ -313,7 +313,7 @@ public class SaOAuth2Template {
*/
public String buildRedirectUri(String redirectUri, String code, String state) {
String url = SaFoxUtil.joinParam(redirectUri, Param.code, code);
if(SaFoxUtil.isEmpty(state) == false) {
if( ! SaFoxUtil.isEmpty(state)) {
url = SaFoxUtil.joinParam(url, Param.state, state);
}
return url;
@@ -327,7 +327,7 @@ public class SaOAuth2Template {
*/
public String buildImplicitRedirectUri(String redirectUri, String token, String state) {
String url = SaFoxUtil.joinSharpParam(redirectUri, Param.token, token);
if(SaFoxUtil.isEmpty(state) == false) {
if( ! SaFoxUtil.isEmpty(state)) {
url = SaFoxUtil.joinSharpParam(url, Param.state, state);
}
return url;
@@ -375,7 +375,7 @@ public class SaOAuth2Template {
public void checkContract(String clientId, String scope) {
List<String> clientScopeList = SaFoxUtil.convertStringToList(checkClientModel(clientId).contractScope);
List<String> scopelist = SaFoxUtil.convertStringToList(scope);
if(clientScopeList.containsAll(scopelist) == false) {
if( ! clientScopeList.containsAll(scopelist)) {
throw new SaOAuth2Exception("请求的Scope暂未签约").setCode(SaOAuth2ErrorCode.CODE_30112);
}
}
@@ -386,7 +386,7 @@ public class SaOAuth2Template {
*/
public void checkRightUrl(String clientId, String url) {
// 1、是否是一个有效的url
if(SaFoxUtil.isUrl(url) == false) {
if( ! SaFoxUtil.isUrl(url)) {
throw new SaOAuth2Exception("无效redirect_url" + url).setCode(SaOAuth2ErrorCode.CODE_30113);
}
@@ -398,7 +398,7 @@ public class SaOAuth2Template {
// 3、是否在[允许地址列表]之中
List<String> allowList = SaFoxUtil.convertStringToList(checkClientModel(clientId).allowUrl);
if(SaStrategy.me.hasElement.apply(allowList, url) == false) {
if( ! SaStrategy.me.hasElement.apply(allowList, url)) {
throw new SaOAuth2Exception("非法redirect_url" + url).setCode(SaOAuth2ErrorCode.CODE_30114);
}
}
@@ -410,7 +410,7 @@ public class SaOAuth2Template {
*/
public SaClientModel checkClientSecret(String clientId, String clientSecret) {
SaClientModel cm = checkClientModel(clientId);
SaOAuth2Exception.throwBy(cm.clientSecret == null || cm.clientSecret.equals(clientSecret) == false,
SaOAuth2Exception.throwBy(cm.clientSecret == null || ! cm.clientSecret.equals(clientSecret),
"无效client_secret: " + clientSecret, SaOAuth2ErrorCode.CODE_30115);
return cm;
}
@@ -427,7 +427,7 @@ public class SaOAuth2Template {
// 再校验 是否签约
List<String> clientScopeList = SaFoxUtil.convertStringToList(cm.contractScope);
List<String> scopelist = SaFoxUtil.convertStringToList(scopes);
if(clientScopeList.containsAll(scopelist) == false) {
if( ! clientScopeList.containsAll(scopelist)) {
throw new SaOAuth2Exception("请求的Scope暂未签约").setCode(SaOAuth2ErrorCode.CODE_30116);
}
// 返回数据
@@ -448,18 +448,18 @@ public class SaOAuth2Template {
SaOAuth2Exception.throwBy(cm == null, "无效code: " + code, SaOAuth2ErrorCode.CODE_30117);
// 校验ClientId是否一致
SaOAuth2Exception.throwBy(cm.clientId.equals(clientId) == false, "无效client_id: " + clientId, SaOAuth2ErrorCode.CODE_30118);
SaOAuth2Exception.throwBy( ! cm.clientId.equals(clientId), "无效client_id: " + clientId, SaOAuth2ErrorCode.CODE_30118);
// 校验Secret是否正确
String dbSecret = checkClientModel(clientId).clientSecret;
SaOAuth2Exception.throwBy(dbSecret == null || dbSecret.equals(clientSecret) == false, "无效client_secret: " + clientSecret, SaOAuth2ErrorCode.CODE_30119);
SaOAuth2Exception.throwBy(dbSecret == null || ! dbSecret.equals(clientSecret), "无效client_secret: " + clientSecret, SaOAuth2ErrorCode.CODE_30119);
// 如果提供了redirectUri则校验其是否与请求Code时提供的一致
if(SaFoxUtil.isEmpty(redirectUri) == false) {
SaOAuth2Exception.throwBy(redirectUri.equals(cm.redirectUri) == false, "无效redirect_uri: " + redirectUri, SaOAuth2ErrorCode.CODE_30120);
if( ! SaFoxUtil.isEmpty(redirectUri)) {
SaOAuth2Exception.throwBy( ! redirectUri.equals(cm.redirectUri), "无效redirect_uri: " + redirectUri, SaOAuth2ErrorCode.CODE_30120);
}
// 返回CodeMdoel
// 返回CodeModel
return cm;
}
/**
@@ -476,11 +476,11 @@ public class SaOAuth2Template {
SaOAuth2Exception.throwBy(rt == null, "无效refresh_token: " + refreshToken, SaOAuth2ErrorCode.CODE_30121);
// 校验ClientId是否一致
SaOAuth2Exception.throwBy(rt.clientId.equals(clientId) == false, "无效client_id: " + clientId, SaOAuth2ErrorCode.CODE_30122);
SaOAuth2Exception.throwBy( ! rt.clientId.equals(clientId), "无效client_id: " + clientId, SaOAuth2ErrorCode.CODE_30122);
// 校验Secret是否正确
String dbSecret = checkClientModel(clientId).clientSecret;
SaOAuth2Exception.throwBy(dbSecret == null || dbSecret.equals(clientSecret) == false, "无效client_secret: " + clientSecret, SaOAuth2ErrorCode.CODE_30123);
SaOAuth2Exception.throwBy(dbSecret == null || ! dbSecret.equals(clientSecret), "无效client_secret: " + clientSecret, SaOAuth2ErrorCode.CODE_30123);
// 返回Refresh-Token
return rt;
@@ -494,7 +494,7 @@ public class SaOAuth2Template {
*/
public AccessTokenModel checkAccessTokenParam(String clientId, String clientSecret, String accessToken) {
AccessTokenModel at = checkAccessToken(accessToken);
SaOAuth2Exception.throwBy(at.clientId.equals(clientId) == false, "无效client_id" + clientId, SaOAuth2ErrorCode.CODE_30124);
SaOAuth2Exception.throwBy( ! at.clientId.equals(clientId), "无效client_id" + clientId, SaOAuth2ErrorCode.CODE_30124);
checkClientSecret(clientId, clientSecret);
return at;
}
@@ -657,7 +657,7 @@ public class SaOAuth2Template {
if(ct == null) {
return;
}
Long ttl = ct.getExpiresIn();
long ttl = ct.getExpiresIn();
SaClientModel cm = checkClientModel(ct.clientId);
if (cm.getPastClientTokenTimeout() != -1) {
ttl = cm.getPastClientTokenTimeout();
@@ -671,7 +671,7 @@ public class SaOAuth2Template {
* @param scope 权限列表(多个逗号隔开)
*/
public void saveGrantScope(String clientId, Object loginId, String scope) {
if(SaFoxUtil.isEmpty(scope) == false) {
if( ! SaFoxUtil.isEmpty(scope)) {
long ttl = checkClientModel(clientId).getAccessTokenTimeout();
SaManager.getSaTokenDao().set(splicingGrantScopeKey(clientId, loginId), scope, ttl);
}
@@ -966,7 +966,7 @@ public class SaOAuth2Template {
* @return key
*/
public String splicingClientTokenIndexKey(String clientId) {
return SaManager.getConfig().getTokenName() + ":oauth2:client-token-indedx:" + clientId;
return SaManager.getConfig().getTokenName() + ":oauth2:client-token-index:" + clientId;
}
/**
* 拼接keyPast-Token 索引
@@ -974,7 +974,7 @@ public class SaOAuth2Template {
* @return key
*/
public String splicingPastTokenIndexKey(String clientId) {
return SaManager.getConfig().getTokenName() + ":oauth2:past-token-indedx:" + clientId;
return SaManager.getConfig().getTokenName() + ":oauth2:past-token-index:" + clientId;
}
/**
* 拼接key用户授权记录

View File

@@ -116,7 +116,7 @@ public class AccessTokenModel implements Serializable {
* @return 属性转Map
*/
public Map<String, Object> toLineMap() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("access_token", accessToken);
map.put("refresh_token", refreshToken);
map.put("expires_in", getExpiresIn());

View File

@@ -84,7 +84,7 @@ public class ClientTokenModel implements Serializable {
* @return 属性转Map
*/
public Map<String, Object> toLineMap() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("client_token", clientToken);
map.put("expires_in", getExpiresIn());
map.put("client_id", clientId);

View File

@@ -215,7 +215,6 @@ public class SaClientModel implements Serializable {
/**
* @return 此 Client 是否打开模式凭证式Client Credentials
* @return 对象自身
*/
public Boolean getIsClient() {
return isClient;