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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

认证服务器的搭建_OAuth2.0分布式系统环境搭建

發布時間:2025/3/12 windows 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 认证服务器的搭建_OAuth2.0分布式系统环境搭建 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

好好學習,天天向上
本文已收錄至我的Github倉庫DayDayUP:http://github.com/RobodLee/DayDayUP,歡迎Star,更多文章請前往:目錄導航

介紹

OAuth(開放授權)是一個開放標準,允許用戶授權第三方應用訪問他們存儲在另外的服務提供者上的信息,而不需要將用戶名和密碼提供給第三方應用或分享他們數據的所有內容。OAuth2.0的系統大致分由客戶端,認證授權服務器以及資源服務器三部分組成。客戶端如果想要訪問資源服務器中的資源,就必須要持有認證授權服務器頒發的Token。認證流程如下圖所示:

這篇文章將通過一個具體的案例來展示如何搭建一個分布式的OAuth2.0系統。整體的結構圖如下所示。有網關,認證授權服務以及資源服務三個部分組成。既然OAuth2是一個標準,如果我們想用的話,必然是用它的實現,也就是Spring-Security-OAuth2,它可以很方便地和Spring Cloud集成。OAuth2.0的更多細節會在案例中繼續介紹。

那么就開始吧!

數據庫

要完成這套系統,需要準備好用到的一些數據表。

  • oauth_client_details:這個數據庫存放了客戶端的配置信息,客戶端有什么樣的權限才可以訪問服務器。表中的字段是固定的,下面會詳細提到。
  • oauth_code:用戶數據庫存取授權碼模式存放授權碼的,表中的字段也是固定的,下面會詳細說明。
  • 后面的5張表存放了用戶的一些信息,如果角色、權限等信息。登錄驗證的時候需要。

建表的sql我放在了源碼的README.md文件中,下載地址見文末。

注冊中心

微服務項目得先有個注冊中心吧,我們選用Eureka。先搭建一個父工程OAuth2Demo,然后在父工程中創建一個Module叫oauth2_eureka。然后添加配置文件及啟動類即可。所需要的依賴我就不在這里貼了,太占篇幅了。有需要的小伙伴直接去我源碼中拷就行了。

spring:application:name: eureka server:port: 8000 #啟動端口 …………@SpringBootApplication @EnableEurekaServer public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class,args);} }

這樣注冊中心就搭建好了。

認證授權服務

服務搭建

在OAuth2Demo中創建一個Module叫oauth2_uaa作為認證服務。添加啟動類和配置文件。

spring.application.name=uaa server.port=8001 eureka.client.serviceUrl.defaultZone = http://localhost:8000/eureka/ ………… @SpringBootApplication @EnableEurekaClient @MapperScan("com.robod.uaa.mapper") public class UaaApplication {public static void main(String[] args) {SpringApplication.run(UaaApplication.class, args);} }

配置

回顧上一篇Spring Security的文章中提到的幾點內容

  • 用戶來源的Service實現UserDetailsService接口,實現loadUserByUsername()方法,從數據庫中獲取數據
  • Spring Security的配置類繼承自WebSecurityConfigurerAdapter,重寫里面的兩個configure()方法

public interface UserService extends UserDetailsService { } //----------------------------------------------------------- @Service("userService") public class UserServiceImpl implements UserService {…………@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {SysUser sysUser = userMapper.findByUsername(username);return sysUser;} }@Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter {…………@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Bean@Overrideprotected AuthenticationManager authenticationManager() throws Exception {return super.authenticationManager();}//認證用戶的來源@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userService).passwordEncoder(passwordEncoder());}//配置SpringSecurity相關信息@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/r/r1").hasAnyAuthority("p1").antMatchers("/login*").permitAll().anyRequest().authenticated().and().formLogin();}}

解釋一下上面的代碼,WebSecurityConfig是Spring Security的配置類,第一個configure()方法配置的是用戶的來源,這里配置了自定義的實現了UserDetailsService接口的UserService,里面的loadUserByUsername()方法從數據庫中查詢出對應的實現了UserDetails接口的SysUser對象,里面的SysPermission封裝了用戶所擁有的權限。然后就交給后續的過濾器去處理了,我們就不用去管了。

然后我們就可以去進行OAuth2.0的相關配置了,方法很簡單,只要在配置類上添加@EnableAuthorizationServer注解并讓其繼承自AuthorizationServerConfigurerAdapter。最后重寫其中的三個configure()方法即可。

@Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {@Autowiredprivate AuthenticationManager authenticationManager; //從WebSecurityConfig中獲取的@Autowiredprivate AuthorizationCodeServices authorizationCodeServices; //本類中的,授權碼模式需要@Autowiredprivate TokenStore tokenStore; //TokenConfig中的@Autowiredprivate PasswordEncoder passwordEncoder;//從WebSecurityConfig中獲取的@Autowiredprivate ClientDetailsService clientDetailsService; //本類中的@Autowiredprivate JwtAccessTokenConverter jwtAccessTokenConverter; //TokenConfig中的//用來配置令牌端點的安全約束@Overridepublic void configure(AuthorizationServerSecurityConfigurer security) throws Exception {security.tokenKeyAccess("permitAll") // /oauth/token_key 提供公有密匙的端點 允許任何人訪問.checkTokenAccess("permitAll") // /oauth/check_token :用于資源服務訪問的令牌解析端點 允許任何人訪問.allowFormAuthenticationForClients(); //表單認證(申請令牌)}//用來配置客戶端詳情服務,客戶端詳情信息在這里進行初始化,//你能夠把客戶端詳情信息寫死在這里或者是通過數據庫來存儲調取詳情信息@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {clients.withClientDetails(clientDetailsService);}//用來配置令牌(token)的訪問端點(url)和令牌服務(token services)@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {endpoints.authenticationManager(authenticationManager) //認證管理器,密碼模式需要.authorizationCodeServices(authorizationCodeServices) //授權碼服務,授權碼模式需要.tokenServices(tokenService()).allowedTokenEndpointRequestMethods(HttpMethod.POST); //允許post提交}@Beanpublic AuthorizationCodeServices authorizationCodeServices(DataSource dataSource) {//設置授權碼模式的授權碼存取到數據中return new JdbcAuthorizationCodeServices(dataSource);}//客戶端詳情服務,從數據庫中獲取@Beanpublic ClientDetailsService clientDetailsService(DataSource dataSource) {ClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);((JdbcClientDetailsService)clientDetailsService).setPasswordEncoder(passwordEncoder);return clientDetailsService;}//令牌管理服務@Beanpublic AuthorizationServerTokenServices tokenService() {DefaultTokenServices service = new DefaultTokenServices();service.setClientDetailsService(clientDetailsService); //客戶端信息服務service.setSupportRefreshToken(true); //支持自動刷新service.setTokenStore(tokenStore);//令牌增強TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();tokenEnhancerChain.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter));service.setTokenEnhancer(tokenEnhancerChain);service.setAccessTokenValiditySeconds(7200); //令牌默認有效期2小時service.setRefreshTokenValiditySeconds(259200); //刷新令牌默認有效期3天return service;} }

現在來解釋一下上面代碼中的內容

  • ClientDetailsService
    我們配置了從數據庫中獲取客戶端配置。但是是怎么從數據庫中獲取的呢,這里用到了一個JdbcClientDetailsService,點擊源碼里看看

可以看到,它是從 oauth_client_details 這張表里查出來的,所以我們的數據庫中只要創建出這張表,表里再添加這些字段即可。

  • JdbcAuthorizationCodeServices
    原理和JdbcClientDetailsService差不多,都是創建出指定的表。
  • TokenStore 和 JwtAccessTokenConverter
    為了方便管理,我們使用TokenConfig這個類去配置Token相關的內容。添加了**@Bean**注解將其添加到Spring容器后就可以在其它的類中去注入使用了。
    @Configuration
    public class TokenConfig {
    private String SIGNING_KEY = "robod_hahaha"; //對稱加密的密鑰
    @Bean
    public TokenStore tokenStore() {//JWT令牌方案
    return new JwtTokenStore(jwtAccessTokenConverter());
    }
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setSigningKey(SIGNING_KEY); //對稱秘鑰,資源服務器使用該秘鑰來驗證
    return converter;
    }
    }
    采用了JWT令牌管理方式,然后使用了對稱密鑰去進行加密。還有另外幾種令牌管理方式:
    • InMemoryTokenStore:在內存中存儲令牌(默認)
    • JdbcTokenStore:令牌存儲在數據庫中
    • RedisTokenStore:令牌存儲在Redis中
  • AuthorizationServerTokenServices
    這個是用來配置令牌管理服務的,我們配置了客戶端詳情服務,令牌增強等內容。

申請令牌的四種方式

到現在為止,我們的認證授權服務就已經配置好了,那么現在就可以去申請令牌了,申請令牌的方式一共有四種:

  • 授權碼模式

第一步申請授權碼
http://localhost:8001/uaa/oauth/authorize?client_id=c1&response_type=code&scope=ROLE_ADMIN&redirect_uri=http://localhost
注意,這里的client_id,scope和redirect_uri都是在oauth_client_details表中設置過的,要一一對應上,否則不行,response_type授權碼模式固定為code。成功訪問后,在頁面上輸入用戶名和密碼,驗證通過后,在瀏覽器的地址欄中就可以看到返回的授權碼。

然后我們拿著授權碼就可以向服務器去申請Token了,參數列表必須和數據庫中配置的一致。

  • 簡化模式
    http://localhost:8001/uaa/oauth/authorize?client_id=c1&response_type=token&scope=ROLE_ADMIN&redirect_uri=http://localhost
    在簡化模式下,我們只需要去指定client_id,response_type,scope和redirect_uri即可,請求成功后,就會跳轉到指定的uri界面,然后令牌就在url中。

  • 密碼模式
    在密碼模式下,我們需要將用戶名和密碼傳到服務器中,驗證通過后,服務器會直接將Token返回給我們

  • 客戶端模式
    該模式最簡單,也是最不安全的。

網關

搭建完了認證授權服務再來創建網關服務。在父工程下創建一個名為oauth2_gateway的Module。啟動類沒什么好說的,配置文件中有幾點需要注意:

spring.application.name=gateway server.port=8010zuul.routes.uaa.stripPrefix = false zuul.routes.uaa.path = /uaa/**zuul.routes.order.stripPrefix = false zuul.routes.order.path = /order/**eureka.client.serviceUrl.defaultZone = http://localhost:8000/eureka/ …………

我們配置了微服務的名稱及端口,還配置了將路徑為/zuul/uaa/**和 /zuul/order/**的請求轉發給uaa和order微服務。

老樣子,第一步進行一些安全配置

@Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/**").permitAll().and().csrf().disable();}}

我們在這里設置了可以接收任何請求,不需要任何的權限。

接下來就需要對具體的資源服務進行配置:

@Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter {public static final String RESOURCE_ID = "res1";@Autowiredprivate TokenStore tokenStore;@Overridepublic void configure(ResourceServerSecurityConfigurer resources) {resources.tokenStore(tokenStore).resourceId(RESOURCE_ID).stateless(true);}@Overridepublic void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/uaa/**").permitAll().antMatchers("/order/**").access("#oauth2.hasScope('ROLE_API')");}}

在這里面,配置了訪問認證服務不需要任何的權限。訪問訂單資源服務需要用戶必須具有 **“ROLE_API”**的scope權限。其中注入的tokenStore和認證服務中的TokenConfig一致。

因為訂單微服務還沒有創建,所以我們來測試一下網關訪問認證授權服務。網關的端口是8010。

來測試一下,先是通過網關獲取令牌,網關微服務的端口是8010。

可以看到,申請到了令牌,說明請求成功地被轉發到了認證服務。

訂單資源服務

最后,我們就可以去創建資源服務了。在父工程下創建一個名為oauth2_order的Module。

第一步,先進行一些安全配置:

@Configuration @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/r/**").authenticated() //所有/r/**的請求必須認證通過.anyRequest().permitAll(); //除了/r/**,其它的請求可以訪問}}

這個@EnableGlobalMethodSecurity是干嗎的呢?是為了開啟注解權限控制的,只有開啟了之后,我們才可以在需要進行權限控制的地方去添加注解實現權限控制。

接下來就是對資源服務器的配置了。在@Configuration注解的配置類上添加**@EnableResourceServer注解,然后繼承自ResourceServerConfigurerAdapter類,然后重寫里面的configure()**方法即可。

@Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter {public static final String RESOURCE_ID = "res1"; //資源服務的id@Autowiredprivate TokenStore tokenStore; //管理令牌的方式,TokenConfig中的@Overridepublic void configure(ResourceServerSecurityConfigurer resources) throws Exception {resources.resourceId(RESOURCE_ID).tokenStore(tokenStore).stateless(true);}@Overridepublic void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/**").access("#oauth2.hasScope('ROLE_ADMIN')").and().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);}}

接下來就是在需要進行權限控制的方法上面添加注解。

@RestController public class OrderController {@GetMapping(value = "/r1")@PreAuthorize("hasAuthority('p1')")//擁有p1權限方可訪問此urlpublic String r1() {return "訪問資源成功";}}

ok!成功了。再來試一下通過網關去訪問order中的資源,用一個沒有權限的用戶訪問試試。

說明網關成功轉發了我們請求,并且我們配置的權限控制也起了作用。

總結

使用OAuth2.0搭建分布式系統到這里就結束了。內容還是挺多的,希望小伙伴們能有靜下心來細品。因為考慮到篇幅,很多非核心的內容我都沒有貼出來,比如pom文件,配置文件的部分內容等。小伙伴們可以下載源碼再配合著這篇文章看。

點擊下載代碼

碼字不易,看完請點贊!點贊!點贊!

要是有什么好的意見歡迎在下方留言。讓我們下期再見!

DayDayUP?github.com

總結

以上是生活随笔為你收集整理的认证服务器的搭建_OAuth2.0分布式系统环境搭建的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。