国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Spring cloud oauth2如何搭建認(rèn)證資源中心

瀏覽:5日期:2023-08-02 14:53:09

一 認(rèn)證中心搭建

添加依賴,如果使用spring cloud的話,不管哪個(gè)服務(wù)都只需要這一個(gè)封裝好的依賴即可

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency>

配置spring security

/** * security配置類 */@Configuration@EnableWebSecurity //開啟web保護(hù)@EnableGlobalMethodSecurity(prePostEnabled = true) // 開啟方法注解權(quán)限配置public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Qualifier('userDetailsServiceImpl') @Autowired private UserDetailsService userDetailsService; //配置用戶簽名服務(wù),賦予用戶權(quán)限等 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService)//指定userDetailsService實(shí)現(xiàn)類去對應(yīng)方法認(rèn).passwordEncoder(passwordEncoder()); //指定密碼加密器 } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } //配置攔截保護(hù)請求,什么請求放行,什么請求需要驗(yàn)證 @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests()//配置所有請求開啟認(rèn)證.anyRequest().permitAll().and().httpBasic(); //啟用http基礎(chǔ)驗(yàn)證 } // 配置token驗(yàn)證管理的Bean @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }}

配置OAuth2認(rèn)證中心

/** * OAuth2授權(quán)服務(wù)器 */@EnableAuthorizationServer //聲明OAuth2認(rèn)證中心@Configurationpublic class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired @Qualifier('authenticationManagerBean') private AuthenticationManager authenticationManager; @Autowired private DataSource dataSource; @Autowired private UserDetailsService userDetailsService; @Autowired private PasswordEncoder passwordEncoder; /** * 這個(gè)方法主要是用于校驗(yàn)注冊的第三方客戶端的信息,可以存儲在數(shù)據(jù)庫中,默認(rèn)方式是存儲在內(nèi)存中,如下所示,注釋掉的代碼即為內(nèi)存中存儲的方式 */ @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception{clients.inMemory().withClient('hou') // 客戶端id,必須有.secret(passwordEncoder.encode('123456')) // 客戶端密碼 .scopes('server').authorizedGrantTypes('authorization_code', 'password', 'refresh_token') //驗(yàn)證類型.redirectUris('http://www.baidu.com');/*redirectUris 關(guān)于這個(gè)配置項(xiàng),是在 OAuth2協(xié)議中,認(rèn)證成功后的回調(diào)地址,此值同樣可以配置多個(gè)*/ //數(shù)據(jù)庫配置,需要建表// clients.withClientDetails(clientDetailsService());// clients.jdbc(dataSource); } // 聲明 ClientDetails實(shí)現(xiàn) private ClientDetailsService clientDetailsService() { return new JdbcClientDetailsService(dataSource); } /** * 控制token端點(diǎn)信息 */ @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore()).userDetailsService(userDetailsService); } //獲取token存儲類型 @Bean public TokenStore tokenStore() { //return new JdbcTokenStore(dataSource); //存儲mysql中 return new InMemoryTokenStore(); //存儲內(nèi)存中 //new RedisTokenStore(connectionFactory); //存儲redis中 } //配置獲取token策略和檢查策略 @Override public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.tokenKeyAccess('permitAll()') //獲取token請求不進(jìn)行攔截.checkTokenAccess('isAuthenticated()') //驗(yàn)證通過返回token信息.allowFormAuthenticationForClients(); // 允許 客戶端使用client_id和client_secret獲取token }}

二 測試獲取Token

默認(rèn)獲取token接口圖中2所示,這里要說明一點(diǎn),參數(shù)key千萬不能有空格,尤其是client_這兩個(gè)

Spring cloud oauth2如何搭建認(rèn)證資源中心

三 需要保護(hù)的資源服務(wù)配置

yml配置客戶端信息以及認(rèn)中心地址

security: oauth2: resource: tokenInfoUri: http://localhost:9099/oauth/check_token preferTokenInfo: true client: client-id: hou client-secret: 123456 grant-type: password scope: server access-token-uri: http://localhost:9099/oauth/token

配置認(rèn)證中心地址即可

/** * 資源中心配置 */@Configuration@EnableResourceServer // 聲明資源服務(wù),即可開啟token驗(yàn)證保護(hù)@EnableGlobalMethodSecurity(prePostEnabled = true) // 開啟方法權(quán)限注解public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests()//配置所有請求不需要認(rèn)證,在方法用注解定制權(quán)限.anyRequest().permitAll(); }}

編寫權(quán)限控制

@RestController@RequestMapping('test')public class TestController { //不需要權(quán)限 @GetMapping('/hou') public String test01(){ return '返回測試數(shù)據(jù)hou'; } @PreAuthorize('hasAnyAuthority(’ROLE_USER’)') //需要權(quán)限 @GetMapping('/zheng') public String test02(){ return '返回測試數(shù)據(jù)zheng'; }}

四 測試權(quán)限

不使用token

Spring cloud oauth2如何搭建認(rèn)證資源中心

使用token

Spring cloud oauth2如何搭建認(rèn)證資源中心

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 在线观看a网站 | 萌白酱国产一区 | 在线欧美精品二区三区 | 欧美一级二级三级视频 | 欧洲免费无线码二区5 | 久久免费小视频 | 久久久国产在线 | 色爽爽爽爽爽爽爽爽 | 国产91会所洗浴女技师按摩 | 国产一区二区三区免费播放 | 神马国产 | 欧美福利一区二区三区 | 在线视频一区二区三区四区 | 亚洲欧美另类在线视频 | 中国人免费观看高清在线观看二区 | 亚洲a在线视频 | 久久亚洲国产午夜精品理论片 | 最近日本免费观看视频 | 国产成人亚洲综合网站不卡 | 国产亚洲精品美女一区二区 | 综合刺激网 | 一色屋成人免费精品网站 | 国产一级内谢a级高清毛片 国产一级片毛片 | 日韩一级视频在线观看播放 | 狠狠色狠狠色综合久久第一次 | 一级做a爰片久久毛片免费看 | 国产成人精品一区二区免费 | 18video9ex欧美生活片 | 欧美日韩免费播放一区二区 | 日韩一区二区三 | 日本一在线中文字幕天堂 | 国产毛片久久国产 | 日韩在线第一区 | 亚洲欧洲无码一区二区三区 | 国产欧美自拍 | 久久国产精品高清一区二区三区 | 午夜欧美精品久久久久久久久 | 成人免费视频网站 | 国产日韩高清一区二区三区 | 国产在线观看xxxx免费 | 国产成人a视频在线观看 |