access_token 读取兼容 Bearer Token 方式

This commit is contained in:
click33
2024-08-24 03:46:03 +08:00
parent a7a3e8c14f
commit 2d13e908b1
6 changed files with 46 additions and 21 deletions

View File

@@ -58,6 +58,7 @@ public class SaOAuth2Consts {
public static String name = "name";
public static String pwd = "pwd";
public static String build_redirect_uri = "build_redirect_uri";
public static String Authorization = "Authorization";
}
/**

View File

@@ -18,8 +18,8 @@ package cn.dev33.satoken.oauth2.data.resolver;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.ClientTokenModel;
import cn.dev33.satoken.oauth2.data.model.request.RequestAuthModel;
import cn.dev33.satoken.oauth2.data.model.request.ClientIdAndSecretModel;
import cn.dev33.satoken.oauth2.data.model.request.RequestAuthModel;
import cn.dev33.satoken.util.SaResult;
import java.util.Map;
@@ -42,6 +42,14 @@ public interface SaOAuth2DataResolver {
*/
ClientIdAndSecretModel readClientIdAndSecret(SaRequest request);
/**
* 数据读取:从请求对象中读取 AccessToken
*
* @param request /
* @return /
*/
String readAccessToken(SaRequest request);
/**
* 数据读取:从请求对象中构建 RequestAuthModel
* @param req SaRequest对象
@@ -75,21 +83,10 @@ public interface SaOAuth2DataResolver {
return SaResult.ok();
}
/**
* 构建返回值: password 模式认证 获取 token
* @param at token信息
* @return /
*/
default Map<String, Object> buildPasswordReturnValue(AccessTokenModel at) {
return buildTokenReturnValue(at);
}
/**
* 构建返回值: 凭证式 模式认证 获取 token
* @param ct token信息
*/
Map<String, Object> buildClientTokenReturnValue(ClientTokenModel ct);
}

View File

@@ -22,8 +22,8 @@ import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts;
import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts.TokenType;
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.ClientTokenModel;
import cn.dev33.satoken.oauth2.data.model.request.RequestAuthModel;
import cn.dev33.satoken.oauth2.data.model.request.ClientIdAndSecretModel;
import cn.dev33.satoken.oauth2.data.model.request.RequestAuthModel;
import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
@@ -56,7 +56,7 @@ public class SaOAuth2DataResolverDefaultImpl implements SaOAuth2DataResolver {
return new ClientIdAndSecretModel(clientId, clientSecret);
}
// 如果请求参数中没有提供 client_id 参数,则尝试从 base auth 中获取
// 如果请求参数中没有提供 client_id 参数,则尝试从 Authorization 中获取
String authorizationValue = SaHttpBasicUtil.getAuthorizationValue();
if(SaFoxUtil.isNotEmpty(authorizationValue)) {
String[] arr = authorizationValue.split(":");
@@ -71,6 +71,33 @@ public class SaOAuth2DataResolverDefaultImpl implements SaOAuth2DataResolver {
throw new SaOAuth2Exception("请提供 client 信息");
}
/**
* 数据读取:从请求对象中读取 AccessToken
*/
@Override
public String readAccessToken(SaRequest request) {
// 优先从请求参数中获取
String accessToken = request.getParam(SaOAuth2Consts.Param.access_token);
if(SaFoxUtil.isNotEmpty(accessToken)) {
return accessToken;
}
// 如果请求参数中没有提供 access_token 参数,则尝试从 Authorization 中获取
String authorizationValue = request.getHeader(SaOAuth2Consts.Param.Authorization);
if(SaFoxUtil.isEmpty(authorizationValue)) {
return null;
}
// 判断前缀,裁剪
String prefix = TokenType.Bearer + " ";
if(authorizationValue.startsWith(prefix)) {
return authorizationValue.substring(prefix.length());
}
// 前缀不符合,返回 null
return null;
}
/**
* 数据读取:从请求对象中构建 RequestAuthModel
*/