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

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

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

瀏覽:133日期: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久久,三上悠亚国产精品一区二区三区
国产专区精品| 国产拍在线视频| 欧美成人a交片免费看| 日本在线不卡视频一二三区| 欧美+日本+国产+在线a∨观看| 麻豆91精品视频| 久久99免费视频| 美女国产精品久久久| 国产精品a久久久久| 国产精品一区亚洲| 国产精品一区二区精品视频观看 | 久久国产婷婷国产香蕉| 日本在线不卡视频一二三区| 玖玖玖国产精品| 免费在线观看不卡| 日韩精品中文字幕一区二区| 日韩激情啪啪| 国产免费av国片精品草莓男男| 国产乱码精品| 日本蜜桃在线观看视频| 激情综合激情| 蜜桃久久久久久| 欧美一级二级三级视频| 久久久91麻豆精品国产一区| 国产h片在线观看| 国产精品日本欧美一区二区三区| 亚洲综合专区| 精品一区二区三区的国产在线观看| 欧美国产不卡| 亚洲成人国产| 日韩有码av| 黄在线观看免费网站ktv| 先锋亚洲精品| 精品一级视频| 久久午夜视频| 激情综合五月| 亚洲一区二区三区无吗| 九九99久久精品在免费线bt| 99热精品在线| 亚洲欧洲高清| 日韩av一二三| 2023国产精品久久久精品双| 国产精品亚洲产品| 国产99精品| 欧美成人精品一级| 视频一区国产视频| 日本不卡免费高清视频在线| 日本不卡视频一二三区| 精品在线99| 9999国产精品| 国产精品成人**免费视频| 日韩亚洲国产欧美| 亚洲成av在线| 国产91在线播放精品| 日本a级不卡| 综合激情视频| 另类激情亚洲| 99国产精品| 久久免费国产| 中文一区一区三区高中清不卡免费| 免费的成人av| 欧美日韩国产亚洲一区| a日韩av网址| 国产精品蜜芽在线观看| 午夜一级久久| 亚洲不卡av不卡一区二区| 久久精品超碰| 日韩精品视频在线看| 国产精品毛片一区二区三区| 日本а中文在线天堂| 麻豆视频一区二区| 日韩av三区| 国产一精品一av一免费爽爽| 中文字幕在线视频久| 国产精品久久久久久久久久齐齐| 亚洲青青久久| 免费视频久久| 午夜亚洲精品| 99久久婷婷| 婷婷综合亚洲| 伊人影院久久| 蜜桃伊人久久| 日本不卡视频一二三区| 色8久久久久| 91精品视频一区二区| 日本aⅴ精品一区二区三区 | 精品午夜久久| 精品久久久网| 天堂а√在线最新版中文在线| 欧美成a人片免费观看久久五月天| 18国产精品| 精品国产乱码久久久久久1区2匹| 欧美日本精品| 国产精品手机在线播放| 日韩av片子| 亚洲国产日韩欧美在线| 亚洲欧美久久久| 日韩毛片一区| 亚洲1234区| 久久久久久免费视频| 神马久久午夜| 久久久久美女| 欧美中文高清| 日韩在线短视频| 亚洲精品动态| 欧美日韩在线观看首页| 在线国产日韩| 亚洲综合电影| 亚洲精品系列| 久久精品电影| 欧美午夜网站| 一本一道久久a久久精品蜜桃| 日韩avvvv在线播放| 国产a久久精品一区二区三区| 九九综合九九| 精品资源在线| 夜夜嗨网站十八久久| 国产成人a视频高清在线观看| 亚洲涩涩av| 色爱av综合网| 精品国产一级| 日韩av一级片| 免费观看在线综合| 色88888久久久久久影院| 欧美日韩一区二区国产| 日韩中文字幕麻豆| 国产综合色区在线观看| 国产日韩高清一区二区三区在线| 精精国产xxxx视频在线野外| 免费看日韩精品| 国产传媒av在线| 亚洲制服一区| 激情久久久久久| 久久av网站| 欧美激情福利| 日韩va亚洲va欧美va久久| 欧洲毛片在线视频免费观看| 精品国产乱码久久久久久1区2匹| 亚洲人成网站在线在线观看| 欧美jjzz| 国内精品福利| 国产精品伊人| 精品黄色一级片| 91精品美女| 亚洲日本三级| 亚洲精品高潮| 国产精品日韩久久久| 精品日韩视频| 在线综合亚洲| 国产高清一区| 99久久久久国产精品| 亚洲天堂1区| 91精品蜜臀一区二区三区在线| 色88888久久久久久影院| 日本不良网站在线观看| av资源新版天堂在线| 福利一区和二区| 成人精品高清在线视频| 成人午夜毛片| 欧美中文一区二区| 欧美日韩少妇| 性欧美69xoxoxoxo| 亚洲在线电影| 亚洲精品无播放器在线播放| 欧美日韩精品一区二区三区在线观看| 日韩精品免费一区二区夜夜嗨| 偷拍亚洲精品| 国产精品久久亚洲不卡| 黄色欧美在线| 久久久久久久久丰满| 99国产精品视频免费观看一公开 | 久久精品亚洲人成影院| 午夜欧美在线| 亚洲免费专区| 97精品一区| 精品国产亚洲一区二区在线观看| 久久精品国产免费| 91精品一区国产高清在线gif| 久久国产欧美| 日本a级不卡| 韩国三级一区| 久久国产成人| 国产精品4hu.www| 日本在线高清| 午夜国产一区二区| 国产精品一区二区精品视频观看 | 国产成人久久| 亚洲一区二区免费在线观看| 国产精品美女在线观看直播| 日韩国产一区二区三区| 99国产精品久久久久久久成人热| 久久国产人妖系列| av亚洲在线观看| 日本久久一区| 好看的亚洲午夜视频在线| 国产精品久久久久久久久久齐齐| 久久久国产精品一区二区中文| 日韩精品视频中文字幕| 亚洲性色视频| 蜜桃视频在线观看一区二区|