token创建抽离到全局策略

This commit is contained in:
click33 2024-08-18 18:41:22 +08:00
parent 281985bfdb
commit 1ee1340192
10 changed files with 200 additions and 58 deletions

View File

@ -20,6 +20,7 @@ import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.CodeModel;
import cn.dev33.satoken.oauth2.data.model.RefreshTokenModel;
import cn.dev33.satoken.oauth2.data.model.SaClientModel;
import cn.dev33.satoken.oauth2.strategy.SaOAuth2Strategy;
import cn.dev33.satoken.util.SaFoxUtil;
import java.util.Collections;
@ -72,7 +73,7 @@ public class SaOAuth2DataConverterDefaultImpl implements SaOAuth2DataConverter {
@Override
public AccessTokenModel convertCodeToAccessToken(CodeModel cm) {
AccessTokenModel at = new AccessTokenModel();
at.accessToken = SaOAuth2Manager.getDataLoader().randomAccessToken(cm.clientId, cm.loginId, cm.scopes);
at.accessToken = SaOAuth2Strategy.instance.createAccessToken.execute(cm.clientId, cm.loginId, cm.scopes);
at.clientId = cm.clientId;
at.loginId = cm.loginId;
at.scopes = cm.scopes;
@ -89,7 +90,7 @@ public class SaOAuth2DataConverterDefaultImpl implements SaOAuth2DataConverter {
@Override
public RefreshTokenModel convertAccessTokenToRefreshToken(AccessTokenModel at) {
RefreshTokenModel rt = new RefreshTokenModel();
rt.refreshToken = SaOAuth2Manager.getDataLoader().randomRefreshToken(at.clientId, at.loginId, at.scopes);
rt.refreshToken = SaOAuth2Strategy.instance.createRefreshToken.execute(at.clientId, at.loginId, at.scopes);
rt.clientId = at.clientId;
rt.loginId = at.loginId;
rt.scopes = at.scopes;
@ -110,7 +111,7 @@ public class SaOAuth2DataConverterDefaultImpl implements SaOAuth2DataConverter {
@Override
public AccessTokenModel convertRefreshTokenToAccessToken(RefreshTokenModel rt) {
AccessTokenModel at = new AccessTokenModel();
at.accessToken = SaOAuth2Manager.getDataLoader().randomAccessToken(rt.clientId, rt.loginId, rt.scopes);
at.accessToken = SaOAuth2Strategy.instance.createAccessToken.execute(rt.clientId, rt.loginId, rt.scopes);
at.refreshToken = rt.refreshToken;
at.clientId = rt.clientId;
at.loginId = rt.loginId;
@ -130,7 +131,7 @@ public class SaOAuth2DataConverterDefaultImpl implements SaOAuth2DataConverter {
@Override
public RefreshTokenModel convertRefreshTokenToRefreshToken(RefreshTokenModel rt) {
RefreshTokenModel newRt = new RefreshTokenModel();
newRt.refreshToken = SaOAuth2Manager.getDataLoader().randomRefreshToken(rt.clientId, rt.loginId, rt.scopes);
newRt.refreshToken = SaOAuth2Strategy.instance.createRefreshToken.execute(rt.clientId, rt.loginId, rt.scopes);
SaClientModel clientModel = SaOAuth2Manager.getDataLoader().getClientModelNotNull(rt.clientId);
newRt.expiresTime = System.currentTimeMillis() + (clientModel.getRefreshTokenTimeout() * 1000);
newRt.clientId = rt.clientId;

View File

@ -19,6 +19,7 @@ import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.ClientTokenModel;
import cn.dev33.satoken.oauth2.data.model.CodeModel;
import cn.dev33.satoken.oauth2.data.model.RequestAuthModel;
import cn.dev33.satoken.util.SaFoxUtil;
import java.util.List;
@ -93,4 +94,6 @@ public interface SaOAuth2DataGenerate {
*/
public void revokeAccessToken(String accessToken);
}

View File

@ -50,7 +50,7 @@ public class SaOAuth2DataGenerateDefaultImpl implements SaOAuth2DataGenerate {
dao.deleteCode(dao.getCodeValue(ra.clientId, ra.loginId));
// 生成新Code
String codeValue = SaOAuth2Manager.getDataLoader().randomCode(ra.clientId, ra.loginId, ra.scopes);
String codeValue = SaOAuth2Strategy.instance.createCodeValue.execute(ra.clientId, ra.loginId, ra.scopes);
CodeModel cm = new CodeModel(codeValue, ra.clientId, ra.scopes, ra.loginId, ra.redirectUri);
// 保存新Code
@ -159,7 +159,7 @@ public class SaOAuth2DataGenerateDefaultImpl implements SaOAuth2DataGenerate {
}
// 2生成 新Access-Token
String newAtValue = SaOAuth2Manager.getDataLoader().randomAccessToken(ra.clientId, ra.loginId, ra.scopes);
String newAtValue = SaOAuth2Strategy.instance.createAccessToken.execute(ra.clientId, ra.loginId, ra.scopes);
AccessTokenModel at = new AccessTokenModel(newAtValue, ra.clientId, ra.loginId, ra.scopes);
at.openid = SaOAuth2Manager.getDataLoader().getOpenid(ra.clientId, ra.loginId);
SaClientModel clientModel = SaOAuth2Manager.getDataLoader().getClientModelNotNull(ra.clientId);
@ -206,7 +206,8 @@ public class SaOAuth2DataGenerateDefaultImpl implements SaOAuth2DataGenerate {
}
// 3生成新Client-Token
ClientTokenModel ct = new ClientTokenModel(SaOAuth2Manager.getDataLoader().randomClientToken(clientId, scopes), clientId, scopes);
String clientTokenValue = SaOAuth2Strategy.instance.createClientToken.execute(clientId, scopes);
ClientTokenModel ct = new ClientTokenModel(clientTokenValue, clientId, scopes);
ct.expiresTime = System.currentTimeMillis() + (cm.getClientTokenTimeout() * 1000);
// 3保存新Client-Token

View File

@ -66,49 +66,5 @@ public interface SaOAuth2DataLoader {
}
// ------------------- 创建对应 token 的算法
/**
* 随机一个 Code
* @param clientId 应用id
* @param loginId 账号id
* @param scopes 权限
* @return Code
*/
default String randomCode(String clientId, Object loginId, List<String> scopes) {
return SaFoxUtil.getRandomString(60);
}
/**
* 随机一个 Access-Token
* @param clientId 应用id
* @param loginId 账号id
* @param scopes 权限
* @return Access-Token
*/
default String randomAccessToken(String clientId, Object loginId, List<String> scopes) {
return SaFoxUtil.getRandomString(60);
}
/**
* 随机一个 Refresh-Token
* @param clientId 应用id
* @param loginId 账号id
* @param scopes 权限
* @return Refresh-Token
*/
default String randomRefreshToken(String clientId, Object loginId, List<String> scopes) {
return SaFoxUtil.getRandomString(60);
}
/**
* 随机一个 Client-Token
* @param clientId 应用id
* @param scopes 权限
* @return Client-Token
*/
default String randomClientToken(String clientId, List<String> scopes) {
return SaFoxUtil.getRandomString(60);
}
}

View File

@ -54,13 +54,13 @@ public class ClientTokenModel implements Serializable {
/**
* 构建一个
* @param accessToken accessToken
* @param clientToken clientToken
* @param clientId 应用id
* @param scopes 请求授权范围
*/
public ClientTokenModel(String accessToken, String clientId, List<String> scopes) {
public ClientTokenModel(String clientToken, String clientId, List<String> scopes) {
super();
this.clientToken = accessToken;
this.clientToken = clientToken;
this.clientId = clientId;
this.scopes = scopes;
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* 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 cn.dev33.satoken.oauth2.function.strategy;
import java.util.List;
/**
* 函数式接口创建一个 AccessToken value
*
* @author click33
* @since 1.39.0
*/
@FunctionalInterface
public interface SaOAuth2CreateAccessTokenValueFunction {
/**
* 创建一个 AccessToken value
* @param clientId 应用id
* @param loginId 账号id
* @param scopes 权限
* @return AccessToken value
*/
String execute(String clientId, Object loginId, List<String> scopes);
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* 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 cn.dev33.satoken.oauth2.function.strategy;
import java.util.List;
/**
* 函数式接口创建一个 ClientToken value
*
* @author click33
* @since 1.39.0
*/
@FunctionalInterface
public interface SaOAuth2CreateClientTokenValueFunction {
/**
* 创建一个 ClientToken value
* @param clientId 应用id
* @param scopes 权限
* @return ClientToken value
*/
String execute(String clientId, List<String> scopes);
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* 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 cn.dev33.satoken.oauth2.function.strategy;
import java.util.List;
/**
* 函数式接口创建一个 code value
*
* @author click33
* @since 1.39.0
*/
@FunctionalInterface
public interface SaOAuth2CreateCodeValueFunction {
/**
* 创建一个 code value
* @param clientId 应用id
* @param loginId 账号id
* @param scopes 权限
* @return code value
*/
String execute(String clientId, Object loginId, List<String> scopes);
}

View File

@ -0,0 +1,38 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* 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 cn.dev33.satoken.oauth2.function.strategy;
import java.util.List;
/**
* 函数式接口创建一个 RefreshToken value
*
* @author click33
* @since 1.39.0
*/
@FunctionalInterface
public interface SaOAuth2CreateRefreshTokenValueFunction {
/**
* 创建一个 RefreshToken value
* @param clientId 应用id
* @param loginId 账号id
* @param scopes 权限
* @return RefreshToken value
*/
String execute(String clientId, Object loginId, List<String> scopes);
}

View File

@ -16,10 +16,11 @@
package cn.dev33.satoken.oauth2.strategy;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.oauth2.function.strategy.SaScopeWorkFunction;
import cn.dev33.satoken.oauth2.function.strategy.*;
import cn.dev33.satoken.oauth2.scope.CommonScope;
import cn.dev33.satoken.oauth2.scope.handler.OpenIdScopeHandler;
import cn.dev33.satoken.oauth2.scope.handler.SaOAuth2ScopeAbstractHandler;
import cn.dev33.satoken.util.SaFoxUtil;
import java.util.LinkedHashMap;
import java.util.Map;
@ -41,8 +42,7 @@ public final class SaOAuth2Strategy {
*/
public static final SaOAuth2Strategy instance = new SaOAuth2Strategy();
// ----------------------- 所有策略
// 权限处理器
/**
* 权限处理器集合
@ -63,7 +63,7 @@ public final class SaOAuth2Strategy {
scopeHandlerMap.put(handler.getHandlerScope(), handler);
// TODO 优化日志输出
SaManager.getLog().info("新增权限处理器:" + handler.getHandlerScope());
// SaTokenEventCenter.doRegisterAnnotationHandler(handler);
// SaTokenEventCenter.doRegisterAnnotationHandler(handler);
}
/**
@ -73,6 +73,9 @@ public final class SaOAuth2Strategy {
scopeHandlerMap.remove(scope);
}
// ----------------------- 所有策略
/**
* 根据 scope 信息对一个 AccessTokenModel 进行加工处理
*/
@ -90,5 +93,32 @@ public final class SaOAuth2Strategy {
};
/**
* 创建一个 code value
*/
public SaOAuth2CreateCodeValueFunction createCodeValue = (clientId, loginId, scopes) -> {
return SaFoxUtil.getRandomString(60);
};
/**
* 创建一个 AccessToken value
*/
public SaOAuth2CreateAccessTokenValueFunction createAccessToken = (clientId, loginId, scopes) -> {
return SaFoxUtil.getRandomString(60);
};
/**
* 创建一个 RefreshToken value
*/
public SaOAuth2CreateRefreshTokenValueFunction createRefreshToken = (clientId, loginId, scopes) -> {
return SaFoxUtil.getRandomString(60);
};
/**
* 创建一个 ClientToken value
*/
public SaOAuth2CreateClientTokenValueFunction createClientToken = (clientId, scopes) -> {
return SaFoxUtil.getRandomString(60);
};
}