使用spring-security-oauth2和spring-security-oauth2-autoconfigure依赖来实现OAuth 2.0+JWT,及介绍迁移到更现代的解决方案

摘要

        关于早些时候的Spring Security版本中,可以使用spring-security-oauth2spring-security-oauth2-autoconfigure依赖来实现OAuth 2.0。然而,随着新版本的发布,Spring Security 5 引入了新的OAuth2客户端和资源服务器支持,而且Spring Security团队已经推出了一个新的项目Spring Authorization Server来替代spring-security-oauth2作为授权服务器。

        本文为复现通过spring-boot-starter-oauth2-resource-serverspring-security-oauth2-autoconfigure实现OAuth 2.0及介绍迁移到更现代的解决方案。通常将一个项目配置成资源服务器以保护资源,另一个项目配置成授权服务器以发放令牌。

        以下是如何实现的示例:

资源服务器配置

首先,确保添加了资源服务器的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>

然后,在application.ymlapplication.properties中配置JWT令牌支持:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://your-auth-server.com # 授权服务器的issuer URI

资源服务器的安全配置类可能如下所示:

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests(authz -> authz
                .antMatchers("/public/**").permitAll()
                .anyRequest().authenticated())
            .oauth2ResourceServer(oauth2 -> oauth2.jwt());
    }
}

授权服务器配置

对于授权服务器,添加以下依赖以使用spring-security-oauth2-autoconfigure

<dependency>
    <groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>

application.yml中添加授权服务器配置:

security:
  oauth2:
    client:
      client-id: your-client-id
      client-secret: your-client-secret
      authorized-grant-types: authorization_code,refresh_token,password,client_credentials
      scope: read,write
    authorization:
      check-token-access: isAuthenticated()

然后,配置授权服务器和安全配置:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;

@Configuration
@EnableAuthorizationServer
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
    // 在这里配置授权服务器的端点、客户端详情、令牌存储等
}

总结

由于spring-security-oauth2-autoconfigure已经过时,并且它的功能在Spring Security 5中已经由原生支持取代,因此强烈建议迁移到Spring Security 5的OAuth2功能,或考虑使用Spring Authorization Server。

如果仍需要继续使用spring-security-oauth2-autoconfigure(可能是因为遗留系统兼容性等原因),那么可能需要查看更详细的文档和案例,因为这涉及到较旧版本的配置方法,包括在AuthorizationServerConfigurerAdapter实现中定义授权端点、令牌服务和安全约束。

请注意,Spring Boot 2.3.x版本的维护截至2021年已经结束,Spring Security 5不再包含OAuth 2.0 Authorization Server的支持,使用上述旧版本的方法应当谨慎,并计划迁移到更现代的解决方案。

基于security-oauth2-autoconfigure实现的OAuth2迁移到更现代的解决方案,Spring Security 5中使用OAuth2配合JWT实现安全认证-CSDN博客

  • 20
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ead_Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值