日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

介紹

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

OAuth2.0分布式系統(tǒng)環(huán)境搭建

 

這篇文章將通過一個(gè)具體的案例來展示如何搭建一個(gè)分布式的OAuth2.0系統(tǒng)。整體的結(jié)構(gòu)圖如下所示。有網(wǎng)關(guān),認(rèn)證授權(quán)服務(wù)以及資源服務(wù)三個(gè)部分組成。既然OAuth2是一個(gè)標(biāo)準(zhǔn),如果我們想用的話,必然是用它的實(shí)現(xiàn),也就是Spring-Security-OAuth2,它可以很方便地和Spring Cloud集成。OAuth2.0的更多細(xì)節(jié)會(huì)在案例中繼續(xù)介紹。

OAuth2.0分布式系統(tǒng)環(huán)境搭建

 

那么就開始吧!

數(shù)據(jù)庫

要完成這套系統(tǒng),需要準(zhǔn)備好用到的一些數(shù)據(jù)表。

OAuth2.0分布式系統(tǒng)環(huán)境搭建

 

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

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

注冊中心

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

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

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

認(rèn)證授權(quán)服務(wù)

服務(wù)搭建

在OAuth2Demo中創(chuàng)建一個(gè)Module叫oauth2_uaa作為認(rèn)證服務(wù)。添加啟動(dòng)類和配置文件。

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的文章中提到的幾點(diǎn)內(nèi)容

  • 用戶來源的Service實(shí)現(xiàn)UserDetailsService接口,實(shí)現(xiàn)loadUserByUsername()方法,從數(shù)據(jù)庫中獲取數(shù)據(jù)
  • Spring Security的配置類繼承自WebSecurityConfigurerAdapter,重寫里面的兩個(gè)configure()方法
OAuth2.0分布式系統(tǒng)環(huán)境搭建

 

public interface UserService extends UserDetailsService {
}
//-----------------------------------------------------------
@Service("userService")
public class UserServiceImpl implements UserService {
	…………
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = userMapper.findByUsername(username);
        return sysUser;
    }
}
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	…………
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    //認(rèn)證用戶的來源
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
    }

    //配置SpringSecurity相關(guān)信息
    @Override
    protected 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的配置類,第一個(gè)configure()方法配置的是用戶的來源,這里配置了自定義的實(shí)現(xiàn)了UserDetailsService接口的UserService,里面的loadUserByUsername()方法從數(shù)據(jù)庫中查詢出對應(yīng)的實(shí)現(xiàn)了UserDetails接口的SysUser對象,里面的SysPermission封裝了用戶所擁有的權(quán)限。然后就交給后續(xù)的過濾器去處理了,我們就不用去管了。

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

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;    //從WebSecurityConfig中獲取的

    @Autowired
    private AuthorizationCodeServices authorizationCodeServices;    //本類中的,授權(quán)碼模式需要

    @Autowired
    private TokenStore tokenStore;  //TokenConfig中的

    @Autowired
    private PasswordEncoder passwordEncoder;//從WebSecurityConfig中獲取的

    @Autowired
    private ClientDetailsService clientDetailsService;   //本類中的

    @Autowired
    private JwtAccessTokenConverter jwtAccessTokenConverter;    //TokenConfig中的

    //用來配置令牌端點(diǎn)的安全約束
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.tokenKeyAccess("permitAll")    // /oauth/token_key 提供公有密匙的端點(diǎn) 允許任何人訪問
                .checkTokenAccess("permitAll")  // /oauth/check_token :用于資源服務(wù)訪問的令牌解析端點(diǎn) 允許任何人訪問
                .allowFormAuthenticationForClients();   //表單認(rèn)證(申請令牌)
    }

    //用來配置客戶端詳情服務(wù),客戶端詳情信息在這里進(jìn)行初始化,
    //你能夠把客戶端詳情信息寫死在這里或者是通過數(shù)據(jù)庫來存儲(chǔ)調(diào)取詳情信息
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.withClientDetails(clientDetailsService);
    }

    //用來配置令牌(token)的訪問端點(diǎn)(url)和令牌服務(wù)(token services)
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)  //認(rèn)證管理器,密碼模式需要
                .authorizationCodeServices(authorizationCodeServices)   //授權(quán)碼服務(wù),授權(quán)碼模式需要
                .tokenServices(tokenService())
                .allowedTokenEndpointRequestMethods(HttpMethod.POST);   //允許post提交
    }

    @Bean
    public AuthorizationCodeServices authorizationCodeServices(DataSource dataSource) {
        //設(shè)置授權(quán)碼模式的授權(quán)碼存取到數(shù)據(jù)中
        return new JdbcAuthorizationCodeServices(dataSource);
    }

    //客戶端詳情服務(wù),從數(shù)據(jù)庫中獲取
    @Bean
    public ClientDetailsService clientDetailsService(DataSource dataSource) {
        ClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);
        ((JdbcClientDetailsService)clientDetailsService).setPasswordEncoder(passwordEncoder);
        return clientDetailsService;
    }

    //令牌管理服務(wù)
    @Bean
    public AuthorizationServerTokenServices tokenService() {
        DefaultTokenServices service = new DefaultTokenServices();
        service.setClientDetailsService(clientDetailsService);  //客戶端信息服務(wù)
        service.setSupportRefreshToken(true);               //支持自動(dòng)刷新
        service.setTokenStore(tokenStore);
        //令牌增強(qiáng)
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter));
        service.setTokenEnhancer(tokenEnhancerChain);

        service.setAccessTokenValiditySeconds(7200);        //令牌默認(rèn)有效期2小時(shí)
        service.setRefreshTokenValiditySeconds(259200);     //刷新令牌默認(rèn)有效期3天
        return service;
    }
}

現(xiàn)在來解釋一下上面代碼中的內(nèi)容

  • ClientDetailsService我們配置了從數(shù)據(jù)庫中獲取客戶端配置。但是是怎么從數(shù)據(jù)庫中獲取的呢,這里用到了一個(gè)JdbcClientDetailsService,點(diǎn)擊源碼里看看
OAuth2.0分布式系統(tǒng)環(huán)境搭建

 

可以看到,它是從 oauth_client_details 這張表里查出來的,所以我們的數(shù)據(jù)庫中只要?jiǎng)?chuàng)建出這張表,表里再添加這些字段即可。

  • JdbcAuthorizationCodeServices原理和JdbcClientDetailsService差不多,都是創(chuàng)建出指定的表。
  • TokenStore 和 JwtAccessTokenConverter為了方便管理,我們使用TokenConfig這個(gè)類去配置Token相關(guān)的內(nèi)容。添加了@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); //對稱秘鑰,資源服務(wù)器使用該秘鑰來驗(yàn)證 return converter; } } 采用了JWT令牌管理方式,然后使用了對稱密鑰去進(jìn)行加密。還有另外幾種令牌管理方式:InMemoryTokenStore:在內(nèi)存中存儲(chǔ)令牌(默認(rèn))JdbcTokenStore:令牌存儲(chǔ)在數(shù)據(jù)庫中redisTokenStore:令牌存儲(chǔ)在Redis中
  • AuthorizationServerTokenServices這個(gè)是用來配置令牌管理服務(wù)的,我們配置了客戶端詳情服務(wù),令牌增強(qiáng)等內(nèi)容。

申請令牌的四種方式

到現(xiàn)在為止,我們的認(rèn)證授權(quán)服務(wù)就已經(jīng)配置好了,那么現(xiàn)在就可以去申請令牌了,申請令牌的方式一共有四種:

  • 授權(quán)碼模式第一步申請授權(quán)碼http://localhost:8001/uaa/oauth/authorize?client_id=c1&response_type=code&scope=ROLE_ADMIN&redirect_uri=http://localhost注意,這里的client_idscoperedirect_uri都是在oauth_client_details表中設(shè)置過的,要一一對應(yīng)上,否則不行,response_type授權(quán)碼模式固定為code。成功訪問后,在頁面上輸入用戶名和密碼,驗(yàn)證通過后,在瀏覽器的地址欄中就可以看到返回的授權(quán)碼。然后我們拿著授權(quán)碼就可以向服務(wù)器去申請Token了,參數(shù)列表必須和數(shù)據(jù)庫中配置的一致。
  • 簡化模式http://localhost:8001/uaa/oauth/authorize?client_id=c1&response_type=token&scope=ROLE_ADMIN&redirect_uri=http://localhost在簡化模式下,我們只需要去指定client_idresponse_typescoperedirect_uri即可,請求成功后,就會(huì)跳轉(zhuǎn)到指定的uri界面,然后令牌就在url中。
  • 密碼模式在密碼模式下,我們需要將用戶名和密碼傳到服務(wù)器中,驗(yàn)證通過后,服務(wù)器會(huì)直接將Token返回給我們
  • 客戶端模式該模式最簡單,也是最不安全的。

網(wǎng)關(guān)

搭建完了認(rèn)證授權(quán)服務(wù)再來創(chuàng)建網(wǎng)關(guān)服務(wù)。在父工程下創(chuàng)建一個(gè)名為oauth2_gateway的Module。啟動(dòng)類沒什么好說的,配置文件中有幾點(diǎn)需要注意:

spring.application.name=gateway
server.port=8010

zuul.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/
…………

我們配置了微服務(wù)的名稱及端口,還配置了將路徑為/zuul/uaa/**和 /zuul/order/**的請求轉(zhuǎn)發(fā)給uaa和order微服務(wù)。

老樣子,第一步進(jìn)行一些安全配置

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/**").permitAll()
                .and().csrf().disable();
    }

}

我們在這里設(shè)置了可以接收任何請求,不需要任何的權(quán)限。

接下來就需要對具體的資源服務(wù)進(jìn)行配置:

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    public static final String RESOURCE_ID = "res1";

    @Autowired
    private TokenStore tokenStore;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.tokenStore(tokenStore)
                .resourceId(RESOURCE_ID)
                .stateless(true);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/uaa/**")
                .permitAll()
                .antMatchers("/order/**")
                .access("#oauth2.hasScope('ROLE_API')");
    }

}

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

因?yàn)橛唵挝⒎?wù)還沒有創(chuàng)建,所以我們來測試一下網(wǎng)關(guān)訪問認(rèn)證授權(quán)服務(wù)。網(wǎng)關(guān)的端口是8010。

來測試一下,先是通過網(wǎng)關(guān)獲取令牌,網(wǎng)關(guān)微服務(wù)的端口是8010。

OAuth2.0分布式系統(tǒng)環(huán)境搭建

 

可以看到,申請到了令牌,說明請求成功地被轉(zhuǎn)發(fā)到了認(rèn)證服務(wù)。

訂單資源服務(wù)

最后,我們就可以去創(chuàng)建資源服務(wù)了。在父工程下創(chuàng)建一個(gè)名為oauth2_order的Module。

第一步,先進(jìn)行一些安全配置:

@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/r/**").authenticated()   //所有/r/**的請求必須認(rèn)證通過
                .anyRequest().permitAll();  //除了/r/**,其它的請求可以訪問
    }

}

這個(gè)@EnableGlobalMethodSecurity是干嗎的呢?是為了開啟注解權(quán)限控制的,只有開啟了之后,我們才可以在需要進(jìn)行權(quán)限控制的地方去添加注解實(shí)現(xiàn)權(quán)限控制。

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

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    public static final String RESOURCE_ID = "res1";    //資源服務(wù)的id

    @Autowired
    private TokenStore tokenStore;  //管理令牌的方式,TokenConfig中的

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId(RESOURCE_ID)
                .tokenStore(tokenStore)
                .stateless(true);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/**").access("#oauth2.hasScope('ROLE_ADMIN')")
                .and().csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

}

接下來就是在需要進(jìn)行權(quán)限控制的方法上面添加注解。

@RestController
public class OrderController {

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

}

ok!成功了。再來試一下通過網(wǎng)關(guān)去訪問order中的資源,用一個(gè)沒有權(quán)限的用戶訪問試試。

OAuth2.0分布式系統(tǒng)環(huán)境搭建

 

說明網(wǎng)關(guān)成功轉(zhuǎn)發(fā)了我們請求,并且我們配置的權(quán)限控制也起了作用。

總結(jié)

使用OAuth2.0搭建分布式系統(tǒng)到這里就結(jié)束了。內(nèi)容還是挺多的,希望小伙伴們能有靜下心來細(xì)品。因?yàn)榭紤]到篇幅,很多非核心的內(nèi)容我都沒有貼出來,比如pom文件,配置文件的部分內(nèi)容等。小伙伴們可以下載源碼再配合著這篇文章看。

分享到:
標(biāo)簽:OAuth2
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定