mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-06-28 04:35:16 +08:00
feat(oauth2): 对 OAuth2 Password 认证模式需要重写处理器添加强提醒
This commit is contained in:
parent
2523d4b8df
commit
c4e34704d5
@ -0,0 +1,27 @@
|
||||
//package com.pj.oauth2.custom;
|
||||
//
|
||||
//import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
|
||||
//import cn.dev33.satoken.oauth2.granttype.handler.PasswordGrantTypeHandler;
|
||||
//import cn.dev33.satoken.oauth2.granttype.handler.model.PasswordAuthResult;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
///**
|
||||
// * 自定义 Password Grant_Type 授权模式处理器认证过程
|
||||
// *
|
||||
// * @author click33
|
||||
// * @since 2025/5/11
|
||||
// */
|
||||
//@Component
|
||||
//public class CustomPasswordGrantTypeHandler extends PasswordGrantTypeHandler {
|
||||
//
|
||||
// @Override
|
||||
// public PasswordAuthResult loginByUsernamePassword(String username, String password) {
|
||||
// if("sa".equals(username) && "123456".equals(password)) {
|
||||
// long userId = 10001;
|
||||
// return new PasswordAuthResult(userId);
|
||||
// } else {
|
||||
// throw new SaOAuth2Exception("无效账号密码");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
@ -8,6 +8,7 @@ import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts;
|
||||
import cn.dev33.satoken.oauth2.data.generate.SaOAuth2DataGenerate;
|
||||
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
|
||||
import cn.dev33.satoken.oauth2.data.model.CodeModel;
|
||||
import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
|
||||
import cn.dev33.satoken.oauth2.data.model.request.RequestAuthModel;
|
||||
import cn.dev33.satoken.oauth2.error.SaOAuth2ErrorCode;
|
||||
import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
|
||||
@ -63,8 +64,11 @@ public class SaOAuth2ServerH5Controller {
|
||||
// 6、判断:如果此次申请的Scope,该用户尚未授权,则转到授权页面
|
||||
boolean isNeedCarefulConfirm = oauth2Template.isNeedCarefulConfirm(ra.loginId, ra.clientId, ra.scopes);
|
||||
if(isNeedCarefulConfirm) {
|
||||
// code=411,需要用户手动确认授权
|
||||
return SaResult.get(411, "need confirm", null);
|
||||
SaClientModel cm = oauth2Template.checkClientModel(ra.clientId);
|
||||
if( ! cm.getIsAutoConfirm()) {
|
||||
// code=411,需要用户手动确认授权
|
||||
return SaResult.get(411, "need confirm", null);
|
||||
}
|
||||
}
|
||||
|
||||
// 7、判断授权类型,重定向到不同地址
|
||||
|
@ -31,11 +31,11 @@ OAuth2.0 与 SSO 相比,增加了对应用授权范围的控制,减弱了应
|
||||
### OAuth2.0 第三方开放平台完整开发流程参考
|
||||
|
||||
1. oauth2-server 平台端
|
||||
1. 搭建 oauth2-server 数据后台管理端(后台人员对底层数据增删改查维护的地方)。
|
||||
2. 搭建 oauth2-server 数据前台申请端(给第三方公司提供一个申请注册 client 的地方)。
|
||||
3. 搭建 oauth2-server 授权端 以及其接口文档(让第三方公司拿到 access_token)。
|
||||
4. 搭建 oauth2-server 资源端 以及其接口文档(让第三方公司通过 access_token 拿到对应的资源数据)。
|
||||
5. 以上四端可以为一个项目,也可以为四个独立的项目。
|
||||
1. 搭建 oauth2-server 数据后台管理端,也称:后台管理。(后台人员对底层数据增删改查维护的地方)。
|
||||
2. 搭建 oauth2-server 数据前台申请端,也称:开放平台。(给第三方公司提供一个申请注册 client 的地方)
|
||||
3. 搭建 oauth2-server 授权端 以及其接口文档,也称:认证中心。(让第三方公司拿到 access_token)
|
||||
4. 搭建 oauth2-server 资源端 以及其接口文档,也称:资源中心。(让第三方公司通过 access_token 拿到对应的资源数据)
|
||||
5. 以上四端可以是一个项目,也可以是四个独立的项目,也可以是一个后端 + 多个前端的形式。
|
||||
|
||||
2. oauth2-client 第三方公司端
|
||||
1. 第三方公司登录 oauth-server 数据前台申请端,申请注册应用,拿到 `clientId`、`clientSecret` 等数据。
|
||||
|
@ -104,6 +104,9 @@ public interface SaOAuth2ErrorCode {
|
||||
/** 无效的请求 Method */
|
||||
int CODE_30151 = 30151;
|
||||
|
||||
/** Password 模式认证失败 */
|
||||
int CODE_30161 = 30161;
|
||||
|
||||
/** 其它异常 */
|
||||
int CODE_30191 = 30191;
|
||||
|
||||
|
@ -21,7 +21,9 @@ import cn.dev33.satoken.oauth2.consts.GrantType;
|
||||
import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts;
|
||||
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
|
||||
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.stp.StpUtil;
|
||||
|
||||
import java.util.List;
|
||||
@ -47,10 +49,10 @@ public class PasswordGrantTypeHandler implements SaOAuth2GrantTypeHandlerInterfa
|
||||
String password = req.getParamNotNull(SaOAuth2Consts.Param.password);
|
||||
|
||||
// 3、调用API 开始登录,如果没能成功登录,则直接退出
|
||||
loginByUsernamePassword(username, password);
|
||||
Object loginId = StpUtil.getLoginIdDefaultNull();
|
||||
PasswordAuthResult passwordAuthResult = loginByUsernamePassword(username, password);
|
||||
Object loginId = passwordAuthResult.getLoginId();
|
||||
if(loginId == null) {
|
||||
throw new SaOAuth2Exception("登录失败");
|
||||
throw new SaOAuth2Exception("登录失败").setCode(SaOAuth2ErrorCode.CODE_30161);
|
||||
}
|
||||
|
||||
// 4、构建 ra 对象
|
||||
@ -65,12 +67,15 @@ public class PasswordGrantTypeHandler implements SaOAuth2GrantTypeHandlerInterfa
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 username、password 进行登录,如果登录失败请直接抛出异常
|
||||
* 根据 username、password 进行登录,如果登录失败请直接抛出异常或返回 loginId = null
|
||||
* @param username /
|
||||
* @param password /
|
||||
*/
|
||||
public void loginByUsernamePassword(String username, String password) {
|
||||
public PasswordAuthResult loginByUsernamePassword(String username, String password) {
|
||||
System.err.println("当前暂未重写 PasswordGrantTypeHandler 处理器,将使用默认实现,仅供开发测试");
|
||||
SaOAuth2Manager.getServerConfig().doLoginHandle.apply(username, password);
|
||||
Object loginId = StpUtil.getLoginIdDefaultNull();
|
||||
return new PasswordAuthResult(loginId);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.granttype.handler.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Model: Password Grant_Type 认证结果
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.43.0
|
||||
*/
|
||||
public class PasswordAuthResult implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -6541180061782004705L;
|
||||
|
||||
/**
|
||||
* 对应账号id
|
||||
*/
|
||||
public Object loginId;
|
||||
|
||||
/**
|
||||
* 构建一个
|
||||
*/
|
||||
public PasswordAuthResult() {
|
||||
|
||||
}
|
||||
/**
|
||||
* 构建一个
|
||||
* @param loginId 对应的账号id
|
||||
*/
|
||||
public PasswordAuthResult(Object loginId) {
|
||||
this();
|
||||
this.loginId = loginId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 对应账号id
|
||||
* @return /
|
||||
*/
|
||||
public Object getLoginId() {
|
||||
return loginId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 对应账号id
|
||||
* @param loginId 对应账号id
|
||||
* @return 对象自身
|
||||
*/
|
||||
public PasswordAuthResult setLoginId(Object loginId) {
|
||||
this.loginId = loginId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PasswordAuthResult{" +
|
||||
", loginId=" + loginId +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
@ -173,7 +173,7 @@ public final class SaOAuth2Strategy {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 scope 信息对一个 AccessTokenModel 进行加工处理
|
||||
* 根据 grantType 构造一个 AccessTokenModel
|
||||
*/
|
||||
public SaOAuth2GrantTypeAuthFunction grantTypeAuth = (req) -> {
|
||||
String grantType = req.getParamNotNull(SaOAuth2Consts.Param.grant_type);
|
||||
|
Loading…
Reference in New Issue
Block a user