From cf1f255a4ae797760e829ff9624f3595ddb4249f Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Fri, 3 May 2024 04:04:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20homeRoute=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9=EF=BC=9A=E5=9C=A8=20/sso/auth=20=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=90=8E=E4=B8=8D=E6=8C=87=E5=AE=9A=20redirect=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=9A=84=E6=83=85=E5=86=B5=E4=B8=8B=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E8=B7=B3=E8=BD=AC=E7=9A=84=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/pj/SaSsoServerApplication.java | 2 ++ .../satoken/sso/config/SaSsoServerConfig.java | 22 ++++++++++++++++++ .../satoken/sso/error/SaSsoErrorCode.java | 3 +++ .../sso/processor/SaSsoServerProcessor.java | 23 +++++++++++++++---- 4 files changed, 46 insertions(+), 4 deletions(-) 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); } }