springboot实现单点登录代码实现
时间: 2023-05-21 09:02:25 浏览: 61
以下是一个简单的 Spring Boot 单点登录实现的代码示例:
1. 首先,我们需要在 pom.xml 文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
```
2. 在 application.yml 文件中添加以下配置:
```
spring:
security:
oauth2:
client:
registration:
sso:
client-id: <your-client-id>
client-secret: <your-client-secret>
client-name: SSO
provider: sso-provider
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
scope: openid,profile,email
provider:
sso-provider:
authorization-uri: <your-authorization-uri>
token-uri: <your-token-uri>
user-info-uri: <your-user-info-uri>
user-name-attribute: sub
```
3. 创建一个 WebSecurityConfigurerAdapter 类,用于配置 Spring Security:
```
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login/**", "/error", "/logout").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login()
.loginPage("/login")
.defaultSuccessURL("/home")
.and()
.logout()
.logoutSuccessUrl("/login")
.and()
.csrf().disable();
}
}
```
4. 创建一个 Controller 类,用于处理登录和注销请求:
```
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/home")
public String home() {
return "home";
}
@GetMapping("/logout")
public String logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
new SecurityContextLogoutHandler().logout(request, response, authentication);
}
return "redirect:/login?logout";
}
}
```
5. 创建一个 login.html 页面,用于显示登录界面:
```
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form th:action="@{/login/oauth2/code/sso}" method="post">
<button type="submit">Login with SSO</button>
</form>
</body>
</html>
```
6. 创建一个 home.html 页面,用于显示登录成功后的界面:
```
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome!</h1>
<a href="/logout">Logout</a>
</body>
</html>
```
这样,我们就完成了一个简单的 Spring Boot 单点登录实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)