Commit eef233644cc489f30ce1b1bec1a5279ff5c6316f
1 parent
2b784d18
1. 增加 yudao-sso-demo-by-code 项目,实现基于授权码模式的单点登录
2. 完成 yudao-sso-demo-by-code 的跳转 SSO 流程
Showing
9 changed files
with
159 additions
and
1 deletions
pom.xml
yudao-example/pom.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | |
| 3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| 5 | + <modelVersion>4.0.0</modelVersion> | |
| 6 | + | |
| 7 | + <!-- 由于方便大家拷贝,使用不使用 yudao 作为 Maven parent --> | |
| 8 | + | |
| 9 | + <groupId>cn.iocoder.boot</groupId> | |
| 10 | + <artifactId>yudao-example</artifactId> | |
| 11 | + <version>1.0.0-snapshot</version> | |
| 12 | + <packaging>pom</packaging> | |
| 13 | + <modules> | |
| 14 | + <module>yudao-sso-demo-by-code</module> | |
| 15 | + </modules> | |
| 16 | + | |
| 17 | + <name>${project.artifactId}</name> | |
| 18 | + <description>提供各种示例,例如说:SSO 单点登录</description> | |
| 19 | + <url>https://github.com/YunaiV/ruoyi-vue-pro</url> | |
| 20 | + | |
| 21 | +</project> | ... | ... |
yudao-example/yudao-sso-demo-by-code/pom.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | |
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | |
| 3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| 5 | + <modelVersion>4.0.0</modelVersion> | |
| 6 | + | |
| 7 | + <!-- 由于方便大家拷贝,使用不使用 yudao 作为 Maven parent --> | |
| 8 | + | |
| 9 | + <groupId>cn.iocoder.boot</groupId> | |
| 10 | + <artifactId>yudao-sso-demo-by-code</artifactId> | |
| 11 | + <version>1.0.0-snapshot</version> | |
| 12 | + <packaging>jar</packaging> | |
| 13 | + | |
| 14 | + <name>${project.artifactId}</name> | |
| 15 | + <description>基于授权码模式,如何实现 SSO 单点登录?</description> | |
| 16 | + <url>https://github.com/YunaiV/ruoyi-vue-pro</url> | |
| 17 | + | |
| 18 | + <properties> | |
| 19 | + <!-- Maven 相关 --> | |
| 20 | + <maven.compiler.source>8</maven.compiler.source> | |
| 21 | + <maven.compiler.target>8</maven.compiler.target> | |
| 22 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
| 23 | + <!-- 统一依赖管理 --> | |
| 24 | + <spring.boot.version>2.6.10</spring.boot.version> | |
| 25 | + </properties> | |
| 26 | + | |
| 27 | + <dependencyManagement> | |
| 28 | + <dependencies> | |
| 29 | + <!-- 统一依赖管理 --> | |
| 30 | + <dependency> | |
| 31 | + <groupId>org.springframework.boot</groupId> | |
| 32 | + <artifactId>spring-boot-dependencies</artifactId> | |
| 33 | + <version>${spring.boot.version}</version> | |
| 34 | + <type>pom</type> | |
| 35 | + <scope>import</scope> | |
| 36 | + </dependency> | |
| 37 | + </dependencies> | |
| 38 | + </dependencyManagement> | |
| 39 | + | |
| 40 | + <dependencies> | |
| 41 | + <!-- Web 相关 --> | |
| 42 | + <dependency> | |
| 43 | + <groupId>org.springframework.boot</groupId> | |
| 44 | + <artifactId>spring-boot-starter-web</artifactId> | |
| 45 | + </dependency> | |
| 46 | + | |
| 47 | + <dependency> | |
| 48 | + <groupId>org.springframework.boot</groupId> | |
| 49 | + <artifactId>spring-boot-starter-security</artifactId> | |
| 50 | + </dependency> | |
| 51 | + </dependencies> | |
| 52 | + | |
| 53 | +</project> | ... | ... |
yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/SSODemoApplication.java
0 → 100644
| 1 | +package cn.iocoder.yudao.ssodemo; | |
| 2 | + | |
| 3 | +import org.springframework.boot.SpringApplication; | |
| 4 | +import org.springframework.boot.autoconfigure.SpringBootApplication; | |
| 5 | + | |
| 6 | +@SpringBootApplication | |
| 7 | +public class SSODemoApplication { | |
| 8 | + | |
| 9 | + public static void main(String[] args) { | |
| 10 | + SpringApplication.run(SSODemoApplication.class, args); | |
| 11 | + } | |
| 12 | + | |
| 13 | +} | ... | ... |
yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/controller/AuthController.java
0 → 100644
| 1 | +package cn.iocoder.yudao.ssodemo.controller; | |
| 2 | + | |
| 3 | +import org.springframework.stereotype.Controller; | |
| 4 | +import org.springframework.web.bind.annotation.PostMapping; | |
| 5 | + | |
| 6 | +@Controller("/auth") | |
| 7 | +public class AuthController { | |
| 8 | + | |
| 9 | + @PostMapping("/login-by-code") | |
| 10 | + public void loginByCode() { | |
| 11 | + | |
| 12 | + } | |
| 13 | + | |
| 14 | +} | ... | ... |
yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/framework/SecurityConfiguration.java
0 → 100644
| 1 | +package cn.iocoder.yudao.ssodemo.framework; | |
| 2 | + | |
| 3 | +import org.springframework.context.annotation.Configuration; | |
| 4 | +import org.springframework.http.HttpMethod; | |
| 5 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
| 6 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | |
| 7 | + | |
| 8 | +@Configuration | |
| 9 | +public class SecurityConfiguration extends WebSecurityConfigurerAdapter { | |
| 10 | + | |
| 11 | + @Override | |
| 12 | + protected void configure(HttpSecurity httpSecurity) throws Exception { | |
| 13 | + httpSecurity.authorizeRequests() | |
| 14 | + // 1. 静态资源,可匿名访问 | |
| 15 | + .antMatchers(HttpMethod.GET, "/*.html", "/**/*.html", "/**/*.css", "/**/*.js").permitAll() | |
| 16 | + // last. 兜底规则,必须认证 | |
| 17 | + .and().authorizeRequests() | |
| 18 | + .anyRequest().authenticated(); | |
| 19 | + } | |
| 20 | +} | ... | ... |
yudao-example/yudao-sso-demo-by-code/src/main/resources/application.yaml
0 → 100644
yudao-example/yudao-sso-demo-by-code/src/main/resources/static/login.html
0 → 100644
| 1 | +<!DOCTYPE html> | |
| 2 | +<html lang="en"> | |
| 3 | +<head> | |
| 4 | + <meta charset="UTF-8"> | |
| 5 | + <title>首页</title> | |
| 6 | + <!-- jQuery:操作 dom、发起请求等 --> | |
| 7 | + <script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/2.1.2/jquery.min.js" type="application/javascript"></script> | |
| 8 | + <!-- jQuery Cookie:操作 cookie 等 --> | |
| 9 | + <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery-cookie/1.4.1/jquery.cookie.min.js" type="application/javascript"/></script> | |
| 10 | + | |
| 11 | + <script type="application/javascript"> | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * 跳转单点登录 | |
| 15 | + */ | |
| 16 | + function ssoLogin() { | |
| 17 | + const clientId = 'yudao-sso-demo-by-code'; // 可以改写成,你的 clientId | |
| 18 | + const redirectUri = encodeURIComponent('http://127.0.0.1:18080/callback'); // 注意,需要使用 encodeURIComponent 编码地址 | |
| 19 | + const responseType = 'code'; // 1)授权码模式,对应 code;2)简化模式,对应 token | |
| 20 | + window.location.href = 'http://127.0.0.1:1024/sso?client_id=' + clientId | |
| 21 | + + '&redirect_uri=' + redirectUri | |
| 22 | + + '&response_type=' + responseType; | |
| 23 | + } | |
| 24 | + </script> | |
| 25 | +</head> | |
| 26 | +<body> | |
| 27 | +<!-- 情况一:未登录:1)跳转 ruoyi-vue-pro 的 SSO 登录页 --> | |
| 28 | +<div> | |
| 29 | + 您未登录,点击 <a href="#" onclick="ssoLogin()">跳转 </a> SSO 单点登录 | |
| 30 | +</div> | |
| 31 | + | |
| 32 | +<!-- 情况二:已登录:1)展示用户信息;2)刷新访问令牌;3)退出登录 --> | |
| 33 | +</body> | |
| 34 | +</html> | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/tenant/TenantPackageDO.java