diff --git a/sa-token-demo/sa-token-demo-sso/sa-token-demo-sso-server/src/main/java/com/pj/SaSsoServerApplication.java b/sa-token-demo/sa-token-demo-sso/sa-token-demo-sso-server/src/main/java/com/pj/SaSsoServerApplication.java index 1d2c46d2..f8559239 100644 --- a/sa-token-demo/sa-token-demo-sso/sa-token-demo-sso-server/src/main/java/com/pj/SaSsoServerApplication.java +++ b/sa-token-demo/sa-token-demo-sso/sa-token-demo-sso-server/src/main/java/com/pj/SaSsoServerApplication.java @@ -13,6 +13,8 @@ public class SaSsoServerApplication { System.out.println(); System.out.println("---------------------- Sa-Token SSO 统一认证中心启动成功 ----------------------"); System.out.println("配置信息:" + SaSsoManager.getServerConfig()); + System.out.println("统一认证登录地址:http://sa-sso-server.com:9000/sso/auth"); + System.out.println("测试前需要根据官网文档修改hosts文件,测试账号密码:sa / 123456"); System.out.println(); } diff --git a/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/config/SaSsoServerConfig.java b/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/config/SaSsoServerConfig.java index a0855c57..2ba6a8a3 100644 --- a/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/config/SaSsoServerConfig.java +++ b/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/config/SaSsoServerConfig.java @@ -54,6 +54,11 @@ public class SaSsoServerConfig implements Serializable { */ public String allowUrl = "*"; + /** + * 主页路由:在 /sso/auth 登录后不指定 redirect 参数的情况下默认跳转的路由 + */ + public String homeRoute; + /** * 是否打开单点注销功能 */ @@ -127,6 +132,22 @@ public class SaSsoServerConfig implements Serializable { return this; } + /** + * @return 主页路由:在 /sso/auth 登录后不指定 redirect 参数的情况下默认跳转的路由 + */ + public String getHomeRoute() { + return homeRoute; + } + + /** + * @param homeRoute 主页路由:在 /sso/auth 登录后不指定 redirect 参数的情况下默认跳转的路由 + * @return 对象自身 + */ + public SaSsoServerConfig setHomeRoute(String homeRoute) { + this.homeRoute = homeRoute; + return this; + } + /** * @return 是否打开单点注销功能 */ @@ -210,6 +231,7 @@ public class SaSsoServerConfig implements Serializable { + "mode=" + mode + ", ticketTimeout=" + ticketTimeout + ", allowUrl=" + allowUrl + + ", homeRoute=" + homeRoute + ", isSlo=" + isSlo + ", isHttp=" + isHttp + ", maxRegClient=" + maxRegClient diff --git a/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/error/SaSsoErrorCode.java b/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/error/SaSsoErrorCode.java index c27bfdff..9d0c6b16 100644 --- a/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/error/SaSsoErrorCode.java +++ b/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/error/SaSsoErrorCode.java @@ -62,4 +62,7 @@ public interface SaSsoErrorCode { /** 提供的 client 参数值无效 */ int CODE_30013 = 30013; + /** 在 /sso/auth 既没有指定 redirect 参数,也没有配置 homeRoute 路由 */ + int CODE_30014 = 30014; + } diff --git a/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/processor/SaSsoServerProcessor.java b/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/processor/SaSsoServerProcessor.java index c99e5f5e..e6d93561 100644 --- a/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/processor/SaSsoServerProcessor.java +++ b/sa-token-plugin/sa-token-sso/src/main/java/cn/dev33/satoken/sso/processor/SaSsoServerProcessor.java @@ -106,10 +106,18 @@ public class SaSsoServerProcessor { } // ---- 情况2:在SSO认证中心已经登录,需要重定向回 Client 端,而这又分为两种方式: String mode = req.getParam(paramName.mode, ""); + String redirect = req.getParam(paramName.redirect); // 方式1:直接重定向回Client端 (mode=simple) if(mode.equals(SaSsoConsts.MODE_SIMPLE)) { - String redirect = req.getParam(paramName.redirect); + + // 若 redirect 为空,则选择 homeRoute,若 homeRoute 也为空,则抛出异常 + if(SaFoxUtil.isEmpty(redirect)) { + if(SaFoxUtil.isEmpty(cfg.getHomeRoute())) { + throw new SaSsoException("未指定 redirect 参数,也未配置 homeRoute 路由,无法完成重定向操作").setCode(SaSsoErrorCode.CODE_30014); + } + return res.redirect(cfg.getHomeRoute()); + } ssoServerTemplate.checkRedirectUrl(redirect); return res.redirect(redirect); } else { @@ -121,9 +129,16 @@ public class SaSsoServerProcessor { throw new SaSsoException("无效 client 标识:" + client).setCode(SaSsoErrorCode.CODE_30013); } - // 开始重定向 - String redirectUrl = ssoServerTemplate.buildRedirectUrl( - stpLogic.getLoginId(), client, req.getParam(paramName.redirect)); + // 若 redirect 为空,则选择 homeRoute,若 homeRoute 也为空,则抛出异常 + if(SaFoxUtil.isEmpty(redirect)) { + if(SaFoxUtil.isEmpty(cfg.getHomeRoute())) { + throw new SaSsoException("未指定 redirect 参数,也未配置 homeRoute 路由,无法完成重定向操作").setCode(SaSsoErrorCode.CODE_30014); + } + return res.redirect(cfg.getHomeRoute()); + } + + // 构建并跳转 + String redirectUrl = ssoServerTemplate.buildRedirectUrl(stpLogic.getLoginId(), client, redirect); return res.redirect(redirectUrl); } }