javascript
Spring Boot 2应用程序和OAuth 2 –传统方法
這篇文章是3個系列文章中的第二部分,探討了如何為基于Spring Boot 2的應用程序啟用OSO2提供程序SSO。 3個帖子是:
1. 引導兼容OpenID Connect的OAuth2授權服務器/ OpenID提供程序的方法
2.與OAuth2授權服務器/ OpenID提供程序集成的舊版Spring Boot / Spring 5方法–這篇文章
3.與OAuth2授權服務器/ OpenID Connect提供商集成的更新的Spring Boot 2 / Spring 5方法–即將推出
這篇文章將探討傳統的Spring Boot 2 / Spring Security 5方法,以為應用程序啟用基于OAuth2的身份驗證機制,本文假設所有
遵循了上一篇博客文章中的步驟, UAA已啟動并正在運行。
可能想到的一個問題是,為什么我應該在Spring Boot 2 / Spring Security 5的背景下談論遺留物,而這本來應該是做SSO的新方法! 原因是,隨著開發人員我們一直在使用Spring Boot 1.5.x的一種方法(現在認為該方法已被棄用),但是其中的功能尚未完全移植到新方法上(能夠啟動OAuth2授權服務器以及創建OAuth2資源服務器的能力是示例),在此期間,Spring Security開發人員(感謝
Rob Winch和Joe Grandja )通過spring-security-oauth2-boot項目的形式為傳統方法提供了橋梁。
方法
因此,舊方法的外觀是什么–在此之前,我已經詳細介紹了它,以總結一下它在一個名為@ EnableOAuth2SSO的注釋和一組支持該注釋的屬性的基礎上的工作原理,示例安全配置如下所示–
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@EnableOAuth2Sso @Configuration public class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter {@Overridepublic void configure(WebSecurity web) throws Exception {super.configure(web);web.ignoring().mvcMatchers("/favicon.ico", "/webjars/**", "/css/**");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable();http.authorizeRequests().antMatchers("/secured/**").authenticated().antMatchers("/").permitAll().anyRequest().authenticated();}}指向UAA的一組支持屬性如下:
ssoServiceUrl: http://localhost:8080/uaasecurity:oauth2:client:client-id: client1client-secret: client1access-token-uri: ${ssoServiceUrl}/oauth/tokenuser-authorization-uri: ${ssoServiceUrl}/oauth/authorizeresource:jwt:key-uri: ${ssoServiceUrl}/token_keyuser-info-uri: ${ssoServiceUrl}/userinfo將spring-security-oauth2-boot項目作為依賴項插入:
compile 'org.springframework.cloud:spring-cloud-starter-oauth2' compile("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.0.0.BUILD-SNAPSHOT")這些注釋也適用于Spring Boo2應用程序。 但是請注意,Spring Boot 2支持兩個不同的Web框架-Spring Web和Spring Webflux ,此方法可過渡地 引入 Spring Web ,這將Spring Web強制為默認框架 。
完整示例及其啟動方法可在我的github存儲庫中找到 – https://github.com/bijukunjummen/oauth2-boot2
測試中
任何以“ / secured / **”開頭的uri均已啟用SSO,如果訪問索引頁,則無需任何身份驗證即可顯示它:
現在,單擊以“ / secured / **”開頭的uri應該會觸發OAuth2 授權代碼流 :
并應通過UAA向用戶顯示登錄屏幕:
使用之前創建的憑證登錄– user1 / user1應該將用戶重定向回該應用程序的Spring Boot 2舊版,并顯示受保護的頁面:
這就完成了使用Spring Boot 2進行SSO的遺留方法。請注意,這只是偽身份驗證,OAuth2的用途更多是為了授權訪問用戶資源,而不是在這里使用身份驗證。 可以在此處找到澄清這一點的文章。 下一篇有關本機Spring Security 5 / Spring Boot2的文章將提供使用OpenID Connect的更清晰的身份驗證機制。
翻譯自: https://www.javacodegeeks.com/2018/02/spring-boot-2-applications-oauth-2-legacy-approach.html
總結
以上是生活随笔為你收集整理的Spring Boot 2应用程序和OAuth 2 –传统方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果手机怎样换行打字
- 下一篇: 苹果手机怎么一次性删除通讯录里的联系人