This commit is contained in:
click33 2024-10-30 11:43:54 +08:00
commit adfdacbdf2
29 changed files with 138 additions and 185 deletions

View File

@ -20,6 +20,7 @@ import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.util.SaFoxUtil;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -95,7 +96,7 @@ public interface SaRequest {
* 获取 [ 请求体 ] 里提交的所有参数名称
* @return 参数名称列表
*/
List<String> getParamNames();
Collection<String> getParamNames();
/**
* 获取 [ 请求体 ] 里提交的所有参数

View File

@ -10,7 +10,7 @@
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.7.0</version>
<version>3.0.1</version>
<relativePath/>
</parent>

View File

@ -10,7 +10,7 @@
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.9.1</version>
<version>3.0.1</version>
<relativePath/>
</parent>

View File

@ -10,14 +10,13 @@
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.7.0</version>
<version>3.0.1</version>
<relativePath/>
</parent>
<!-- 定义 Sa-Token 版本号 -->
<properties>
<sa-token.version>1.39.0</sa-token.version>
<solon.version>2.7.0</solon.version>
</properties>
<dependencies>

View File

@ -10,7 +10,7 @@
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.7.0</version>
<version>3.0.1</version>
<relativePath/>
</parent>

View File

@ -10,7 +10,7 @@
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.7.0</version>
<version>3.0.1</version>
<relativePath/>
</parent>

View File

@ -10,7 +10,7 @@
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.7.0</version>
<version>3.0.1</version>
<relativePath/>
</parent>

View File

@ -23,7 +23,7 @@
<servlet-api.version>3.1.0</servlet-api.version>
<jakarta-servlet-api.version>6.0.0</jakarta-servlet-api.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<solon.version>2.7.0</solon.version>
<solon.version>3.0.1</solon.version>
<noear-redisx.version>1.6.2</noear-redisx.version>
<noear-snack3.version>3.2.88</noear-snack3.version>
<jfinal.version>4.9.17</jfinal.version>

View File

@ -18,6 +18,7 @@ package cn.dev33.satoken.context.dubbo.model;
import cn.dev33.satoken.context.model.SaRequest;
import org.apache.dubbo.rpc.RpcContext;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -64,7 +65,7 @@ public class SaRequestForDubbo implements SaRequest {
* @return 参数名称列表
*/
@Override
public List<String> getParamNames(){
public Collection<String> getParamNames(){
return null;
}

View File

@ -18,6 +18,7 @@ package cn.dev33.satoken.context.dubbo3.model;
import cn.dev33.satoken.context.model.SaRequest;
import org.apache.dubbo.rpc.RpcContext;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -64,7 +65,7 @@ public class SaRequestForDubbo3 implements SaRequest {
* @return 参数名称列表
*/
@Override
public List<String> getParamNames(){
public Collection<String> getParamNames(){
return null;
}

View File

@ -18,6 +18,7 @@ package cn.dev33.satoken.context.grpc.model;
import cn.dev33.satoken.context.grpc.context.SaTokenGrpcContext;
import cn.dev33.satoken.context.model.SaRequest;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -51,7 +52,7 @@ public class SaRequestForGrpc implements SaRequest {
* @return 参数名称列表
*/
@Override
public List<String> getParamNames(){
public Collection<String> getParamNames(){
return null;
}

View File

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

View File

@ -170,6 +170,17 @@ public interface SaOAuth2Dao {
}
}
/**
* 持久化nonce-索引
* @param c .
*/
default void saveCodeNonceIndex(CodeModel c) {
if(c == null || SaFoxUtil.isEmpty(c.nonce)) {
return;
}
getSaTokenDao().set(splicingCodeNonceIndexSaveKey(c.code), c.nonce, SaOAuth2Manager.getServerConfig().getCodeTimeout());
}
// ------------------- delete数据
@ -404,6 +415,18 @@ public interface SaOAuth2Dao {
return getSaTokenDao().get(splicingStateSaveKey(state));
}
/**
* 获取nonce
* @param code /
* @return /
*/
default String getNonce(String code) {
if(SaFoxUtil.isEmpty(code)) {
return null;
}
return getSaTokenDao().get(splicingCodeNonceIndexSaveKey(code));
}
// ------------------- 拼接key
@ -510,6 +533,15 @@ public interface SaOAuth2Dao {
return getSaTokenConfig().getTokenName() + ":oauth2:state:" + state;
}
/**
* 拼接keycode-nonce 索引 参数持久化
* @param code 授权码
* @return key
*/
default String splicingCodeNonceIndexSaveKey(String code) {
return getSaTokenConfig().getTokenName() + ":oauth2:code-nonce-index:" + code;
}
// -------- bean 对象代理

View File

@ -31,6 +31,7 @@ import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
import cn.dev33.satoken.oauth2.exception.SaOAuth2RefreshTokenException;
import cn.dev33.satoken.oauth2.strategy.SaOAuth2Strategy;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
import java.util.LinkedHashMap;
import java.util.List;
@ -58,12 +59,15 @@ public class SaOAuth2DataGenerateDefaultImpl implements SaOAuth2DataGenerate {
// 生成新Code
String codeValue = SaOAuth2Strategy.instance.createCodeValue.execute(ra.clientId, ra.loginId, ra.scopes);
CodeModel cm = new CodeModel(codeValue, ra.clientId, ra.scopes, ra.loginId, ra.redirectUri);
CodeModel cm = new CodeModel(codeValue, ra.clientId, ra.scopes, ra.loginId, ra.redirectUri, ra.getNonce());
// 保存新Code
dao.saveCode(cm);
dao.saveCodeIndex(cm);
// 保存code-nonce
dao.saveCodeNonceIndex(cm);
// 返回
return cm;
}

View File

@ -52,6 +52,11 @@ public class CodeModel implements Serializable {
* 重定向的地址
*/
public String redirectUri;
/**
* 随机数
*/
public String nonce;
/**
* 构建一个
@ -67,13 +72,14 @@ public class CodeModel implements Serializable {
* @param loginId 对应的账号id
* @param redirectUri 重定向地址
*/
public CodeModel(String code, String clientId, List<String> scopes, Object loginId, String redirectUri) {
public CodeModel(String code, String clientId, List<String> scopes, Object loginId, String redirectUri, String nonce) {
super();
this.code = code;
this.clientId = clientId;
this.scopes = scopes;
this.loginId = loginId;
this.redirectUri = redirectUri;
this.nonce = nonce;
}
public String getCode() {
@ -121,10 +127,19 @@ public class CodeModel implements Serializable {
return this;
}
public String getNonce() {
return nonce;
}
public CodeModel setNonce(String nonce) {
this.nonce = nonce;
return this;
}
@Override
public String toString() {
return "CodeModel [code=" + code + ", clientId=" + clientId + ", scopes=" + scopes + ", loginId=" + loginId
+ ", redirectUri=" + redirectUri + "]";
+ ", redirectUri=" + redirectUri + ", nonce=" + nonce + " ]";
}
}

View File

@ -62,6 +62,11 @@ public class RequestAuthModel implements Serializable {
*/
public String state;
/**
* 随机数
*/
public String nonce;
/**
* @return clientId
@ -158,7 +163,23 @@ public class RequestAuthModel implements Serializable {
this.state = state;
return this;
}
/**
* @return nonce
*/
public String getNonce() {
return nonce;
}
/**
* @param nonce 要设置的随机数
* @return 对象自身
*/
public RequestAuthModel setNonce(String nonce) {
this.nonce = nonce;
return this;
}
/**
* 检查此Model参数是否有效
* @return 对象自身

View File

@ -136,6 +136,7 @@ public class SaOAuth2DataResolverDefaultImpl implements SaOAuth2DataResolver {
ra.responseType = req.getParamNotNull(Param.response_type);
ra.redirectUri = req.getParamNotNull(Param.redirect_uri);
ra.state = req.getParam(Param.state);
ra.nonce = req.getParam(Param.nonce);
ra.scopes = SaOAuth2Manager.getDataConverter().convertScopeStringToList(req.getParam(Param.scope));
ra.loginId = loginId;
return ra;

View File

@ -22,6 +22,8 @@ import cn.dev33.satoken.jwt.SaJwtUtil;
import cn.dev33.satoken.jwt.error.SaJwtErrorCode;
import cn.dev33.satoken.jwt.exception.SaJwtException;
import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.consts.SaOAuth2Consts;
import cn.dev33.satoken.oauth2.dao.SaOAuth2Dao;
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.ClientTokenModel;
import cn.dev33.satoken.oauth2.data.model.oidc.IdTokenModel;
@ -104,7 +106,12 @@ public class OidcScopeHandler implements SaOAuth2ScopeHandlerInterface {
* @return /
*/
public String getNonce() {
String nonce = SaHolder.getRequest().getParam("nonce");
String nonce = SaHolder.getRequest().getParam(SaOAuth2Consts.Param.nonce);
if(SaFoxUtil.isEmpty(nonce)) {
//通过code查找nonce
//为了避免其它handler可能会用到nonce,任由其自然过期只取用不删除
nonce = SaOAuth2Manager.getDao().getNonce(SaHolder.getRequest().getParam(SaOAuth2Consts.Param.code));
}
if(SaFoxUtil.isEmpty(nonce)) {
nonce = SaFoxUtil.getRandomString(32);
}

View File

@ -71,13 +71,8 @@ public class SaRequestForServlet implements SaRequest {
* @return 参数名称列表
*/
@Override
public List<String> getParamNames(){
Enumeration<String> parameterNames = request.getParameterNames();
List<String> list = new ArrayList<>();
while (parameterNames.hasMoreElements()) {
list.add(parameterNames.nextElement());
}
return list;
public Collection<String> getParamNames(){
return Collections.list(request.getParameterNames());
}
/**

View File

@ -27,10 +27,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* SaRequest 包装类的实现Reactor 响应式编程版
@ -74,9 +71,8 @@ public class SaRequestForReactor implements SaRequest {
* @return 参数名称列表
*/
@Override
public List<String> getParamNames(){
Set<String> names = request.getQueryParams().keySet();
return new ArrayList<>(names);
public Collection<String> getParamNames(){
return request.getQueryParams().keySet();
}
/**

View File

@ -27,10 +27,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* SaRequest 包装类的实现Reactor 响应式编程版
@ -74,9 +71,8 @@ public class SaRequestForReactor implements SaRequest {
* @return 参数名称列表
*/
@Override
public List<String> getParamNames(){
Set<String> names = request.getQueryParams().keySet();
return new ArrayList<>(names);
public Collection<String> getParamNames(){
return request.getQueryParams().keySet();
}
/**

View File

@ -71,13 +71,8 @@ public class SaRequestForServlet implements SaRequest {
* @return 参数名称列表
*/
@Override
public List<String> getParamNames(){
Enumeration<String> parameterNames = request.getParameterNames();
List<String> list = new ArrayList<>();
while (parameterNames.hasMoreElements()) {
list.add(parameterNames.nextElement());
}
return list;
public Collection<String> getParamNames(){
return Collections.list(request.getParameterNames());
}
/**

View File

@ -51,21 +51,9 @@ public class XPluginImp implements Plugin {
@Override
public void start(AppContext context) {
// Sa-Token 日志输出 Bean
context.getBeanAsync(SaLog.class, bean -> {
SaManager.setLog(bean);
});
context.beanMake(SaSsoAutoConfigure.class);
context.beanMake(SaOAuth2AutoConfigure.class);
//注入其它 Bean
context.lifecycle(-99, () -> {
beanInitDo(context);
context.beanMake(SaSsoAutoConfigure.class);
context.beanMake(SaOAuth2AutoConfigure.class);
});
}
private void beanInitDo(AppContext context) {
// 注入上下文Bean
SaManager.setSaTokenContext(new SaContextForSolon());
@ -78,6 +66,12 @@ public class XPluginImp implements Plugin {
SaManager.setConfig(saTokenConfig);
}
// Sa-Token 日志输出 Bean
context.getBeanAsync(SaLog.class, bean -> {
SaManager.setLog(bean);
});
//注入 SaTokenConfig
context.getBeanAsync(SaTokenConfig.class, bean -> {
SaManager.setConfig(bean);
});
@ -146,6 +140,5 @@ public class XPluginImp implements Plugin {
context.getBeanAsync(StpLogic.class, bean -> {
StpUtil.setStpLogic(bean);
});
}
}

View File

@ -1,90 +0,0 @@
/*
* 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.solon.dao;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.util.SaFoxUtil;
import org.noear.snack.ONode;
// 不能删为保持与旧的序列化兼容
/**
* Snack3 定制版 SaSession重写类型转换API
*
* @author noear
* @since 1.12
*/
@Deprecated
public class SaSessionForJson extends SaSession {
private static final long serialVersionUID = -7600983549653130681L;
public SaSessionForJson() {
super();
}
/**
* 构建一个 SaSession 对象
* @param id Session id
*/
public SaSessionForJson(String id) {
super(id);
}
/**
* 取值 (指定转换类型)
* @param <T> 泛型
* @param key key
* @param cs 指定转换类型
* @return
*/
@Override
public <T> T getModel(String key, Class<T> cs) {
if(SaFoxUtil.isBasicType(cs)) {
return SaFoxUtil.getValueByType(get(key), cs);
}
return ONode.deserialize(getString(key), cs);
}
/**
* 取值 (指定转换类型, 并指定值为Null时返回的默认值)
* @param <T> 泛型
* @param key key
* @param cs 指定转换类型
* @param defaultValue 值为Null时返回的默认值
* @return
*/
@Override
@SuppressWarnings("unchecked")
public <T> T getModel(String key, Class<T> cs, Object defaultValue) {
Object value = get(key);
if(valueIsNull(value)) {
return (T)defaultValue;
}
if(SaFoxUtil.isBasicType(cs)) {
return SaFoxUtil.getValueByType(get(key), cs);
}
return ONode.deserialize(getString(key), cs);
}
/**
* 忽略 timeout 字段的序列化
*/
@Override
public long getTimeout() {
return super.getTimeout();
}
}

View File

@ -15,7 +15,6 @@
*/
package cn.dev33.satoken.solon.integration;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.exception.BackResultException;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.exception.StopMatchException;
@ -24,7 +23,6 @@ import cn.dev33.satoken.filter.SaFilterErrorStrategy;
import cn.dev33.satoken.filter.SaFilter;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
import cn.dev33.satoken.strategy.SaStrategy;
import org.noear.solon.Solon;
import org.noear.solon.core.handle.*;
import org.noear.solon.core.route.RoutingTable;

View File

@ -20,10 +20,7 @@ import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.util.SaFoxUtil;
import org.noear.solon.core.handle.Context;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* @author noear
@ -33,7 +30,7 @@ public class SaRequestForSolon implements SaRequest {
protected Context ctx;
public SaRequestForSolon(){
public SaRequestForSolon() {
ctx = Context.current();
}
@ -48,18 +45,18 @@ public class SaRequestForSolon implements SaRequest {
}
@Override
public List<String> getParamNames(){
Set<String> names = ctx.paramMap().keySet();
return new ArrayList<>(names);
public Collection<String> getParamNames() {
return ctx.paramNames();
}
/**
* 获取 [请求体] 里提交的所有参数
*
* @return 参数列表
*/
@Override
public Map<String, String> getParamMap(){
return ctx.paramMap();
public Map<String, String> getParamMap() {
return ctx.paramMap().toValueMap();
}
@Override
@ -76,31 +73,19 @@ public class SaRequestForSolon implements SaRequest {
* [ Cookie作用域 ] 里获取一个值 (第一个此名称的)
*/
@Override
public String getCookieFirstValue(String name){
public String getCookieFirstValue(String name) {
return ctx.cookie(name);
}
/**
* [ Cookie作用域 ] 里获取一个值 (最后一个此名称的)
*
* @param name
* @return
*/
@Override
public String getCookieLastValue(String name){
String value = null;
String cookieStr = ctx.header("Cookie");
if(SaFoxUtil.isNotEmpty(cookieStr)) {
String[] cookieItems = cookieStr.split(";");
for (String item : cookieItems) {
String[] kv = item.split("=");
if (kv.length == 2) {
if (kv[0].trim().equals(name)) {
value = kv[1].trim();
}
}
}
}
return value;
public String getCookieLastValue(String name) {
return ctx.cookieMap().holder(name).getLastValue();
}
@Override
@ -111,7 +96,7 @@ public class SaRequestForSolon implements SaRequest {
@Override
public String getUrl() {
String currDomain = SaManager.getConfig().getCurrDomain();
if( ! SaFoxUtil.isEmpty(currDomain)) {
if (!SaFoxUtil.isEmpty(currDomain)) {
return currDomain + this.getRequestPath();
}
return ctx.url();
@ -127,5 +112,4 @@ public class SaRequestForSolon implements SaRequest {
ctx.forward(path);
return null;
}
}

View File

@ -48,9 +48,10 @@ public class SaResponseForSolon implements SaResponse {
}
/**
* 在响应头里添加一个值
* @param name 名字
* @param value
* 在响应头里添加一个值
*
* @param name 名字
* @param value
* @return 对象自身
*/
public SaResponse addHeader(String name, String value) {

View File

@ -27,7 +27,6 @@ import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.bean.InitializingBean;
/**
* @author noear

View File

@ -40,13 +40,15 @@ public class Config {
@Bean
public void saTokenPathInterceptor2() {
Solon.app().before((ctx) -> {
Solon.app().routerInterceptor((ctx, mainHandler, chain) -> {
SaRouter.match("/**", StpUtil::checkLogin);
// 根据路由划分模块不同模块不同鉴权
SaRouter.match("/user/**", r -> StpUtil.checkPermission("user"));
SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin"));
SaRouter.match("/goods/**", r -> StpUtil.checkPermission("goods"));
SaRouter.match("/orders/**", r -> StpUtil.checkPermission("orders"));
chain.doIntercept(ctx, mainHandler);
});
}
}