From df55bab9a82ee8c97c0878cee64f3a02c5fb470e Mon Sep 17 00:00:00 2001 From: Github_lizhw <171332584@qq.com> Date: Sun, 3 Nov 2024 23:24:44 +0800 Subject: [PATCH] sso service --- .../main/java/com/pj/config/CorsConfig.java | 2 +- .../pj/controller/SsoServerController.java | 101 ++++++++++-------- .../src/main/resources/application.yml | 18 ++-- .../src/main/resources/static/sa-res/login.js | 1 + .../src/main/resources/templates/index.html | 20 ++++ 5 files changed, 88 insertions(+), 54 deletions(-) create mode 100644 ruoyi-modules/ruoyi-sso-server/src/main/resources/templates/index.html diff --git a/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/config/CorsConfig.java b/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/config/CorsConfig.java index e6789c5b8..3595b7e1a 100644 --- a/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/config/CorsConfig.java +++ b/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/config/CorsConfig.java @@ -38,7 +38,7 @@ public class CorsConfig implements WebMvcConfigurer { config.addAllowedMethod("*"); config.setAllowCredentials(true); config.setMaxAge(3600L); - source.registerCorsConfiguration("/**", config); + source.registerCorsConfiguration("/**", config);//TODO 配置跨域的请求路径,后续生产需要改 FilterRegistrationBean bean = new FilterRegistrationBean<>(new CorsFilter(source)); bean.setOrder(Ordered.HIGHEST_PRECEDENCE); return bean; diff --git a/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/controller/SsoServerController.java b/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/controller/SsoServerController.java index ae786e4a8..1b199d86d 100644 --- a/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/controller/SsoServerController.java +++ b/ruoyi-modules/ruoyi-sso-server/src/main/java/com/pj/controller/SsoServerController.java @@ -1,5 +1,7 @@ package com.pj.controller; +import cn.dev33.satoken.context.SaHolder; +import cn.dev33.satoken.context.model.SaRequest; import cn.dev33.satoken.sso.config.SaSsoServerConfig; import cn.dev33.satoken.sso.processor.SaSsoServerProcessor; import cn.dev33.satoken.stp.StpUtil; @@ -16,64 +18,69 @@ import org.springframework.web.servlet.ModelAndView; /** * Sa-Token-SSO Server端 Controller - * @author click33 * + * @author click33 */ @RestController public class SsoServerController { - /** - * SSO-Server端:处理所有SSO相关请求 - * http://{host}:{port}/sso/auth -- 单点登录授权地址,接受参数:redirect=授权重定向地址 - * http://{host}:{port}/sso/doLogin -- 账号密码登录接口,接受参数:name、pwd - * http://{host}:{port}/sso/checkTicket -- Ticket校验接口(isHttp=true时打开),接受参数:ticket=ticket码、ssoLogoutCall=单点注销回调地址 [可选] - * http://{host}:{port}/sso/signout -- 单点注销地址(isSlo=true时打开),接受参数:loginId=账号id、sign=参数签名 - */ - @RequestMapping("/sso/*") - public Object ssoRequest() { - return SaSsoServerProcessor.instance.dister(); - } + /** + * SSO-Server端:处理所有SSO相关请求 + * http://{host}:{port}/sso/auth -- 单点登录授权地址,接受参数:redirect=授权重定向地址 + * http://{host}:{port}/sso/doLogin -- 账号密码登录接口,接受参数:name、pwd + * http://{host}:{port}/sso/checkTicket -- Ticket校验接口(isHttp=true时打开),接受参数:ticket=ticket码、ssoLogoutCall=单点注销回调地址 [可选] + * http://{host}:{port}/sso/signout -- 单点注销地址(isSlo=true时打开),接受参数:loginId=账号id、sign=参数签名 + */ + @RequestMapping("/sso/*") + public Object ssoRequest() { + // 如果登录时没有提供redirect参数,则进入平台中心首页 /home,而不是重定向到 client 端 + SaRequest req = SaHolder.getRequest(); + if (req.isPath("/sso/auth") && !req.hasParam("redirect") && StpUtil.isLogin()) { + return new ModelAndView("index.html"); + } + return SaSsoServerProcessor.instance.dister(); + } - // 配置SSO相关参数 - @Autowired - private void configSso(SaSsoServerConfig ssoServer) { + // 配置SSO相关参数 + @Autowired + private void configSso(SaSsoServerConfig ssoServer) { - // 配置:未登录时返回的View - ssoServer.notLoginView = () -> { - return new ModelAndView("sa-login.html"); - }; + // 配置:未登录时返回的View + ssoServer.notLoginView = () -> { + return new ModelAndView("sa-login.html"); + }; - // 配置:登录处理函数 - ssoServer.doLoginHandle = (name, pwd) -> { - // 此处仅做模拟登录,真实环境应该查询数据进行登录 - if("sa".equals(name) && "123456".equals(pwd)) { - PasswordLoginBody body = new PasswordLoginBody(); - body.setUsername("sa"); - body.setPassword("123456"); - body.setClientId("ClientId"); - body.setGrantType("password"); - body.setTenantId("1"); - body.setCode("code1"); - body.setUuid("qweqw"); + // 配置:登录处理函数 + ssoServer.doLoginHandle = (name, pwd) -> { + // 此处仅做模拟登录,真实环境应该查询数据进行登录 + if ("sa".equals(name) && "123456".equals(pwd)) { + PasswordLoginBody body = new PasswordLoginBody(); + body.setUsername("sa"); + body.setPassword("123456"); + body.setClientId("ClientId"); + body.setGrantType("password"); + body.setTenantId("1"); + body.setCode("code1"); + body.setUuid("qweqw"); - SysClientVo client = new SysClientVo(); - client.setId(1L); - client.setClientId("ClientId1"); - client.setClientKey("setClientKey"); - client.setClientSecret("setClientSecret"); - client.setGrantType("password"); - client.setDeviceType("pc"); - client.setActiveTimeout(604800L); - client.setTimeout(604800L); - client.setStatus("0"); + SysClientVo client = new SysClientVo(); + client.setId(1L); + client.setClientId("ClientId1"); + client.setClientKey("setClientKey"); + client.setClientSecret("setClientSecret"); + client.setGrantType("password"); + client.setDeviceType("pc"); + client.setActiveTimeout(1800L); + client.setTimeout(300L); + client.setStatus("0"); - LoginVo loginVo = IAuthStrategy.login(JSONUtil.toJsonStr(body), client, body.getGrantType()); + LoginVo loginVo = IAuthStrategy.login(JSONUtil.toJsonStr(body), client, body.getGrantType()); - return SaResult.ok("登录成功!").setData(StpUtil.getTokenValue()); - } - return SaResult.error("登录失败!"); - }; + return SaResult.ok("登录成功!").setData(StpUtil.getTokenValue()); + } + return SaResult.error("登录失败!"); + }; - } + } } diff --git a/ruoyi-modules/ruoyi-sso-server/src/main/resources/application.yml b/ruoyi-modules/ruoyi-sso-server/src/main/resources/application.yml index 0d2a009ff..2d05ac855 100644 --- a/ruoyi-modules/ruoyi-sso-server/src/main/resources/application.yml +++ b/ruoyi-modules/ruoyi-sso-server/src/main/resources/application.yml @@ -1,6 +1,6 @@ # 端口 server: - port: 9000 + port: 19000 spring: cloud: @@ -40,23 +40,28 @@ spring: # Sa-Token配置 sa-token: + sign: + # API 接口签名秘钥 (随便乱摁几个字母即可) + secret-key: kQwIOrYbtXmSDkwEiFngrKidMcdrgKor + # 读写同源,开启后每次访问都会自动续期 # token名称 (同时也是cookie名称) token-name: Authorization - # cookie写入 + # cookie写入 TODO 后续必须关掉,cookie 有安全隐患 is-read-cookie: true # 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录) is-concurrent: true - # 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token) - is-share: false + # 在多人登录同一账号时,是否共用一个token (为true(默认)时所有登录共用一个token, 为false时每次登录新建一个token),这个为false会导致登陆多次后redis内存的token不能很好释放 + is-share: true # jwt秘钥 - jwt-secret-key: abcdefghijklmnopqrstuvwxyz + jwt-secret-key: abcdcosctlklmnopqrstuvwxyz # ------- SSO-模式二相关配置 sso-server: # Ticket有效期 (单位: 秒),默认五分钟 ticket-timeout: 300 - # 所有允许的授权回调地址 + # 所有允许的授权回调地址 TODO 生产和测试一定要改 allow-url: "*" + active-timeout: 30 # ruoyi-security配置 security: @@ -75,4 +80,5 @@ security: # swagger 文档配置 - /*/api-docs - /*/api-docs/** + - /sso/signout diff --git a/ruoyi-modules/ruoyi-sso-server/src/main/resources/static/sa-res/login.js b/ruoyi-modules/ruoyi-sso-server/src/main/resources/static/sa-res/login.js index c14744ef8..11da72c6a 100644 --- a/ruoyi-modules/ruoyi-sso-server/src/main/resources/static/sa-res/login.js +++ b/ruoyi-modules/ruoyi-sso-server/src/main/resources/static/sa-res/login.js @@ -2,6 +2,7 @@ var sa = {}; function setCookie(cname, cvalue, exdays) { + //TODO 这里需要先清理旧token,可以用jscookie包,然后sso登录还有点问题,sso的首页访问不了 var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toGMTString(); diff --git a/ruoyi-modules/ruoyi-sso-server/src/main/resources/templates/index.html b/ruoyi-modules/ruoyi-sso-server/src/main/resources/templates/index.html new file mode 100644 index 000000000..95f13b171 --- /dev/null +++ b/ruoyi-modules/ruoyi-sso-server/src/main/resources/templates/index.html @@ -0,0 +1,20 @@ + + + + 用户中心 + + + + + + +
+

在这里判断登录状态和获取用户信息

+
+ + + + + + +