日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring-security-oauth2实现OAuth2.0服务

發(fā)布時間:2024/9/30 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring-security-oauth2实现OAuth2.0服务 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

關(guān)于OAuth的介紹查看我的另一篇文章OAuth的4種授權(quán)方式,spring-security-oauth2是實現(xiàn)OAuth2.0的框架,配置稍微有些繁瑣,因此本文記錄下大概的思路,加深印象。

OAuth 2.0中主要有Authorization Service授權(quán)服務(wù)和Resource Service資源服務(wù),他們可以在同一個應(yīng)用程序中,也可以在兩個應(yīng)用程序中,甚至多個資源服務(wù)共享一個授權(quán)服務(wù)。

spring-security提供了相應(yīng)的endpoints來管理token的請求,/oauth/authorize端點負責(zé)授權(quán)服務(wù),/oauth/token端點負責(zé)token的請求服務(wù);資源服務(wù)中的過濾器OAuth2AuthenticationProcessingFilter 負責(zé)校驗Token。

下面從總體上介紹授權(quán)服務(wù)和資源服務(wù)的主要配置,詳細的配置在githubspring-security-oauth demo上。

授權(quán)服務(wù)配置

@Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {private static final String DEMO_RESOURCE_ID = "openapi";@Autowiredprivate OrderAuthProperties orderAuthProperties;/*** 注意這里AuthenticationManager和UserAccountService* 是在SecurityConfiguration配置的,把倆個配置類關(guān)聯(lián)了起來*/@AutowiredAuthenticationManager authenticationManager;@Autowiredprivate UserAccountService userAccountService;@Autowiredprivate AuthorizationCodeServices authorizationCodeServices;@AutowiredRedisConnectionFactory redisConnectionFactory;@Autowiredprivate OAuthClientDetailsService oAuthClientDetailsService;@Autowiredprivate DataSource dataSource;/*** 配置第三方客戶端的信息,可以從內(nèi)存中加載,也可以從數(shù)據(jù)庫加載(更常用)*/@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.jdbc(dataSource);clients.withClientDetails(oAuthClientDetailsService);// 這里將第三方客戶端放到內(nèi)存里,配置兩個客戶端,一個用于client認證 // clients.inMemory() // .withClient("client_1") // .resourceIds(DEMO_RESOURCE_ID) // .authorizedGrantTypes("client_credentials", "refresh_token") // .scopes("select") // .authorities("client") // .secret("123456") // .and().withClient("client_2") // .secret("123456") // .resourceIds(DEMO_RESOURCE_ID) // .authorizedGrantTypes("authorization_code", "code", "password", "refresh_token") // .scopes("select") // .redirectUris("http://www.baidu.com");}/*** 定義token endpoint的安全配置*/@Overridepublic void configure(AuthorizationServerSecurityConfigurer oauthServer) {oauthServer.realm("oauth2-resources").tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()").passwordEncoder(clientPasswordEncoder()).allowFormAuthenticationForClients();}/*** 第三方客戶端使用加密方式*/@Beanpublic PasswordEncoder clientPasswordEncoder() {// 不做加密處理,明文存儲return NoOpPasswordEncoder.getInstance();}/*** 定義授權(quán)、token endpoint和token服務(wù),即如何生成token和token存儲在哪里(內(nèi)存/數(shù)據(jù)庫/JWT)** <p>* 這里最重要的就是DefaultTokenServices,默認是生成隨機值作為token* <p>*/@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) {// 這里設(shè)置了兩個TokenEnhancer,可改變token值,它會在token生成后/保存前調(diào)用TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));endpoints.authenticationManager(authenticationManager) // 注入authenticationManager開啟密碼授權(quán)模式// 必須要配置userDetailsService,才支持refresh token grant,to ensure that the account is still active// 這里和SecurityConfiguration的userAccountService也可以不一樣,為什么?.userDetailsService(userAccountService).authorizationCodeServices(authorizationCodeServices) // 定義authorizationCodeServices支持auth code grant..tokenStore(tokenStore()).tokenEnhancer(tokenEnhancerChain);}/*** TokenEnhancer** JWT提供的,幫助把OAuth認證信息轉(zhuǎn)為JWT,即access_token,它返回的很多默認字段(jti,ati)都是在這里定義的*/@Beanpublic JwtAccessTokenConverter accessTokenConverter() {JwtAccessTokenConverter converter = new JwtAccessTokenConverter();// 這里使用的密鑰也要定義在資源服務(wù)里,以便資源服務(wù)也可以校驗token,否則認證服務(wù)就要提供校驗token的接口給資源服務(wù)converter.setSigningKey(orderAuthProperties.getOauthJwtSecret());return converter;}/*** TokenEnhancer** 向token里添加自定義信息*/@Beanpublic JwtTokenEnhancer tokenEnhancer() {return new JwtTokenEnhancer();}/*** 定義token的存儲方式:可以放在redis/數(shù)據(jù)庫(oauth_access_token表)/內(nèi)存,或者jwt中* 這里放在JWT里,根本就不必后端存儲token了,這是JWT很大的優(yōu)勢** 但是JWT也有缺點,1.不容易撤銷授權(quán),所以一般令牌時效性很短,撤銷授權(quán)可以在刷新時實現(xiàn),怎么實現(xiàn)?* 2.如果要存儲的信息很多,令牌會變得很大*/@Beanpublic TokenStore tokenStore() {return new JwtTokenStore(accessTokenConverter());} }

總結(jié)

以上是生活随笔為你收集整理的spring-security-oauth2实现OAuth2.0服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。