为什么选择spring security oauth2_Spring Security 与 Oauth2.0

405e1157938dd57cefe469af2e676845.png

问题

最近由于工作变动,我又开始搞 Java 后台了(做回老本行)。目前第一个工作项目是搞一个用户认证中心,于是便一脚踏入了 Spring Security 的坑里面。其实当下比较流行的一套解决方案就是 Spring Security + Oauth2.0 + JWT 方式。可是当我开始集成 Spring Security 和 Oauth2.0 的时候,我眉头一皱突然发现这个事情不简单。 在创建 Springboot 工程时,可以选择以下的 Oauth2.0 依赖:

spring-boot-starter-oauth2-client
spring-boot-starter-oauth2-resource-server
spring-cloud-starter-oauth2

我心想咋还这么多依赖包呢。本着面向百度编程的原则,从一众“天下文章一大抄”的博客里发现他们还用的是另外两个依赖:

spring-security-oauth2
spring-security-oauth2-autoconfigure

这我就头大了啊,咋还整得这么复杂呢。所以只能去扒官方文档了。

官方文档解释

看到 Spring 官方对于 Oauth2.0 的解释,他说了这么一句

The Spring Security OAuth project is deprecated. The latest OAuth 2.0 support is provided by Spring Security. See the OAuth 2.0 Migration Guide for further details.

也就是说原来的 Spring Security OAuth2.0 已经废弃了,有关 OAuth2.0 的支持已经集成到了 Spring Security 里面了。这我就懵了。。。emmm。也就是说现在如果使用spring-security-oauth2,里面大部分方法都是被横线划掉的(被废弃的)。为啥要这样呢?

后来本着吃瓜的心态,扒了扒 OAuth 和 Spring 社区的历史。发现在 2018 年,Spring 社区就发布了声明,说是要逐渐停止现有的 OAuth2 支持,而在 Spring Security5 中构建下一代 OAuth2.0 支持。原因是Oauth2 落地混乱:Spring Security OAuth、Spring Cloud Security、Spring Boot 1.5.x 以及当时最新的 Spring Security5.x 中都提供了对 OAuth2 的实现。因此官方要统一放在一个地方。

我想,这是个好事啊,省的让大家不知道使用哪一个了。当然官方也是很给力,不仅完成了对 Oauth2.0 的支持,也加入了 OpenID Connect1.0 的支持。但是呢,社区又来了个骚操作:宣布不再支持授权服务器。因为官方认为授权服务器是一种产品形态,并非框架该完成的,其次目前已经有很多商用和开源的授权服务器了(例如 Keycloak、Okta)。但是一众开发者不服啊,在社区里进行激烈讨论。于是官方妥协,发起了新的项目 spring-authorization-server,目前已经迭代到了0.0.3版本。

现状 & 迁移

吃完瓜,来看看这几个包现在是什么状态。

spring-security-oauth2  -> 被废弃,建议不使用,否则后期无法维护

spring-security-oauth2-autoconfigure  -> 自动配置,没有用处
spring-boot-starter-oauth2-client -> 最新
spring-boot-starter-oauth2-resource-server  -> 最新
spring-cloud-starter-oauth2 -> 引用 spring-security-oauth2,但尚未标注被废弃

这样就比较明确了,现在的项目中需要依据该服务的用途去引用对应的包。

授权服务器

如果服务想做成授权服务器,暂时只能引用spring-cloud-starter-oauth2。因为这个包也是引用了spring-security-oauth2,但尚未标注@Deprecated,然后仍旧使用@EnableAuthorityServer注解进行配置。等待spring-authorization-server成熟,需要切换过来。

客户端

现在如果要开发客户端,只需要引用spring-boot-starter-oauth2-client,也只需要在原来的 SpringSecurity 的配置类中,调用.oauth2Client()即可配置,不需要以前的@EnableOAuth2Client注解了。

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

 @Override
 protected void configure(HttpSecurity http) throws Exception {
  http
   .authorizeRequests()
    .anyRequest().authenticated()
    .and()
   .formLogin()
    .loginPage("/login")
    .failureUrl("/login-error")
    .permitAll()
    .and()
   .oauth2Client(); //
 }

}

除此之外,也需要配置WebClientOAuth2AuthorizedClientManager这两个 Bean。具体如何实现,先挖个坑,以后再填。

资源服务器

资源服务器也只需要引用spring-boot-starter-oauth2-resource-server,若使用 JWK 方式,其配置如下(也仅仅是调用.oauth2ResourceServer()

@Configuration
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    @Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
    private String jwkSetUri;

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http
                .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/message/**").hasAuthority("SCOPE_all")
                .anyRequest().authenticated();
    }

    @Bean
    JwtDecoder jwtDecoder() {
        return NimbusJwtDecoder.withJwkSetUri(this.jwkSetUri).build();
    }
}

这样一下就清爽很多了。具体如何迁移,也可以参考官方文档 OAuth 2.0 Migration Guide

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值