Spring Security 與 Oauth2.0

問題

最近因爲工做變更,我又開始搞 Java 後臺了(作回老本行)。目前第一個工做項目是搞一個用戶認證中心,因而便一腳踏入了 Spring Security 的坑裏面。其實當下比較流行的一套解決方案就是 Spring Security + Oauth2.0 + JWT 方式。但是當我開始集成 Spring Security 和 Oauth2.0 的時候,我眉頭一皺忽然發現這個事情不簡單。java

<!--more-->git

在建立 Springboot 工程時,能夠選擇如下的 Oauth2.0 依賴:github

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

我心想咋還這麼多依賴包呢。本着面向百度編程的原則,從一衆「天下文章一大抄」的博客裏發現他們還用的是另外兩個依賴:spring

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,裏面大部分方法都是被橫線劃掉的(被廢棄的)。爲啥要這樣呢?ide

後來本着吃瓜的心態,扒了扒 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 的實現。所以官方要統一放在一個地方。spring-boot

我想,這是個好事啊,省的讓你們不知道使用哪個了。固然官方也是很給力,不只完成了對 Oauth2.0 的支持,也加入了 OpenID Connect1.0 的支持。可是呢,社區又來了個騷操做:宣佈再也不支持受權服務器。由於官方認爲受權服務器是一種產品形態,並不是框架該完成的,其次目前已經有不少商用和開源的受權服務器了(例如 Keycloak、Okta)。可是一衆開發者不服啊,在社區裏進行激烈討論。因而官方妥協,發起了新的項目 spring-authorization-server,目前已經迭代到了0.0.3版本。ui

現狀 & 遷移

吃完瓜,來看看這幾個包如今是什麼狀態。

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

相關文章
相關標籤/搜索