日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Springboot開發OAuth2認證授權與資源服務器操作

瀏覽:135日期:2023-03-02 11:38:04

設計并開發一個開放平臺。

一、設計:

Springboot開發OAuth2認證授權與資源服務器操作

網關可以 與認證授權服務合在一起,也可以分開。

二、開發與實現:

用Oauth2技術對訪問受保護的資源的客戶端進行認證與授權。

Oauth2技術應用的關鍵是:

1)服務器對OAuth2客戶端進行認證與授權。

2)Token的發放。

3)通過access_token訪問受OAuth2保護的資源。

選用的關鍵技術:Springboot, Spring-security, Spring-security-oauth2。

提供一個簡化版,用戶、token數據保存在內存中,用戶與客戶端的認證授權服務、資源服務,都是在同一個工程中。現實項目中,技術架構通常上將用戶與客戶端的認證授權服務設計在一個子系統(工程)中,而資源服務設計為另一個子系統(工程)。

1、Spring-security對用戶身份進行認證授權:

主要作用是對用戶身份通過用戶名與密碼的方式進行認證并且授權。

package com.banling.oauth2server.config; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.authentication.AuthenticationManager;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.web.util.matcher.AntPathRequestMatcher; @Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter{@Autowired public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception { //用戶信息保存在內存中//在鑒定角色roler時,會默認加上ROLLER_前綴auth.inMemoryAuthentication().withUser('user').password('user').roles('USER').and().withUser('test').password('test').roles('TEST'); }@Override protected void configure(HttpSecurity http) throws Exception {http.formLogin() //登記界面,默認是permit All.and().authorizeRequests().antMatchers('/','/home').permitAll() //不用身份認證可以訪問.and().authorizeRequests().anyRequest().authenticated() //其它的請求要求必須有身份認證.and().csrf() //防止CSRF(跨站請求偽造)配置.requireCsrfProtectionMatcher(new AntPathRequestMatcher('/oauth/authorize')).disable(); }@Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean(); }}

配置用戶信息,保存在內存中。也可以自定義將用戶數據保存在數據庫中,實現UserDetailsService接口,進行認證與授權,略。

配置訪問哪些URL需要授權。必須配置authorizeRequests(),否則啟動報錯,說是沒有啟用security技術。

注意,在這里的身份進行認證與授權沒有涉及到OAuth的技術:

當訪問要授權的URL時,請求會被DelegatingFilterProxy攔截,如果還沒有授權,請求就會被重定向到登錄界面。在登錄成功(身份認證并授權)后,請求被重定向至之前訪問的URL。

2、OAuth2的授權服務:

主要作用是OAuth2的客戶端進行認證與授權。

package com.banling.oauth2server.config; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.authentication.AuthenticationManager;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;import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;import org.springframework.security.oauth2.provider.approval.ApprovalStore;import org.springframework.security.oauth2.provider.approval.TokenApprovalStore;import org.springframework.security.oauth2.provider.token.TokenStore;import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore; @Configuration@EnableAuthorizationServerpublic class AuthServerConfig extends AuthorizationServerConfigurerAdapter{@Autowiredprivate TokenStore tokenStore;@Autowired private AuthenticationManager authenticationManager;@Autowiredprivate ApprovalStore approvalStore;@Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception {//添加客戶端信息//使用內存存儲OAuth客戶端信息clients.inMemory()// client_id.withClient('client')// client_secret.secret('secret')// 該client允許的授權類型,不同的類型,則獲得token的方式不一樣。.authorizedGrantTypes('authorization_code','implicit','refresh_token').resourceIds('resourceId')//回調uri,在authorization_code與implicit授權方式時,用以接收服務器的返回信息.redirectUris('http://localhost:8090/')// 允許的授權范圍.scopes('app','test'); }@Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {//reuseRefreshTokens設置為false時,每次通過refresh_token獲得access_token時,也會刷新refresh_token;也就是說,會返回全新的access_token與refresh_token。//默認值是true,只返回新的access_token,refresh_token不變。endpoints.tokenStore(tokenStore).approvalStore(approvalStore).reuseRefreshTokens(false).authenticationManager(authenticationManager); }@Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {security.realm('OAuth2-Sample').allowFormAuthenticationForClients().tokenKeyAccess('permitAll()').checkTokenAccess('isAuthenticated()'); }@Beanpublic TokenStore tokenStore() {//token保存在內存中(也可以保存在數據庫、Redis中)。//如果保存在中間件(數據庫、Redis),那么資源服務器與認證服務器可以不在同一個工程中。//注意:如果不保存access_token,則沒法通過access_token取得用戶信息return new InMemoryTokenStore();}@Beanpublic ApprovalStore approvalStore() throws Exception {TokenApprovalStore store = new TokenApprovalStore();store.setTokenStore(tokenStore);return store;}}

配置OAuth2的客戶端信息:clientId、client_secret、authorization_type、redirect_url等。本例是將數據保存在內存中。也可以保存在數據庫中,實現ClientDetailsService接口,進行認證與授權,略。

TokenStore是access_token的存儲單元,可以保存在內存、數據庫、Redis中。本例是保存在內存中。

3、OAuth2的資源服務:

主要作用是配置資源受保護的OAuth2策略。

package com.banling.oauth2server.config; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.http.SessionCreationPolicy;import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;import org.springframework.security.oauth2.provider.token.TokenStore; @Configuration@EnableResourceServerpublic class ResServerConfig extends ResourceServerConfigurerAdapter{@Autowiredprivate TokenStore tokenStore; @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception {resources.tokenStore(tokenStore).resourceId('resourceId'); } @Override public void configure(HttpSecurity http) throws Exception {/* 注意: 1、必須先加上: .requestMatchers().antMatchers(...),表示對資源進行保護,也就是說,在訪問前要進行OAuth認證。 2、接著:訪問受保護的資源時,要具有哪里權限。 ------------------------------------ 否則,請求只是被Security的攔截器攔截,請求根本到不了OAuth2的攔截器。 同時,還要注意先配置:security.oauth2.resource.filter-order=3,否則通過access_token取不到用戶信息。 ------------------------------------ requestMatchers()部分說明: Invoking requestMatchers() will not override previous invocations of :: mvcMatcher(String)}, requestMatchers(), antMatcher(String), regexMatcher(String), and requestMatcher(RequestMatcher). */http// Since we want the protected resources to be accessible in the UI as well we need // session creation to be allowed (it’s disabled by default in 2.0.6)//另外,如果不設置,那么在通過瀏覽器訪問被保護的任何資源時,每次是不同的SessionID,并且將每次請求的歷史都記錄在OAuth2Authentication的details的中.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).and() .requestMatchers() .antMatchers('/user','/res/**') .and() .authorizeRequests() .antMatchers('/user','/res/**') .authenticated(); }}

配置哪些URL資源是受OAuth2保護的。注意,必須配置sessionManagement(),否則訪問受護資源請求不會被OAuth2的攔截器ClientCredentialsTokenEndpointFilter與OAuth2AuthenticationProcessingFilter攔截,也就是說,沒有配置的話,資源沒有受到OAuth2的保護。

4、受OAuth2保存的資源:

1)獲取OAuth2客戶端的信息

package com.banling.oauth2server.web; import java.security.Principal; import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class UserController { @RequestMapping('/user')public Principal user(Principal principal) {//principal在經過security攔截后,是org.springframework.security.authentication.UsernamePasswordAuthenticationToken//在經OAuth2攔截后,是OAuth2Authentication return principal;}}

2)其它受保護的資源

package com.banling.oauth2server.web;import java.security.Principal; import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; /** * 作為OAuth2的資源服務時,不能在Controller(或者RestController)注解上寫上URL,因為這樣不會被識別,會報404錯誤。<br> *<br> { *<br> 'timestamp': 1544580859138, *<br> 'status': 404, *<br> 'error': 'Not Found', *<br> 'message': 'No message available', *<br> 'path': '/res/getMsg' *<br> } *<br> * * */@RestController()//作為資源服務時,不能帶上url,@RestController('/res')是錯的,無法識別。只能在方法上注解全路徑public class ResController {@RequestMapping('/res/getMsg')public String getMsg(String msg,Principal principal) {//principal中封裝了客戶端(用戶,也就是clientDetails,區別于Security的UserDetails,其實clientDetails中也封裝了UserDetails),不是必須的參數,除非你想得到用戶信息,才加上principal。return 'Get the msg: '+msg;}}5、application.properties配置:

security.oauth2.resource.filter-order=3 必須配置,否則對受護資源請求不會被OAuth2的攔截器攔截。

6、測試

1)authorization_code方式獲取code,然后再通過code獲取access_token(和refresh_token)。

在瀏覽輸入:

http://localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://localhost:8090/

在登錄界面輸入用戶名與密碼user/user,提交。

提交后服務重定向 至scope的授權界面:

Springboot開發OAuth2認證授權與資源服務器操作

授權后,在回調uri中可以得從code:

Springboot開發OAuth2認證授權與資源服務器操作

用postman工具,設置header的值,通過code獲取access_token與fresh_token:

Springboot開發OAuth2認證授權與資源服務器操作

2)implict方式接獲取access_token。

瀏覽器中輸入:

http://localhost:8080/oauth/authorize?client_id=client&response_type=token&redirect_uri=http://localhost:8090/

可以直接獲得access_token。

Springboot開發OAuth2認證授權與資源服務器操作

3)通過refresh_token獲取access_token與refresh_token。

用postman工具測試,根據refresh_token獲取新的access_token與fresh_token。

Springboot開發OAuth2認證授權與資源服務器操作

4)獲取OAuth2客戶端的信息。

可以通過get方式,也可以通過設置header獲取。

get方式,看url字符串:

Springboot開發OAuth2認證授權與資源服務器操作

設置header的方式:

Springboot開發OAuth2認證授權與資源服務器操作

5)訪問其它受保護的資源

Springboot開發OAuth2認證授權與資源服務器操作

github上的源碼: https://github.com/banat020/OAuth2-server

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
美女毛片一区二区三区四区最新中文字幕亚洲| 国产成人久久| 亚洲精品在线影院| 国产精品丝袜在线播放| 欧美亚洲三级| 鲁大师成人一区二区三区| 影视先锋久久| 99视频精品视频高清免费| 麻豆一区二区99久久久久| 鲁大师影院一区二区三区| 国产一区调教| 成人美女视频| 国产欧美激情| 午夜视频一区二区在线观看| 国产中文一区| 色88888久久久久久影院| 久久99精品久久久野外观看| 伊人久久亚洲| 免费人成黄页网站在线一区二区| 久久99影视| 一区二区三区网站| 精品国产亚洲日本| 老鸭窝亚洲一区二区三区| 日韩高清一级| 香蕉久久久久久| 日韩在线一二三区| 在线 亚洲欧美在线综合一区| 日韩网站中文字幕| 国产资源在线观看入口av| 国产精品扒开腿做爽爽爽软件| 日韩精品社区| 国产日韩欧美在线播放不卡| 日本成人精品| 青青草国产成人99久久| 青青伊人久久| 欧美日韩午夜电影网| 久久精品av麻豆的观看方式| 国产视频一区二| 精品精品久久| 久久久久欧美精品| 国产精品av久久久久久麻豆网| 精精国产xxxx视频在线播放| 国产精品99一区二区| 日韩精品一二三| 啪啪亚洲精品| 久久久久久网| 久久亚洲电影| 国产精品magnet| 国产精品久久久久av电视剧| 欧美专区在线| 麻豆一区二区99久久久久| 亚洲欧洲高清| 在线观看视频免费一区二区三区| 日韩精品视频网站| 中文在线а√天堂| 天堂成人国产精品一区| 国产精品一级| 九九久久婷婷| 国产精品久久亚洲不卡| 久久久久.com| 国产亚洲精品精品国产亚洲综合 | 日韩视频一二区| 国产aⅴ精品一区二区四区| 亚洲va中文在线播放免费| 99国产精品一区二区| 免费成人性网站| 极品日韩av| 成人国产精选| 另类综合日韩欧美亚洲| 亚洲永久精品唐人导航网址| 91精品综合| 欧美黄页在线免费观看| 日韩三级精品| 亚洲天堂日韩在线| 九九综合在线| 日韩亚洲一区在线| 久久免费精品| 免费一级欧美片在线观看网站| 亚洲精品中文字幕99999| 日韩久久视频| 在线手机中文字幕| 欧美激情视频一区二区三区免费| 蜜桃av一区二区| 欧美专区18| 老牛影视一区二区三区| 国产亚洲高清视频| 99成人超碰| 米奇777超碰欧美日韩亚洲| 欧美日韩视频免费观看| 亚洲精品国产嫩草在线观看 | 怡红院精品视频在线观看极品| 91精品国产乱码久久久久久久 | 亚洲精品2区| 久久在线免费| 99视频精品免费观看| 亚洲欧美日本日韩| 热久久国产精品| 亚洲精品激情| 国产精品久久久久久久久免费高清| 欧美一区成人| 久久精品三级| 日本不良网站在线观看| 91精品一区二区三区综合在线爱| 欧美+日本+国产+在线a∨观看| 欧美粗暴jizz性欧美20| 亚洲一区不卡| 免费一区二区视频| 日韩在线黄色| 日本在线不卡视频| 国产精品xvideos88| 婷婷综合成人| 精品国产乱码| 99精品99| 鲁大师精品99久久久| 欧美不卡高清一区二区三区| 亚洲在线网站| 麻豆久久久久久| 国产精品美女久久久| 麻豆久久久久久| 亚洲影院天堂中文av色| 国产一区二区精品福利地址| 久久久影院免费| 日本不卡视频在线| 日本综合字幕| 日本成人在线视频网站| 国产精品字幕| 国产日韩三级| 丝袜脚交一区二区| 国产日韩电影| 国产情侣久久| 午夜在线视频观看日韩17c| 久久久久伊人| 91精品日本| 国产精品毛片在线| 国产精品88久久久久久| 国产亚洲字幕| 国产精品美女| 亚洲91视频| 国产精品xxx| 日韩精品视频在线看| 不卡在线一区二区| 精品久久91| 国产乱码精品| 青青草视频一区| 一区二区电影在线观看| 99在线观看免费视频精品观看| 中文字幕在线免费观看视频| 国产伦理久久久久久妇女| 天堂久久一区| 日韩国产一区二| 日韩制服丝袜先锋影音| 欧美日韩一二三四| 亚洲女同av| 久久久久午夜电影| 免费精品国产| 国产视频一区三区| 日韩午夜电影| 亚洲深夜福利在线观看| 亚洲精品免费观看| 亚洲tv在线| 国产精品一区二区三区四区在线观看 | 国产日韩专区| 久久亚洲二区| 日韩在线电影| 日本综合视频| 国产精品白丝av嫩草影院| 欧美视频久久| 色婷婷狠狠五月综合天色拍| 高清一区二区| 黑丝一区二区三区| 久久在线91| 欧美成人综合| 日韩av中文字幕一区二区| 国产精品久久久久久久久久齐齐| 国产一区二区三区久久| 999久久久精品国产| 亚洲一区二区三区中文字幕在线观看| 日本在线不卡视频一二三区| 国产一区二区三区探花| 黄色在线观看www| 国产亚洲网站| 国产极品一区| 欧美午夜精彩| 91综合久久爱com| 新版的欧美在线视频| 麻豆91精品| 91亚洲国产高清| 亚洲精品九九| 久久久一二三| 国产精品一区2区3区| 婷婷综合在线| 精品一区二区三区的国产在线观看| 色婷婷精品视频| 国产欧美在线| 青青久久av| 免费看一区二区三区| 天堂成人免费av电影一区| 久久精品国产福利| 亚洲精品日本| 伊人久久在线|