mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 12:18:42 +08:00 
			
		
		
		
	1. 增加 yudao-sso-demo-by-code 项目,实现基于授权码模式的单点登录
2. 完成 yudao-sso-demo-by-code 的跳转 SSO 流程
This commit is contained in:
		@@ -0,0 +1,13 @@
 | 
			
		||||
package cn.iocoder.yudao.ssodemo;
 | 
			
		||||
 | 
			
		||||
import org.springframework.boot.SpringApplication;
 | 
			
		||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
			
		||||
 | 
			
		||||
@SpringBootApplication
 | 
			
		||||
public class SSODemoApplication {
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        SpringApplication.run(SSODemoApplication.class, args);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,14 @@
 | 
			
		||||
package cn.iocoder.yudao.ssodemo.controller;
 | 
			
		||||
 | 
			
		||||
import org.springframework.stereotype.Controller;
 | 
			
		||||
import org.springframework.web.bind.annotation.PostMapping;
 | 
			
		||||
 | 
			
		||||
@Controller("/auth")
 | 
			
		||||
public class AuthController {
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/login-by-code")
 | 
			
		||||
    public void loginByCode() {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,20 @@
 | 
			
		||||
package cn.iocoder.yudao.ssodemo.framework;
 | 
			
		||||
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.http.HttpMethod;
 | 
			
		||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 | 
			
		||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 | 
			
		||||
 | 
			
		||||
@Configuration
 | 
			
		||||
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void configure(HttpSecurity httpSecurity) throws Exception {
 | 
			
		||||
        httpSecurity.authorizeRequests()
 | 
			
		||||
                // 1. 静态资源,可匿名访问
 | 
			
		||||
                .antMatchers(HttpMethod.GET, "/*.html", "/**/*.html", "/**/*.css", "/**/*.js").permitAll()
 | 
			
		||||
                // last. 兜底规则,必须认证
 | 
			
		||||
                .and().authorizeRequests()
 | 
			
		||||
                .anyRequest().authenticated();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,2 @@
 | 
			
		||||
server:
 | 
			
		||||
  port: 18080
 | 
			
		||||
@@ -0,0 +1,34 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
	<meta charset="UTF-8">
 | 
			
		||||
	<title>首页</title>
 | 
			
		||||
	<!-- jQuery:操作 dom、发起请求等 -->
 | 
			
		||||
	<script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/2.1.2/jquery.min.js" type="application/javascript"></script>
 | 
			
		||||
	<!-- jQuery Cookie:操作 cookie 等 -->
 | 
			
		||||
	<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>
 | 
			
		||||
 | 
			
		||||
	<script type="application/javascript">
 | 
			
		||||
 | 
			
		||||
		/**
 | 
			
		||||
		 * 跳转单点登录
 | 
			
		||||
     */
 | 
			
		||||
		function ssoLogin() {
 | 
			
		||||
			const clientId = 'yudao-sso-demo-by-code'; // 可以改写成,你的 clientId
 | 
			
		||||
      const redirectUri = encodeURIComponent('http://127.0.0.1:18080/callback'); // 注意,需要使用 encodeURIComponent 编码地址
 | 
			
		||||
      const responseType = 'code'; // 1)授权码模式,对应 code;2)简化模式,对应 token
 | 
			
		||||
      window.location.href = 'http://127.0.0.1:1024/sso?client_id=' + clientId
 | 
			
		||||
				+ '&redirect_uri=' + redirectUri
 | 
			
		||||
				+ '&response_type=' + responseType;
 | 
			
		||||
    }
 | 
			
		||||
	</script>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
<!-- 情况一:未登录:1)跳转 ruoyi-vue-pro 的 SSO 登录页 -->
 | 
			
		||||
<div>
 | 
			
		||||
	您未登录,点击 <a href="#" onclick="ssoLogin()">跳转 </a> SSO 单点登录
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
<!-- 情况二:已登录:1)展示用户信息;2)刷新访问令牌;3)退出登录 -->
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
		Reference in New Issue
	
	Block a user