SSO模块增加 server-url 属性,用于简化各种 url 配置

This commit is contained in:
click33 2022-08-15 09:14:28 +08:00
parent 4a3f4fa548
commit 5434ee8800
5 changed files with 103 additions and 13 deletions

View File

@ -178,12 +178,36 @@ public final class SaStrategy {
// 默认使用jdk的注解处理器
return element.getAnnotation(annotationClass);
};
/**
* 拼接两个url
* <p> 例如url1=http://domain.cnurl2=/sso/auth则返回http://domain.cn/sso/auth
* <p> 参数 [第一个url, 第二个url]
*/
public BiFunction<String, String, String> spliceTwoUrl = (url1, url2) -> {
// q1任意一个为空则直接返回另一个
if(url1 == null) {
return url2;
}
if(url2 == null) {
return url1;
}
// q2如果 url2 http 开头将其视为一个完整地址
if(url2.startsWith("http")) {
return url2;
}
// q3将两个地址拼接在一起
return url1 + url2;
};
//
// 重写策略 set连缀风格
//
/**
* 重写创建 Token 的策略
* <p> 参数 [账号id, 账号类型]
@ -249,6 +273,17 @@ public final class SaStrategy {
this.getAnnotation = getAnnotation;
return this;
}
/**
* 拼接两个url
* <p> 例如url1=http://domain.cnurl2=/sso/auth则返回http://domain.cn/sso/auth
* <p> 参数 [第一个url, 第二个url]
*
* @param spliceTwoUrl 要设置的 spliceTwoUrl
*/
public void setSpliceTwoUrl(BiFunction<String, String, String> spliceTwoUrl) {
this.spliceTwoUrl = spliceTwoUrl;
}
}

View File

@ -18,8 +18,8 @@ public class SsoClientController {
// SSO-Client端首页
@RequestMapping("/")
public String index() {
String authUrl = SaSsoManager.getConfig().getAuthUrl();
String solUrl = SaSsoManager.getConfig().getSloUrl();
String authUrl = SaSsoManager.getConfig().splicingAuthUrl();
String solUrl = SaSsoManager.getConfig().splicingSloUrl();
String str = "<h2>Sa-Token SSO-Client 应用端</h2>" +
"<p>当前会话是否登录:" + StpUtil.isLogin() + "</p>" +
"<p><a href=\"javascript:location.href='" + authUrl + "?mode=simple&redirect=' + encodeURIComponent(location.href);\">登录</a> " +

View File

@ -7,6 +7,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.strategy.SaStrategy;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
@ -53,7 +54,7 @@ public class SaSsoConfig implements Serializable {
/**
* 配置 Server 端单点登录授权地址
*/
public String authUrl;
public String authUrl = "/sso/auth";
/**
* 是否打开单点注销功能
@ -73,26 +74,30 @@ public class SaSsoConfig implements Serializable {
/**
* 配置 Server 端的 ticket 校验地址
*/
public String checkTicketUrl;
public String checkTicketUrl = "/sso/checkTicket";
/**
* 配置 Server 端查询 userinfo 地址
*/
public String userinfoUrl;
public String userinfoUrl = "/sso/userinfo";
/**
* 配置 Server 端单点注销地址
*/
public String sloUrl;
public String sloUrl = "/sso/logout";
/**
* 配置当前 Client 端的单点注销回调URL 为空时自动获取
*/
public String ssoLogoutCall;
/**
* 配置 Server 端主机总地址拼接在 authUrlcheckTicketUrluserinfoUrlsloUrl 属性前面用以简化各种 url 配置
*/
public String serverUrl;
// ----------------- 其它
/**
* 接口调用时的时间戳允许的差距单位ms-1代表不校验差距
*/
@ -261,6 +266,22 @@ public class SaSsoConfig implements Serializable {
return this;
}
/**
* @return 配置的 Server 端主机总地址拼接在 authUrlcheckTicketUrluserinfoUrlsloUrl 属性前面用以简化各种 url 配置
*/
public String getServerUrl() {
return serverUrl;
}
/**
* @param serverUrl 配置 Server 端主机总地址拼接在 authUrlcheckTicketUrluserinfoUrlsloUrl 属性前面用以简化各种 url 配置
* @return 对象自身
*/
public SaSsoConfig setServerUrl(String serverUrl) {
this.serverUrl = serverUrl;
return this;
}
/**
* @return 接口调用时的时间戳允许的差距单位ms-1代表不校验差距
*/
@ -276,7 +297,7 @@ public class SaSsoConfig implements Serializable {
this.timestampDisparity = timestampDisparity;
return this;
}
@Override
public String toString() {
return "SaSsoConfig ["
@ -290,10 +311,42 @@ public class SaSsoConfig implements Serializable {
+ ", userinfoUrl=" + userinfoUrl
+ ", sloUrl=" + sloUrl
+ ", ssoLogoutCall=" + ssoLogoutCall
+ ", serverUrl=" + serverUrl
+ ", timestampDisparity=" + timestampDisparity
+ "]";
}
// 额外添加的一些函数
/**
* @return 获取拼接urlServer 端单点登录授权地址
*/
public String splicingAuthUrl() {
return SaStrategy.me.spliceTwoUrl.apply(getServerUrl(), getAuthUrl());
}
/**
* @return 获取拼接urlServer 端的 ticket 校验地址
*/
public String splicingCheckTicketUrl() {
return SaStrategy.me.spliceTwoUrl.apply(getServerUrl(), getCheckTicketUrl());
}
/**
* @return 获取拼接urlServer 端查询 userinfo 地址
*/
public String splicingUserinfoUrl() {
return SaStrategy.me.spliceTwoUrl.apply(getServerUrl(), getUserinfoUrl());
}
/**
* @return 获取拼接urlServer 端单点注销地址
*/
public String splicingSloUrl() {
return SaStrategy.me.spliceTwoUrl.apply(getServerUrl(), getSloUrl());
}
/**
* 以数组形式写入允许的授权回调地址
* @param url 所有集合
@ -305,7 +358,6 @@ public class SaSsoConfig implements Serializable {
}
// -------------------- SaSsoHandle 所有回调函数 --------------------

View File

@ -22,6 +22,9 @@ public class SaSsoConsts {
/** SSO-Server端校验ticket 获取账号id */
public static String ssoCheckTicket = "/sso/checkTicket";
/** SSO-Server端获取userinfo */
public static String ssoUserinfo = "/sso/userinfo";
/** SSO-Server端 (and Client端):单点注销地址 */
public static String ssoLogout = "/sso/logout";

View File

@ -253,7 +253,7 @@ public class SaSsoTemplate {
public String buildServerAuthUrl(String clientLoginUrl, String back) {
// 服务端认证地址
String serverUrl = SaSsoManager.getConfig().getAuthUrl();
String serverUrl = SaSsoManager.getConfig().splicingAuthUrl();
// 对back地址编码
back = (back == null ? "" : back);
@ -327,7 +327,7 @@ public class SaSsoTemplate {
* @return Server端 账号资料查询地址
*/
public String buildUserinfoUrl(Object loginId) {
String userinfoUrl = SaSsoManager.getConfig().getUserinfoUrl();
String userinfoUrl = SaSsoManager.getConfig().splicingUserinfoUrl();
return addSignParams(userinfoUrl, loginId);
}
@ -340,7 +340,7 @@ public class SaSsoTemplate {
*/
public String buildCheckTicketUrl(String ticket, String ssoLogoutCallUrl) {
// 裸地址
String url = SaSsoManager.getConfig().getCheckTicketUrl();
String url = SaSsoManager.getConfig().splicingCheckTicketUrl();
// 拼接ticket参数
url = SaFoxUtil.joinParam(url, ParamName.ticket, ticket);
@ -360,7 +360,7 @@ public class SaSsoTemplate {
* @return 单点注销URL
*/
public String buildSloUrl(Object loginId) {
String url = SaSsoManager.getConfig().getSloUrl();
String url = SaSsoManager.getConfig().splicingSloUrl();
return addSignParams(url, loginId);
}