sa-token-oauth2 新增自定义 grant_type 能力

This commit is contained in:
click33
2024-08-23 03:24:30 +08:00
parent 4aa4941598
commit 3345e3aaf9
22 changed files with 778 additions and 277 deletions
@@ -1,5 +1,6 @@
package com.pj.oauth2;
import cn.dev33.satoken.oauth2.consts.GrantType;
import cn.dev33.satoken.oauth2.data.loader.SaOAuth2DataLoader;
import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
import org.springframework.stereotype.Component;
@@ -22,10 +23,14 @@ public class SaOAuth2DataLoaderImpl implements SaOAuth2DataLoader {
.setClientSecret("aaaa-bbbb-cccc-dddd-eeee") // client 秘钥
.addAllowUrls("*") // 所有允许授权的 url
.addContractScopes("openid", "userid", "userinfo") // 所有签约的权限
.setEnableCode(true) // 是否开启授权码模式
.setEnableImplicit(true) // 是否开启隐式模式
.setEnablePassword(true) // 是否开启密码模式
.setEnableClient(true) // 是否开启客户端模式
.addAllowGrantTypes( // 所有允许的授权模式
GrantType.authorization_code, // 授权码式
GrantType.implicit, // 隐式式
GrantType.refresh_token, // 刷新令牌
GrantType.password, // 密码式
GrantType.client_credentials, // 客户端模式
"phone_code" // 自定义授权模式 手机号验证码登录
)
;
}
return null;
@@ -0,0 +1,56 @@
package com.pj.oauth2.custom;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.request.RequestAuthModel;
import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
import cn.dev33.satoken.oauth2.granttype.handler.SaOAuth2GrantTypeHandlerInterface;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 自定义 phone_code 授权模式处理器
*
* @author click33
* @since 2024/8/23
*/
@Component
public class PhoneCodeGrantTypeHandler implements SaOAuth2GrantTypeHandlerInterface {
@Override
public String getHandlerGrantType() {
return "phone_code";
}
@Override
public AccessTokenModel getAccessTokenModel(SaRequest req, String clientId, List<String> scopes) {
String phone = req.getParamNotNull("phone");
String code = req.getParamNotNull("code");
String realCode = SaManager.getSaTokenDao().get("phone_code:" + phone);
// 1、校验验证码是否正确
if(!code.equals(realCode)) {
throw new SaOAuth2Exception("验证码错误");
}
// 2、校验通过,删除验证码
SaManager.getSaTokenDao().delete("phone_code:" + phone);
// 3、登录
long userId = 10001; // 模拟 userId,真实项目应该根据手机号从数据库查询
// 4、构建 ra 对象
RequestAuthModel ra = new RequestAuthModel();
ra.clientId = clientId;
ra.loginId = userId;
ra.scopes = scopes;
// 5、生成 Access-Token
AccessTokenModel at = SaOAuth2Manager.getDataGenerate().generateAccessToken(ra, true);
return at;
}
}
@@ -0,0 +1,26 @@
package com.pj.oauth2.custom;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 自定义手机登录接口
*
* @author click33
* @since 2024/8/23
*/
@RestController
public class PhoneLoginController {
@RequestMapping("/oauth2/sendPhoneCode")
public SaResult sendCode(String phone) {
String code = SaFoxUtil.getRandomNumber(100000, 999999) + "";
SaManager.getSaTokenDao().set("phone_code:" + phone, code, 60 * 5);
System.out.println("手机号:" + phone + ",验证码:" + code + ",已发送成功");
return SaResult.ok("验证码发送成功");
}
}
@@ -1,4 +1,4 @@
package com.pj.oauth2;
package com.pj.oauth2.custom;
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.ClientTokenModel;
@@ -5,6 +5,8 @@ server:
sa-token:
# token名称 (同时也是 Cookie 名称)
token-name: satoken-oauth2-server
# 是否打印操作日志
is-log: true
# OAuth2.0 配置
oauth2:
# 是否全局开启授权码模式