单点登录(sso)在微服务架构中广泛应用,spring security整合oauth2是实现方式之一。1. 搭建oauth2认证中心需引入相关依赖,并通过@enableauthorizationserver配置客户端信息及用户详情;2. 客户端接入时添加spring-boot-starter-oauth2-client依赖,在application.yml中配置认证中心参数并通过@enablewebsecurity启用oauth2登录支持;3. 单点登出可通过维护token黑名单或利用openid connect的end_session_endpoint实现,客户端触发登出时清除本地会话并跳转至认证中心完成统一退出;4. 注意事项包括处理跨域问题、合理设置token有效期、确保生产环境使用https以及正确配置用户信息接口。整个流程需细致配置各环节参数以保障登录流程顺畅。

单点登录(SSO)在现代系统中越来越常见,尤其是在微服务架构下。Spring Security整合OAuth2实现SSO是一个比较常见的做法,核心在于搭建一个认证中心(Authorization Server),并让其他服务作为资源服务器或客户端去信任它。

下面我会以最常见的场景为例:使用Spring Security + OAuth2搭建一个认证中心,并让另一个应用作为客户端接入,完成一次单点登录流程。
首先需要构建一个OAuth2的授权服务器(Authorization Server),这是整个单点登录的核心。

步骤如下:
引入依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>配置OAuth2客户端信息(比如客户端ID、密钥、回调地址等):
@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client-id")
.secret("{noop}client-secret")
.authorizedGrantTypes("authorization_code", "refresh_token", "implicit")
.scopes("read", "write")
.redirectUris("http://localhost:8081/login/oauth2/code/my-client");
}
}同时配置用户信息(可以是内存中的用户,也可以对接数据库):
@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}这样就完成了一个基础的OAuth2认证中心搭建。
接下来是让另一个Spring Boot应用作为客户端接入这个认证中心,完成登录跳转和用户认证。
关键配置包括:
添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>在application.yml中配置OAuth2提供者信息:
spring:
security:
oauth2:
client:
registration:
my-client:
client-id: client-id
client-secret: client-secret
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
scope: read,write
provider:
my-provider:
authorization-uri: http://localhost:8080/oauth/authorize
token-uri: http://localhost:8080/oauth/token
user-info-uri: http://localhost:8080/user
user-name-attribute: name确保安全配置允许OAuth2登录:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login(); // 开启OAuth2登录支持
}
}当访问客户端应用时,会自动跳转到认证中心进行登录,登录成功后返回客户端页面。
单点登录完成后,退出登录也应同步进行。OAuth2协议本身不直接支持单点登出,但可以通过以下方式模拟:
end_session_endpoint来统一登出。例如,在客户端添加登出逻辑:
@GetMapping("/logout")
public String logout(HttpServletRequest request, HttpServletResponse response) throws ServletException {
request.logout(); // 清除本地session
return "redirect:http://localhost:8080/logout"; // 跳转到认证中心登出页面
}{noop}。user-info-uri能正确返回用户信息,否则可能无法完成登录。基本上就这些。整个过程不算复杂,但细节容易忽略,尤其是URL路径、跨域、token验证这几个环节,稍微配置不对就会导致登录失败或无法跳转。只要一步步对照着做,应该没问题。
以上就是Spring Security整合OAuth2实现单点登录的详细步骤的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号