mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-06-28 04:35:16 +08:00
fefactor(oauth2): 将认证流程回调从 SaOAuth2ServerConfig 转移到 SaOAuth2Strategy
This commit is contained in:
parent
c4e34704d5
commit
9f52c4d399
@ -3,6 +3,7 @@ package com.pj.oauth2;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.oauth2.config.SaOAuth2ServerConfig;
|
||||
import cn.dev33.satoken.oauth2.processor.SaOAuth2ServerProcessor;
|
||||
import cn.dev33.satoken.oauth2.strategy.SaOAuth2Strategy;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -32,12 +33,12 @@ public class SaOAuth2ServerController {
|
||||
@Autowired
|
||||
public void configOAuth2Server(SaOAuth2ServerConfig oauth2Server) {
|
||||
// 未登录的视图
|
||||
oauth2Server.notLoginView = ()->{
|
||||
SaOAuth2Strategy.instance.notLoginView = ()->{
|
||||
return new ModelAndView("login.html");
|
||||
};
|
||||
|
||||
// 登录处理函数
|
||||
oauth2Server.doLoginHandle = (name, pwd) -> {
|
||||
SaOAuth2Strategy.instance.doLoginHandle = (name, pwd) -> {
|
||||
if("sa".equals(name) && "123456".equals(pwd)) {
|
||||
StpUtil.login(10001);
|
||||
return SaResult.ok().set("satoken", StpUtil.getTokenValue());
|
||||
@ -46,7 +47,7 @@ public class SaOAuth2ServerController {
|
||||
};
|
||||
|
||||
// 授权确认视图
|
||||
oauth2Server.confirmView = (clientId, scopes)->{
|
||||
SaOAuth2Strategy.instance.confirmView = (clientId, scopes)->{
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("clientId", clientId);
|
||||
map.put("scope", scopes);
|
||||
|
@ -12,7 +12,7 @@
|
||||
@Autowired
|
||||
public void configOAuth2Server(SaOAuth2ServerConfig oauth2Server) {
|
||||
// 配置:未登录时返回的View
|
||||
oauth2Server.notLoginView = ()->{
|
||||
SaOAuth2Strategy.instance.notLoginView = ()->{
|
||||
return new ModelAndView("xxx.html");
|
||||
};
|
||||
}
|
||||
@ -66,7 +66,7 @@ public SaResult ss(String name, String pwd) {
|
||||
@Autowired
|
||||
public void configOAuth2Server(SaOAuth2ServerConfig oauth2Server) {
|
||||
// 配置:授权确认视图
|
||||
oauth2Server.confirmView = (clientId, scopes)->{
|
||||
SaOAuth2Strategy.instance.confirmView = (clientId, scopes)->{
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("clientId", clientId);
|
||||
map.put("scope", scopes);
|
||||
|
@ -143,7 +143,7 @@ public class SaOAuth2ServerController {
|
||||
// oauth2Server.addClient(...)
|
||||
|
||||
// 配置:未登录时返回的View
|
||||
oauth2Server.notLoginView = () -> {
|
||||
SaOAuth2Strategy.instance.notLoginView = () -> {
|
||||
String msg = "当前会话在OAuth-Server端尚未登录,请先访问"
|
||||
+ "<a href='/oauth2/doLogin?name=sa&pwd=123456' target='_blank'> doLogin登录 </a>"
|
||||
+ "进行登录之后,刷新页面开始授权";
|
||||
@ -151,7 +151,7 @@ public class SaOAuth2ServerController {
|
||||
};
|
||||
|
||||
// 配置:登录处理函数
|
||||
oauth2Server.doLoginHandle = (name, pwd) -> {
|
||||
SaOAuth2Strategy.instance.doLoginHandle = (name, pwd) -> {
|
||||
if("sa".equals(name) && "123456".equals(pwd)) {
|
||||
StpUtil.login(10001);
|
||||
return SaResult.ok();
|
||||
@ -160,7 +160,7 @@ public class SaOAuth2ServerController {
|
||||
};
|
||||
|
||||
// 配置:确认授权时返回的 view
|
||||
oauth2Server.confirmView = (clientId, scopes) -> {
|
||||
SaOAuth2Strategy.instance.confirmView = (clientId, scopes) -> {
|
||||
String scopeStr = SaFoxUtil.convertListToString(scopes);
|
||||
String yesCode =
|
||||
"fetch('/oauth2/doConfirm?client_id=" + clientId + "&scope=" + scopeStr + "', {method: 'POST'})" +
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user