fefactor(oauth2): 将认证流程回调从 SaOAuth2ServerConfig 转移到 SaOAuth2Strategy
This commit is contained in:
+17
-34
@@ -17,10 +17,6 @@ package cn.dev33.satoken.oauth2.config;
|
||||
|
||||
import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts;
|
||||
import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
|
||||
import cn.dev33.satoken.oauth2.function.SaOAuth2ConfirmViewFunction;
|
||||
import cn.dev33.satoken.oauth2.function.SaOAuth2DoLoginHandleFunction;
|
||||
import cn.dev33.satoken.oauth2.function.SaOAuth2NotLoginViewFunction;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -92,6 +88,23 @@ public class SaOAuth2ServerConfig implements Serializable {
|
||||
/** client 列表 */
|
||||
public Map<String, SaClientModel> clients = new LinkedHashMap<>();
|
||||
|
||||
// 额外方法
|
||||
|
||||
/**
|
||||
* 注册 client
|
||||
* @return /
|
||||
*/
|
||||
public SaOAuth2ServerConfig addClient(SaClientModel client) {
|
||||
if(this.clients == null) {
|
||||
this.clients = new LinkedHashMap<>();
|
||||
}
|
||||
this.clients.put(client.getClientId(), client);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
// get set
|
||||
|
||||
/**
|
||||
* @return enableCode
|
||||
*/
|
||||
@@ -390,24 +403,6 @@ public class SaOAuth2ServerConfig implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
// -------------------- SaOAuth2Handle 所有回调函数 --------------------
|
||||
|
||||
/**
|
||||
* OAuth-Server端:未登录时返回的View
|
||||
*/
|
||||
public SaOAuth2NotLoginViewFunction notLoginView = () -> "当前会话在 OAuth-Server 认证中心尚未登录";
|
||||
|
||||
/**
|
||||
* OAuth-Server端:确认授权时返回的View
|
||||
*/
|
||||
public SaOAuth2ConfirmViewFunction confirmView = (clientId, scopes) -> "本次操作需要用户授权";
|
||||
|
||||
/**
|
||||
* OAuth-Server端:登录函数
|
||||
*/
|
||||
public SaOAuth2DoLoginHandleFunction doLoginHandle = (name, pwd) -> SaResult.error();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SaOAuth2ServerConfig{" +
|
||||
@@ -432,17 +427,5 @@ public class SaOAuth2ServerConfig implements Serializable {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注册 client
|
||||
* @return /
|
||||
*/
|
||||
public SaOAuth2ServerConfig addClient(SaClientModel client) {
|
||||
if(this.clients == null) {
|
||||
this.clients = new LinkedHashMap<>();
|
||||
}
|
||||
this.clients.put(client.getClientId(), client);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+4
-2
@@ -24,6 +24,7 @@ import cn.dev33.satoken.oauth2.data.model.request.RequestAuthModel;
|
||||
import cn.dev33.satoken.oauth2.error.SaOAuth2ErrorCode;
|
||||
import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
|
||||
import cn.dev33.satoken.oauth2.granttype.handler.model.PasswordAuthResult;
|
||||
import cn.dev33.satoken.oauth2.strategy.SaOAuth2Strategy;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
import java.util.List;
|
||||
@@ -72,8 +73,9 @@ public class PasswordGrantTypeHandler implements SaOAuth2GrantTypeHandlerInterfa
|
||||
* @param password /
|
||||
*/
|
||||
public PasswordAuthResult loginByUsernamePassword(String username, String password) {
|
||||
System.err.println("当前暂未重写 PasswordGrantTypeHandler 处理器,将使用默认实现,仅供开发测试");
|
||||
SaOAuth2Manager.getServerConfig().doLoginHandle.apply(username, password);
|
||||
System.err.println("警告信息:当前 password 认证模式,使用默认实现 (SaOAuth2Strategy.instance.doLoginHandle),仅供开发测试");
|
||||
System.err.println("正式项目请重写 PasswordGrantTypeHandler 处理器 loginByUsernamePassword 方法");
|
||||
SaOAuth2Strategy.instance.doLoginHandle.apply(username, password);
|
||||
Object loginId = StpUtil.getLoginIdDefaultNull();
|
||||
return new PasswordAuthResult(loginId);
|
||||
}
|
||||
|
||||
+3
-4
@@ -123,7 +123,7 @@ public class SaOAuth2ServerProcessor {
|
||||
|
||||
// 2、如果尚未登录, 则先去登录
|
||||
if( ! SaOAuth2Manager.getStpLogic().isLogin()) {
|
||||
return cfg.notLoginView.get();
|
||||
return SaOAuth2Strategy.instance.notLoginView.get();
|
||||
}
|
||||
|
||||
// 3、构建请求 Model
|
||||
@@ -140,7 +140,7 @@ public class SaOAuth2ServerProcessor {
|
||||
if(isNeedCarefulConfirm) {
|
||||
SaClientModel cm = oauth2Template.checkClientModel(ra.clientId);
|
||||
if( ! cm.getIsAutoConfirm()) {
|
||||
return cfg.confirmView.apply(ra.clientId, ra.scopes);
|
||||
return SaOAuth2Strategy.instance.confirmView.apply(ra.clientId, ra.scopes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,9 +221,8 @@ public class SaOAuth2ServerProcessor {
|
||||
public Object doLogin() {
|
||||
// 获取变量
|
||||
SaRequest req = SaHolder.getRequest();
|
||||
SaOAuth2ServerConfig cfg = SaOAuth2Manager.getServerConfig();
|
||||
|
||||
return cfg.doLoginHandle.apply(req.getParam(Param.name), req.getParam(Param.pwd));
|
||||
return SaOAuth2Strategy.instance.doLoginHandle.apply(req.getParam(Param.name), req.getParam(Param.pwd));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+29
-3
@@ -24,6 +24,9 @@ import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
|
||||
import cn.dev33.satoken.oauth2.data.model.request.ClientIdAndSecretModel;
|
||||
import cn.dev33.satoken.oauth2.error.SaOAuth2ErrorCode;
|
||||
import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
|
||||
import cn.dev33.satoken.oauth2.function.SaOAuth2ConfirmViewFunction;
|
||||
import cn.dev33.satoken.oauth2.function.SaOAuth2DoLoginHandleFunction;
|
||||
import cn.dev33.satoken.oauth2.function.SaOAuth2NotLoginViewFunction;
|
||||
import cn.dev33.satoken.oauth2.function.strategy.*;
|
||||
import cn.dev33.satoken.oauth2.granttype.handler.AuthorizationCodeGrantTypeHandler;
|
||||
import cn.dev33.satoken.oauth2.granttype.handler.PasswordGrantTypeHandler;
|
||||
@@ -32,6 +35,7 @@ import cn.dev33.satoken.oauth2.granttype.handler.SaOAuth2GrantTypeHandlerInterfa
|
||||
import cn.dev33.satoken.oauth2.scope.CommonScope;
|
||||
import cn.dev33.satoken.oauth2.scope.handler.*;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -55,7 +59,8 @@ public final class SaOAuth2Strategy {
|
||||
*/
|
||||
public static final SaOAuth2Strategy instance = new SaOAuth2Strategy();
|
||||
|
||||
// 权限处理器
|
||||
|
||||
// ------------------ 权限处理器 ------------------
|
||||
|
||||
/**
|
||||
* 权限处理器集合
|
||||
@@ -141,7 +146,8 @@ public final class SaOAuth2Strategy {
|
||||
}
|
||||
};
|
||||
|
||||
// grant_type 处理器
|
||||
|
||||
// ------------------ grant_type 处理器 ------------------
|
||||
|
||||
/**
|
||||
* grant_type 处理器集合
|
||||
@@ -206,7 +212,7 @@ public final class SaOAuth2Strategy {
|
||||
};
|
||||
|
||||
|
||||
// ----------------------- 所有策略
|
||||
// ------------------ 凭证创建 ------------------
|
||||
|
||||
/**
|
||||
* 创建一个 code value
|
||||
@@ -236,4 +242,24 @@ public final class SaOAuth2Strategy {
|
||||
return SaFoxUtil.getRandomString(60);
|
||||
};
|
||||
|
||||
|
||||
// ------------------ 认证流程回调 ------------------
|
||||
|
||||
/**
|
||||
* OAuth-Server端:未登录时返回的View
|
||||
*/
|
||||
public SaOAuth2NotLoginViewFunction notLoginView = () -> "当前会话在 OAuth-Server 认证中心尚未登录";
|
||||
|
||||
/**
|
||||
* OAuth-Server端:确认授权时返回的View
|
||||
*/
|
||||
public SaOAuth2ConfirmViewFunction confirmView = (clientId, scopes) -> "本次操作需要用户授权";
|
||||
|
||||
/**
|
||||
* OAuth-Server端:登录函数
|
||||
*/
|
||||
public SaOAuth2DoLoginHandleFunction doLoginHandle = (name, pwd) -> SaResult.error();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user