Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ default String getHeader(String name, String defaultValue) {
*/
String getRequestPath();

/**
* 返回当前请求path (包括上下文名称)
* @return /
*/
String getRequestUri();

/**
* 返回当前请求 path 是否为指定值
* @param path path
Expand Down
14 changes: 5 additions & 9 deletions sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,8 @@ protected String distUsableToken(Object id, SaLoginModel loginModel) {
return SaStrategy.instance.generateUniqueToken.execute(
"token",
getConfigOfMaxTryTimes(),
() -> {
return createTokenValue(id, loginModel.getDeviceOrDefault(), loginModel.getTimeout(), loginModel.getExtraData());
},
tokenValue -> {
return getLoginIdNotHandle(tokenValue) == null;
}
() -> createTokenValue(id, loginModel.getDeviceOrDefault(), loginModel.getTimeout(), loginModel.getExtraData()),
tokenValue -> getLoginIdNotHandle(tokenValue) == null
);
}

Expand Down Expand Up @@ -578,7 +574,7 @@ protected void checkLoginArgs(Object id, SaLoginModel loginModel) {
if( ! isSupportExtra()) {
// 如果不支持,开发者却传入了 extra 扩展参数,那么就打印警告信息
Map<String, Object> extraData = loginModel.getExtraData();
if(extraData != null && extraData.size() > 0) {
if(extraData != null && !extraData.isEmpty()) {
SaManager.log.warn("当前 StpLogic 不支持 extra 扩展参数模式,传入的 extra 参数将被忽略");
}
}
Expand Down Expand Up @@ -912,7 +908,7 @@ public boolean isLogin() {
*/
public boolean isLogin(Object loginId) {
// 判断条件:能否根据 loginId 查询到对应的 tokenSign 值
return getTokenSignListByLoginId(loginId, null).size() > 0;
return !getTokenSignListByLoginId(loginId, null).isEmpty();
}

/**
Expand Down Expand Up @@ -2020,7 +2016,7 @@ public String getTokenValueByLoginId(Object loginId) {
*/
public String getTokenValueByLoginId(Object loginId, String device) {
List<String> tokenValueList = getTokenValueListByLoginId(loginId, device);
return tokenValueList.size() == 0 ? null : tokenValueList.get(tokenValueList.size() - 1);
return tokenValueList.isEmpty() ? null : tokenValueList.get(tokenValueList.size() - 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public String getRequestPath() {
return null;
}

/**
* 不返回
*/
@Override
public String getRequestUri() {
return null;
}

/**
* 返回当前请求的url,例:http://xxx.com/test
* @return see note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public String getRequestPath() {
return null;
}

/**
* 不返回
*/
@Override
public String getRequestUri() {
return null;
}

/**
* 返回当前请求的url,例:http://xxx.com/test
* @return see note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public String getRequestPath() {
return null;
}

/**
* 不返回
*/
@Override
public String getRequestUri() {
return null;
}

/**
* 返回当前请求的url,例:http://xxx.com/test
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import cn.dev33.satoken.oauth2.model.RequestAuthModel;
import cn.dev33.satoken.oauth2.model.SaClientModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;

/**
Expand All @@ -53,63 +54,63 @@ public static Object serverRequest() {
SaResponse res = SaHolder.getResponse();
SaOAuth2Config cfg = SaOAuth2Manager.getConfig();

SaClientModel cm = currClientModel();
String prefixUrl = cm.prefixUrl;
String reqPath = SaFoxUtil.isNotEmpty(prefixUrl) ? req.getRequestUri().replaceAll(prefixUrl, "") : req.getRequestUri();

// ------------------ 路由分发 ------------------

// 模式一:Code授权码
if(req.isPath(Api.authorize) && req.isParam(Param.response_type, ResponseType.code)) {
SaClientModel cm = currClientModel();
if(reqPath.equals(Api.authorize) && req.isParam(Param.response_type, ResponseType.code)) {
if(cfg.getIsCode() && (cm.isCode || cm.isAutoMode)) {
return authorize(req, res, cfg);
}
throw new SaOAuth2Exception("暂未开放的授权模式").setCode(SaOAuth2ErrorCode.CODE_30131);
}

// Code授权码 获取 Access-Token
if(req.isPath(Api.token) && req.isParam(Param.grant_type, GrantType.authorization_code)) {
if(reqPath.equals(Api.token) && req.isParam(Param.grant_type, GrantType.authorization_code)) {
return token(req, res, cfg);
}

// Refresh-Token 刷新 Access-Token
if(req.isPath(Api.refresh) && req.isParam(Param.grant_type, GrantType.refresh_token)) {
if(reqPath.equals(Api.refresh) && req.isParam(Param.grant_type, GrantType.refresh_token)) {
return refreshToken(req);
}

// 回收 Access-Token
if(req.isPath(Api.revoke)) {
if(reqPath.equals(Api.revoke)) {
return revokeToken(req);
}

// doLogin 登录接口
if(req.isPath(Api.doLogin)) {
if(reqPath.equals(Api.doLogin)) {
return doLogin(req, res, cfg);
}

// doConfirm 确认授权接口
if(req.isPath(Api.doConfirm)) {
if(reqPath.equals(Api.doConfirm)) {
return doConfirm(req);
}

// 模式二:隐藏式
if(req.isPath(Api.authorize) && req.isParam(Param.response_type, ResponseType.token)) {
SaClientModel cm = currClientModel();
if(reqPath.equals(Api.authorize) && req.isParam(Param.response_type, ResponseType.token)) {
if(cfg.getIsImplicit() && (cm.isImplicit || cm.isAutoMode)) {
return authorize(req, res, cfg);
}
throw new SaOAuth2Exception("暂未开放的授权模式").setCode(SaOAuth2ErrorCode.CODE_30132);
}

// 模式三:密码式
if(req.isPath(Api.token) && req.isParam(Param.grant_type, GrantType.password)) {
SaClientModel cm = currClientModel();
if(reqPath.equals(Api.token) && req.isParam(Param.grant_type, GrantType.password)) {
if(cfg.getIsPassword() && (cm.isPassword || cm.isAutoMode)) {
return password(req, res, cfg);
}
throw new SaOAuth2Exception("暂未开放的授权模式").setCode(SaOAuth2ErrorCode.CODE_30133);
}

// 模式四:凭证式
if(req.isPath(Api.client_token) && req.isParam(Param.grant_type, GrantType.client_credentials)) {
SaClientModel cm = currClientModel();
if(reqPath.equals(Api.client_token) && req.isParam(Param.grant_type, GrantType.client_credentials)) {
if(cfg.getIsClient() && (cm.isClient || cm.isAutoMode)) {
return clientToken(req, res, cfg);
}
Expand Down Expand Up @@ -297,7 +298,8 @@ public static Object password(SaRequest req, SaResponse res, SaOAuth2Config cfg)
AccessTokenModel at = SaOAuth2Util.generateAccessToken(ra, true);

// 6、返回 Access-Token
return SaResult.data(at.toLineMap());
// return at.toLineMap();
return retObj;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public class SaClientModel implements Serializable {
/** 单独配置此Client:Past-Client-Token 保存的时间(单位:秒) [默认取全局配置] */
public long pastClientTokenTimeout;

/** 前置url */
public String prefixUrl;

public SaClientModel() {
SaOAuth2Config config = SaOAuth2Manager.getConfig();
Expand Down Expand Up @@ -325,17 +327,23 @@ public SaClientModel setPastClientTokenTimeout(long pastClientTokenTimeout) {
this.pastClientTokenTimeout = pastClientTokenTimeout;
return this;
}

//

/**
* @param prefixUrl 前置url
* @return 对象自身
*/
public SaClientModel setPrefixUrl(String prefixUrl) {
this.prefixUrl = prefixUrl;
return this;
}
//
@Override
public String toString() {
return "SaClientModel [clientId=" + clientId + ", clientSecret=" + clientSecret + ", contractScope="
+ contractScope + ", allowUrl=" + allowUrl + ", isCode=" + isCode + ", isImplicit=" + isImplicit
+ ", isPassword=" + isPassword + ", isClient=" + isClient + ", isAutoMode=" + isAutoMode
+ ", isNewRefresh=" + isNewRefresh + ", accessTokenTimeout=" + accessTokenTimeout
+ ", refreshTokenTimeout=" + refreshTokenTimeout + ", clientTokenTimeout=" + clientTokenTimeout
+ ", pastClientTokenTimeout=" + pastClientTokenTimeout + "]";
+ ", pastClientTokenTimeout=" + pastClientTokenTimeout + ", prefixUrl=" + prefixUrl + "]";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public String getRequestPath() {
return ApplicationInfo.cutPathPrefix(request.getRequestURI());
}

/**
* 返回当前请求path (包括上下文名称)
*/
@Override
public String getRequestUri() {
return request.getRequestURI();
}

/**
* 返回当前请求的url,例:http://xxx.com/test
* @return see note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ public String getRequestPath() {
return ApplicationInfo.cutPathPrefix(request.getPath().toString());
}


/**
* 不返回
*/
@Override
public String getRequestUri() {
return null;
}

/**
* 返回当前请求的url,例:http://xxx.com/test
* @return see note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ public String getRequestPath() {
return ApplicationInfo.cutPathPrefix(request.getPath().toString());
}

/**
* 不返回
*/
@Override
public String getRequestUri() {
return null;
}

/**
* 返回当前请求的url,例:http://xxx.com/test
* @return see note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public String getRequestPath() {
return ApplicationInfo.cutPathPrefix(request.getRequestURI());
}

/**
* 返回当前请求path (包括上下文名称)
*/
@Override
public String getRequestUri() {
return request.getRequestURI();
}

/**
* 返回当前请求的url,例:http://xxx.com/test
* @return see note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public String getRequestPath() {
return ctx.pathNew();
}

@Override
public String getRequestUri() {
return null;
}

@Override
public String getUrl() {
String currDomain = SaManager.getConfig().getCurrDomain();
Expand Down