springboot oauth2實現(xiàn)單點登錄實例
我們見過的很多網(wǎng)站,容許使用第三方賬號登錄,他不需要關(guān)注用戶信息,只需要用戶拿到授權(quán)碼就可以訪問。
oauth2是用來做三方登錄的,他的授權(quán)方式有好幾種,授權(quán)碼模式、密碼模式、隱式模式、客戶端模式。
oauth2認證的過程如下:一般我們請求一個需要登錄的網(wǎng)站A,會提示我們使用第三方網(wǎng)站C的用戶登錄,我們登錄,這時候需要我們授權(quán),就是authorize,授權(quán)之后,會得到一個token,我們拿到這個token就可以訪問這個網(wǎng)站A了。A網(wǎng)站不關(guān)心C網(wǎng)站的用戶信息。
springsecurity結(jié)合oauth2可以做這個功能,這里給出一個springboot+security+oauth2的實例,這個實例很直觀,就是我們有兩個服務A,C,A是需要用戶登錄的網(wǎng)站,而C是授權(quán)服務器。當我們訪問A的資源:http://localhost:8000/hello,他會跳到C的服務器上登錄,登錄完成,授權(quán),就直接調(diào)回來A網(wǎng)站,這里使用了單點登錄功能。
構(gòu)建項目:我這里構(gòu)建了一個父級項目securityoauth2,然后加入了兩個模塊:auth-server,client-1。
項目依賴:
securityoauth2->pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.3.2.RELEASE</version> </dependency> </dependencies> <modules> <module>auth-server</module> <module>client-1</module> </modules>
***********auth-server***************************************
AuthServerConfiguration.java
package com.xxx.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; @Configuration @EnableAuthorizationServer public class AuthServerConfiguration extends AuthorizationServerConfigurerAdapter{ @Autowired private PasswordEncoder passwordEncoder; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client") .secret(passwordEncoder.encode("secret")) .autoApprove(true) .redirectUris("http://localhost:8000/login","http://localhost:8001/login") .scopes("user") .accessTokenValiditySeconds(7200) .authorizedGrantTypes("authorization_code"); } }
授權(quán)服務配置這里,主要設置client_id,以及secret,這里設置了自動授權(quán)autoApprove(true),就是我們輸入用戶名和密碼登錄之后,不會出現(xiàn)一個需要我們手動點擊authorize的按鈕進行授權(quán)。另外配置了redirect_uri,這個是必須的。最后還設置了授權(quán)類型,這里選擇的是授權(quán)碼模式。
SecurityConfiguration.java
package com.xxx.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration @Order(1) @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter{ @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(AuthenticationManagerBuilder builder) throws Exception { builder.inMemoryAuthentication() .withUser("admin") .password(passwordEncoder().encode("admin")) .roles("admin"); } @Override protected void configure(HttpSecurity http) throws Exception { http.requestMatchers() .antMatchers("/login") .antMatchers("/oauth/authorize") .and() .authorizeRequests().anyRequest().authenticated() .and() .formLogin() .and() .csrf().disable(); } }
Security是做權(quán)限管理的,他需要配置用戶和密碼,這里采用內(nèi)存保存的方式,將用戶名和密碼保存在內(nèi)存中,而不是數(shù)據(jù)庫或者redis中,關(guān)于保存在哪里,對于了解oauth2來說,放在內(nèi)存是最簡單的,省去了建表,做數(shù)據(jù)庫查詢的麻煩。
TestController.java
package com.xxx.web; import java.security.Principal; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/test") public class TestController { @GetMapping("/user") public Principal currentUser(Principal principal) { return principal; } }
這個controller配置,不是用來測試訪問他的,而是用來做一個接口,給client-1使用,client-1配置文件中有個配置:security.oauth2.resource.user-info-uri就是配置的這個接口。
App.java
package com.xxx; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; @SpringBootApplication @EnableResourceServer public class App { public static void main( String[] args ){ SpringApplication.run(App.class, args); } }
application.yaml //無配置,可以略
***********client-1*********************************************************
SecurityConfiguration.java
package com.xxx.config; 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.configuration.WebSecurityConfigurerAdapter; @SuppressWarnings("deprecation") @Configuration @EnableOAuth2Sso public class SecurityConfiguration extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated().and().csrf().disable(); } }
TestController.java
package com.xxx.web; import java.util.Arrays; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @GetMapping("/hello") public String hello() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); return authentication.getName()+Arrays.toString(authentication.getAuthorities().toArray()); } }
App.java
package com.xxx; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main( String[] args ){ SpringApplication.run(App.class, args); } }
application.yaml
server: port: 8000 servlet: session: cookie: name: s1 security: oauth2: client: client-id: client client-secret: secret user-authorization-uri: http://localhost:8080/oauth/authorize access-token-uri: http://localhost:8080/oauth/token resource: user-info-uri: http://localhost:8080/api/test/user
以上都是代碼部分,下面主要是測試,測試過程很簡單,就是我們啟動兩個服務,打開瀏覽器訪問client-1的http://localhost:8000/hello接口,這時候因為需要登錄,會跳轉(zhuǎn)到auth-server服務的http://localhost:8080/login,登錄成功,會接著訪問授權(quán)接口,授權(quán)成功,會跳轉(zhuǎn)到http://localhost:8000/login,然后跳轉(zhuǎn)到http://localhost:8000/hello,貌似很復雜,我們先看看效果:
值得注意的是,如圖所示的2、3順序,在這個結(jié)果頁面是這樣的,但是在登錄頁面并不是這樣,而是先3后2:
可以這么解釋:client-1的接口http://localhost:8000/hello需要權(quán)限才能訪問,這時候,需要登錄自己的系統(tǒng)http://localhost:8000/login,而自己的系統(tǒng)支持oauth2,他通過封裝了client_id,response_type,redirect_uri,state等參數(shù)的授權(quán)碼模式請求接口,向auth-server服務發(fā)起授權(quán)請求http://localhost:8080/oauth/authorize?client_id=client&redirect_uri=http://localhost:8000/login&response_type=code&state=PphcA2,請求會跳轉(zhuǎn)auth-server的登錄頁面 http://localhost:8080/login。
這里因為使用了單點登錄,先從client-1跳轉(zhuǎn)到了auth-server,最后拿到了code之后,跳回了client-1,訪問成功。而頁面上顯示的內(nèi)容并不是一開始就是這樣子的,他會先跳轉(zhuǎn)auth-server登錄頁面:
以上代碼有了,結(jié)果也驗證了,但是個人還是不是很了解這個登錄授權(quán)的原理和過程,也是在學習中,這個所謂的單點登陸,在代碼上就用了一個@EnableOAuth2Sso注解在client-1項目的SecurityConfiguration類上,該類也是繼承自WebSecurityConfigurerAdapter類,然后覆蓋了configure(HttpSecurity http)方法。
到此這篇關(guān)于springboot oauth2實現(xiàn)單點登錄實例的文章就介紹到這了,更多相關(guān)springboot oauth2單點登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis通過JDBC數(shù)據(jù)驅(qū)動生成的執(zhí)行語句問題
這篇文章主要介紹了MyBatis通過JDBC數(shù)據(jù)驅(qū)動生成的執(zhí)行語句問題的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08Java使用OpenOffice將office文件轉(zhuǎn)換為PDF的示例方法
OpenOffice是一個開源的辦公套件,它包含了文檔處理、電子表格、演示文稿以及繪圖等多種功能,類似于Microsoft Office,本文將給大家介紹Java使用OpenOffice將office文件轉(zhuǎn)換為PDF的示例方法,需要的朋友可以參考下2024-09-09