sso service

This commit is contained in:
Github_lizhw 2024-11-03 23:24:44 +08:00
parent 5e2fd3a535
commit df55bab9a8
5 changed files with 88 additions and 54 deletions

View File

@ -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<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return bean;

View File

@ -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 -- 账号密码登录接口接受参数namepwd
* http://{host}:{port}/sso/checkTicket -- Ticket校验接口isHttp=true时打开接受参数ticket=ticket码ssoLogoutCall=单点注销回调地址 [可选]
* http://{host}:{port}/sso/signout -- 单点注销地址isSlo=true时打开接受参数loginId=账号idsign=参数签名
*/
@RequestMapping("/sso/*")
public Object ssoRequest() {
return SaSsoServerProcessor.instance.dister();
}
/**
* SSO-Server端处理所有SSO相关请求
* http://{host}:{port}/sso/auth -- 单点登录授权地址接受参数redirect=授权重定向地址
* http://{host}:{port}/sso/doLogin -- 账号密码登录接口接受参数namepwd
* http://{host}:{port}/sso/checkTicket -- Ticket校验接口isHttp=true时打开接受参数ticket=ticket码ssoLogoutCall=单点注销回调地址 [可选]
* http://{host}:{port}/sso/signout -- 单点注销地址isSlo=true时打开接受参数loginId=账号idsign=参数签名
*/
@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("登录失败!");
};
}
}
}

View File

@ -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

View File

@ -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();

View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<title>用户中心</title>
<meta charset="utf-8">
<base th:href="@{/}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="./sa-res/login.css">
</head>
<body>
<div class="view-box">
<p>在这里判断登录状态和获取用户信息</p>
</div>
<!-- scripts -->
<script src="./sa-res/jquery.min.js"></script>
<script src="./sa-res/layer/layer.js"></script>
<script src="./sa-res/login.js"></script>
</body>
</html>