mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-19 01:48:47 +08:00
sso service
This commit is contained in:
parent
5e2fd3a535
commit
df55bab9a8
@ -38,7 +38,7 @@ public class CorsConfig implements WebMvcConfigurer {
|
|||||||
config.addAllowedMethod("*");
|
config.addAllowedMethod("*");
|
||||||
config.setAllowCredentials(true);
|
config.setAllowCredentials(true);
|
||||||
config.setMaxAge(3600L);
|
config.setMaxAge(3600L);
|
||||||
source.registerCorsConfiguration("/**", config);
|
source.registerCorsConfiguration("/**", config);//TODO 配置跨域的请求路径,后续生产需要改
|
||||||
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
|
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
|
||||||
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||||
return bean;
|
return bean;
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package com.pj.controller;
|
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.config.SaSsoServerConfig;
|
||||||
import cn.dev33.satoken.sso.processor.SaSsoServerProcessor;
|
import cn.dev33.satoken.sso.processor.SaSsoServerProcessor;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
@ -16,64 +18,69 @@ import org.springframework.web.servlet.ModelAndView;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sa-Token-SSO Server端 Controller
|
* Sa-Token-SSO Server端 Controller
|
||||||
* @author click33
|
|
||||||
*
|
*
|
||||||
|
* @author click33
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
public class SsoServerController {
|
public class SsoServerController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SSO-Server端:处理所有SSO相关请求
|
* SSO-Server端:处理所有SSO相关请求
|
||||||
* http://{host}:{port}/sso/auth -- 单点登录授权地址,接受参数:redirect=授权重定向地址
|
* http://{host}:{port}/sso/auth -- 单点登录授权地址,接受参数:redirect=授权重定向地址
|
||||||
* http://{host}:{port}/sso/doLogin -- 账号密码登录接口,接受参数:name、pwd
|
* http://{host}:{port}/sso/doLogin -- 账号密码登录接口,接受参数:name、pwd
|
||||||
* http://{host}:{port}/sso/checkTicket -- Ticket校验接口(isHttp=true时打开),接受参数:ticket=ticket码、ssoLogoutCall=单点注销回调地址 [可选]
|
* http://{host}:{port}/sso/checkTicket -- Ticket校验接口(isHttp=true时打开),接受参数:ticket=ticket码、ssoLogoutCall=单点注销回调地址 [可选]
|
||||||
* http://{host}:{port}/sso/signout -- 单点注销地址(isSlo=true时打开),接受参数:loginId=账号id、sign=参数签名
|
* http://{host}:{port}/sso/signout -- 单点注销地址(isSlo=true时打开),接受参数:loginId=账号id、sign=参数签名
|
||||||
*/
|
*/
|
||||||
@RequestMapping("/sso/*")
|
@RequestMapping("/sso/*")
|
||||||
public Object ssoRequest() {
|
public Object ssoRequest() {
|
||||||
return SaSsoServerProcessor.instance.dister();
|
// 如果登录时没有提供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相关参数
|
// 配置SSO相关参数
|
||||||
@Autowired
|
@Autowired
|
||||||
private void configSso(SaSsoServerConfig ssoServer) {
|
private void configSso(SaSsoServerConfig ssoServer) {
|
||||||
|
|
||||||
// 配置:未登录时返回的View
|
// 配置:未登录时返回的View
|
||||||
ssoServer.notLoginView = () -> {
|
ssoServer.notLoginView = () -> {
|
||||||
return new ModelAndView("sa-login.html");
|
return new ModelAndView("sa-login.html");
|
||||||
};
|
};
|
||||||
|
|
||||||
// 配置:登录处理函数
|
// 配置:登录处理函数
|
||||||
ssoServer.doLoginHandle = (name, pwd) -> {
|
ssoServer.doLoginHandle = (name, pwd) -> {
|
||||||
// 此处仅做模拟登录,真实环境应该查询数据进行登录
|
// 此处仅做模拟登录,真实环境应该查询数据进行登录
|
||||||
if("sa".equals(name) && "123456".equals(pwd)) {
|
if ("sa".equals(name) && "123456".equals(pwd)) {
|
||||||
PasswordLoginBody body = new PasswordLoginBody();
|
PasswordLoginBody body = new PasswordLoginBody();
|
||||||
body.setUsername("sa");
|
body.setUsername("sa");
|
||||||
body.setPassword("123456");
|
body.setPassword("123456");
|
||||||
body.setClientId("ClientId");
|
body.setClientId("ClientId");
|
||||||
body.setGrantType("password");
|
body.setGrantType("password");
|
||||||
body.setTenantId("1");
|
body.setTenantId("1");
|
||||||
body.setCode("code1");
|
body.setCode("code1");
|
||||||
body.setUuid("qweqw");
|
body.setUuid("qweqw");
|
||||||
|
|
||||||
SysClientVo client = new SysClientVo();
|
SysClientVo client = new SysClientVo();
|
||||||
client.setId(1L);
|
client.setId(1L);
|
||||||
client.setClientId("ClientId1");
|
client.setClientId("ClientId1");
|
||||||
client.setClientKey("setClientKey");
|
client.setClientKey("setClientKey");
|
||||||
client.setClientSecret("setClientSecret");
|
client.setClientSecret("setClientSecret");
|
||||||
client.setGrantType("password");
|
client.setGrantType("password");
|
||||||
client.setDeviceType("pc");
|
client.setDeviceType("pc");
|
||||||
client.setActiveTimeout(604800L);
|
client.setActiveTimeout(1800L);
|
||||||
client.setTimeout(604800L);
|
client.setTimeout(300L);
|
||||||
client.setStatus("0");
|
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.ok("登录成功!").setData(StpUtil.getTokenValue());
|
||||||
}
|
}
|
||||||
return SaResult.error("登录失败!");
|
return SaResult.error("登录失败!");
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# 端口
|
# 端口
|
||||||
server:
|
server:
|
||||||
port: 9000
|
port: 19000
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
cloud:
|
cloud:
|
||||||
@ -40,23 +40,28 @@ spring:
|
|||||||
|
|
||||||
# Sa-Token配置
|
# Sa-Token配置
|
||||||
sa-token:
|
sa-token:
|
||||||
|
sign:
|
||||||
|
# API 接口签名秘钥 (随便乱摁几个字母即可)
|
||||||
|
secret-key: kQwIOrYbtXmSDkwEiFngrKidMcdrgKor
|
||||||
|
# 读写同源,开启后每次访问都会自动续期
|
||||||
# token名称 (同时也是cookie名称)
|
# token名称 (同时也是cookie名称)
|
||||||
token-name: Authorization
|
token-name: Authorization
|
||||||
# cookie写入
|
# cookie写入 TODO 后续必须关掉,cookie 有安全隐患
|
||||||
is-read-cookie: true
|
is-read-cookie: true
|
||||||
# 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
|
# 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
|
||||||
is-concurrent: true
|
is-concurrent: true
|
||||||
# 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
|
# 在多人登录同一账号时,是否共用一个token (为true(默认)时所有登录共用一个token, 为false时每次登录新建一个token),这个为false会导致登陆多次后redis内存的token不能很好释放
|
||||||
is-share: false
|
is-share: true
|
||||||
# jwt秘钥
|
# jwt秘钥
|
||||||
jwt-secret-key: abcdefghijklmnopqrstuvwxyz
|
jwt-secret-key: abcdcosctlklmnopqrstuvwxyz
|
||||||
|
|
||||||
# ------- SSO-模式二相关配置
|
# ------- SSO-模式二相关配置
|
||||||
sso-server:
|
sso-server:
|
||||||
# Ticket有效期 (单位: 秒),默认五分钟
|
# Ticket有效期 (单位: 秒),默认五分钟
|
||||||
ticket-timeout: 300
|
ticket-timeout: 300
|
||||||
# 所有允许的授权回调地址
|
# 所有允许的授权回调地址 TODO 生产和测试一定要改
|
||||||
allow-url: "*"
|
allow-url: "*"
|
||||||
|
active-timeout: 30
|
||||||
|
|
||||||
# ruoyi-security配置
|
# ruoyi-security配置
|
||||||
security:
|
security:
|
||||||
@ -75,4 +80,5 @@ security:
|
|||||||
# swagger 文档配置
|
# swagger 文档配置
|
||||||
- /*/api-docs
|
- /*/api-docs
|
||||||
- /*/api-docs/**
|
- /*/api-docs/**
|
||||||
|
- /sso/signout
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
var sa = {};
|
var sa = {};
|
||||||
|
|
||||||
function setCookie(cname, cvalue, exdays) {
|
function setCookie(cname, cvalue, exdays) {
|
||||||
|
//TODO 这里需要先清理旧token,可以用jscookie包,然后sso登录还有点问题,sso的首页访问不了
|
||||||
var d = new Date();
|
var d = new Date();
|
||||||
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
|
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
|
||||||
var expires = "expires=" + d.toGMTString();
|
var expires = "expires=" + d.toGMTString();
|
||||||
|
|||||||
@ -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>
|
||||||
Loading…
Reference in New Issue
Block a user