完成 sa-token-demo-sso1-client-solon 示例 demo

This commit is contained in:
noear 2023-03-13 18:00:32 +08:00
parent 8a35671a0b
commit 269a058137
5 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-demo-sso1-client-solon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Solon -->
<parent>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<version>2.2.3</version>
<relativePath/>
</parent>
<!-- 定义 Sa-Token 版本号 -->
<properties>
<sa-token.version>1.34.0</sa-token.version>
</properties>
<dependencies>
<!-- Solon 依赖 -->
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-api</artifactId>
</dependency>
<!-- Sa-Token 权限认证, 在线文档https://sa-token.cc/ -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-solon-plugin</artifactId>
<version>${sa-token.version}</version>
</dependency>
<!-- Sa-Token 插件整合SSO -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-sso</artifactId>
<version>${sa-token.version}</version>
</dependency>
<!-- Sa-Token 整合 redisx -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redisx</artifactId>
<version>${sa-token.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,22 @@
package com.pj;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.solon.dao.SaTokenDaoOfRedis;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
/**
* @author noear 2023/3/13 created
*/
@Configuration
public class SaConfig {
/**
* 配置 Sa-Token 单独使用的Redis连接 此处需要和SSO-Server端连接同一个Redis
* */
@Bean
public SaTokenDao saTokenDaoInit(@Inject("${sa-token-dao.redis}") SaTokenDaoOfRedis saTokenDao) {
return saTokenDao;
}
}

View File

@ -0,0 +1,20 @@
package com.pj;
import org.noear.solon.Solon;
import org.noear.solon.annotation.SolonMain;
/**
* SSO模式一Client端 Demo
* @author kong
*
*/
@SolonMain
public class SaSso1ClientApp {
public static void main(String[] args) {
Solon.start(SaSso1ClientApp.class, args);
System.out.println("\nSa-Token SSO模式一 Client端启动成功");
}
}

View File

@ -0,0 +1,40 @@
package com.pj.sso;
import cn.dev33.satoken.sso.SaSsoManager;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Render;
/**
* Sa-Token-SSO Client端 Controller
* @author kong
*/
@Controller
public class SsoClientController implements Render {
// SSO-Client端首页
@Mapping("/")
public String index() {
String authUrl = SaSsoManager.getConfig().splicingAuthUrl();
String solUrl = SaSsoManager.getConfig().splicingSloUrl();
String str = "<h2>Sa-Token SSO-Client 应用端</h2>" +
"<p>当前会话是否登录:" + StpUtil.isLogin() + "</p>" +
"<p><a href=\"javascript:location.href='" + authUrl + "?mode=simple&redirect=' + encodeURIComponent(location.href);\">登录</a> " +
"<a href=\"javascript:location.href='" + solUrl + "?back=' + encodeURIComponent(location.href);\">注销</a> </p>";
return str;
}
// 全局异常拦截并转换
@Override
public void render(Object data, Context ctx) throws Throwable {
if(data instanceof Exception){
data = SaResult.error(((Exception)data).getMessage());
}
ctx.render(data);
}
}

View File

@ -0,0 +1,25 @@
# 端口
server:
port: 9001
# Sa-Token 配置
sa-token:
# SSO-相关配置
sso:
# SSO-Server端-单点登录授权地址
auth-url: http://sso.stp.com:9000/sso/auth
# SSO-Server端-单点注销地址
slo-url: http://sso.stp.com:9000/sso/signout
# 配置 Sa-Token 单独使用的Redis连接 此处需要和SSO-Server端连接同一个Redis
sa-token-dao: #名字可以随意取
redis:
server: "localhost:6379"
password: 123456
db: 1
maxTotal: 200