Spring Security 與 Oauth2.0 Spring Security 與 Oauth2.0 問題 官方文檔解釋 現狀 & 遷移 授權服務器 客戶端 資源服務器

Spring Security 與 Oauth2.0

問題

最近由於工作變動,我又開始搞 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(); //
    }

}

除此之外,也需要配置 WebClient 和 OAuth2AuthorizedClientManager 這兩個 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();
    }
}

來源:https://www.tuicool.com/articles/euUN3i2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章