javascript
使用OAuth 2 / OpenID Connect的SSO的Spring Boot 2本机方法
這篇文章是3篇系列文章的最后一部分,該系列文章探討了如何為基于Spring Boot 2的應用程序啟用OAuth2提供程序SSO。 3個帖子是:
這篇文章將探討使用Spring Security中的本機OAuth2支持為Spring Boot 2應用程序啟用SSO的嶄新方法。
該文章再次假定第一篇文章中描述的所有內容均已完成。
Spring Boot 2自動配置
Spring Boot 2為Spring Security中的本機OAuth2支持提供了自動配置(請參見org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientAutoConfiguration類)。
通過以下gradle坐標可用的“ spring-security-oauth2-client”庫激活了自動配置:
compile "org.springframework.security:spring-security-oauth2-client"此自動配置會處理一組屬性,對于已啟動的UAA身份提供程序,該組屬性如下:
uaa-base-url: http://localhost:8080/uaaspring:security:oauth2:client:registration:uaa:client-id: client1client-secret: client1authorizationGrantType: authorization_coderedirect_uri_template: "{baseUrl}/login/oauth2/code/{registrationId}"scope: resource.read,resource.write,openid,profileclientName: oauth2-sample-clientprovider:uaa:token-uri: ${uaa-base-url}/oauth/tokenauthorization-uri: ${uaa-base-url}/oauth/authorizeuser-info-uri: ${uaa-base-url}/userinfojwk-set-uri: ${uaa-base-url}/token_keysuserNameAttribute: user_name如果要依靠Spring Boot 2自動配置支持來實現對本機OAuth2的支持,并實現應用程序的啟動,那么在訪問應用程序時將看到以下頁面:
請注意,此登錄頁面是由Spring Security OAuth2創建的默認頁面,默認情況下會顯示注冊列表。
單擊“ oauth2-sample-client”,將顯示身份提供者(在這種情況下,UAA)的登錄頁面:
對于基于OpenID Connect的流程,應用程序將獲得ID令牌以及我正在解碼并呈現在頁面上的Access令牌:
客制化
我要進行的快速自定義之一是在訪問通過“ / secured” uri模式指定的任何受保護頁面時重定向到UAA,以下是一組應啟用此配置的配置:
package sample.oauth2.configimport 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@Configuration class OAuth2SecurityConfig : WebSecurityConfigurerAdapter() {override fun configure(web: WebSecurity) {super.configure(web)web.ignoring().mvcMatchers("/favicon.ico","/webjars/**","/css/**")}override fun configure(http: HttpSecurity) {http.csrf().disable()http.authorizeRequests().antMatchers("/secured/**").authenticated().antMatchers("/", "/custom_login").permitAll().anyRequest().authenticated().and().oauth2Login().loginPage("/custom_login")} }請參閱上面設置為“ / custom_login”的URI,依次將控制權簡單地移交給OAuth2控制的端點,這些端點知道設置適當的參數并重定向到UAA:
@Controller class LoginController {@RequestMapping("/custom_login")fun loginPage(): String {return "redirect:/oauth2/authorization/uaa"} }到此結束對Spring Boo2應用程序中對本機OAuth2支持的探索。
所有示例都可以在我的github存儲庫中找到 – https://github.com/bijukunjummen/oauth2-boot2
以下參考資料有助于理解OAuth2支持:
1. Spring安全性文檔 – https://docs.spring.io/spring-security/site/docs/current/reference/html/
2. Joe Grandja的 Spring One Platform 2017演示文稿 – https://www.youtube.com/watch?v=WhrOCurxFWU
翻譯自: https://www.javacodegeeks.com/2018/03/spring-boot-2-native-approach-sso-oauth-2-openid-connect.html
總結
以上是生活随笔為你收集整理的使用OAuth 2 / OpenID Connect的SSO的Spring Boot 2本机方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 尼康d40上市价格(尼康D40价格)
- 下一篇: rust语法丑陋_抛出异常–缓慢而丑陋