最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年11月16日 11:14:19   作者:侯賽雷  
這篇文章主要介紹了Spring cloud oauth2如何搭建認(rèn)證資源中心,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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

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

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

配置spring security

/**
 * security配置類(lèi)
 */
@Configuration
@EnableWebSecurity //開(kāi)啟web保護(hù)
@EnableGlobalMethodSecurity(prePostEnabled = true) // 開(kāi)啟方法注解權(quán)限配置
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Qualifier("userDetailsServiceImpl")
  @Autowired
  private UserDetailsService userDetailsService;

  //配置用戶(hù)簽名服務(wù),賦予用戶(hù)權(quán)限等
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService)//指定userDetailsService實(shí)現(xiàn)類(lèi)去對(duì)應(yīng)方法認(rèn)
        .passwordEncoder(passwordEncoder()); //指定密碼加密器
  }
  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }
  //配置攔截保護(hù)請(qǐng)求,什么請(qǐng)求放行,什么請(qǐng)求需要驗(yàn)證
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        //配置所有請(qǐng)求開(kāi)啟認(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)證中心
@Configuration
public 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)注冊(cè)的第三方客戶(hù)端的信息,可以存儲(chǔ)在數(shù)據(jù)庫(kù)中,默認(rèn)方式是存儲(chǔ)在內(nèi)存中,如下所示,注釋掉的代碼即為內(nèi)存中存儲(chǔ)的方式
   */
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
        clients.inMemory()
        .withClient("hou") // 客戶(hù)端id,必須有
        .secret(passwordEncoder.encode("123456")) // 客戶(hù)端密碼
            .scopes("server")
        .authorizedGrantTypes("authorization_code", "password", "refresh_token") //驗(yàn)證類(lèi)型
        .redirectUris("http://www.baidu.com");
        /*redirectUris 關(guān)于這個(gè)配置項(xiàng),是在 OAuth2協(xié)議中,認(rèn)證成功后的回調(diào)地址,此值同樣可以配置多個(gè)*/
     //數(shù)據(jù)庫(kù)配置,需要建表
//    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存儲(chǔ)類(lèi)型
  @Bean
  public TokenStore tokenStore() {
    //return new JdbcTokenStore(dataSource); //存儲(chǔ)mysql中
    return new InMemoryTokenStore();  //存儲(chǔ)內(nèi)存中
    //new RedisTokenStore(connectionFactory); //存儲(chǔ)redis中
  }

  //配置獲取token策略和檢查策略
  @Override
  public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    oauthServer.tokenKeyAccess("permitAll()") //獲取token請(qǐng)求不進(jìn)行攔截
        .checkTokenAccess("isAuthenticated()") //驗(yàn)證通過(guò)返回token信息
        .allowFormAuthenticationForClients();  // 允許 客戶(hù)端使用client_id和client_secret獲取token
  }
}

二 測(cè)試獲取Token

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

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

yml配置客戶(hù)端信息以及認(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ù),即可開(kāi)啟token驗(yàn)證保護(hù)
@EnableGlobalMethodSecurity(prePostEnabled = true) // 開(kāi)啟方法權(quán)限注解
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

  @Override
  public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        //配置所有請(qǐng)求不需要認(rèn)證,在方法用注解定制權(quán)限
        .anyRequest().permitAll();
  }
}

編寫(xiě)權(quán)限控制

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

四 測(cè)試權(quán)限

不使用token

使用token

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中獲取List中最后一個(gè)元素的三種方法

    Java中獲取List中最后一個(gè)元素的三種方法

    在Java編程中我們經(jīng)常需要獲取一個(gè)List集合中的最后一個(gè)元素,這篇文章主要給大家介紹了關(guān)于Java中獲取List中最后一個(gè)元素的三種方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • 如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問(wèn)詳解

    如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問(wèn)詳解

    授權(quán)是最常見(jiàn)的JWT使用場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何基于JWT實(shí)現(xiàn)接口的授權(quán)訪問(wèn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • windows下 jdk1.7安裝教程圖解

    windows下 jdk1.7安裝教程圖解

    java編程的初學(xué)者在開(kāi)始編碼前都會(huì)遇到一個(gè)難題,那就是jdk1.7環(huán)境變量配置怎么操作,怎么安裝,針對(duì)這個(gè)難題,小編特地為大家整理相關(guān)教程,不了解的朋友可以前往查看使用
    2018-05-05
  • Java中使用MongoDB數(shù)據(jù)庫(kù)實(shí)例Demo

    Java中使用MongoDB數(shù)據(jù)庫(kù)實(shí)例Demo

    MongoDB是由C++語(yǔ)言編寫(xiě)的,基于分布式文件存儲(chǔ)的數(shù)據(jù)庫(kù),是一個(gè)介于關(guān)系數(shù)據(jù)庫(kù)和非關(guān)系數(shù)據(jù)庫(kù)之間的產(chǎn)品,是最接近于關(guān)系型數(shù)據(jù)庫(kù)的NoSQL數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于Java中使用MongoDB數(shù)據(jù)庫(kù)的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • Java如何利用Mybatis進(jìn)行數(shù)據(jù)權(quán)限控制詳解

    Java如何利用Mybatis進(jìn)行數(shù)據(jù)權(quán)限控制詳解

    這篇文章主要介紹了Java如何利用Mybatis進(jìn)行數(shù)據(jù)權(quán)限控制詳解,數(shù)據(jù)權(quán)限控制最終的效果是會(huì)要求在同一個(gè)數(shù)據(jù)請(qǐng)求方法中,根據(jù)不同的權(quán)限返回不同的數(shù)據(jù)集,而且無(wú)需并且不能由研發(fā)編碼控制。,需要的朋友可以參考下
    2019-06-06
  • Java?axios與spring前后端分離傳參規(guī)范總結(jié)

    Java?axios與spring前后端分離傳參規(guī)范總結(jié)

    這篇文章主要介紹了Java?axios與spring前后端分離傳參規(guī)范總結(jié),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • Springboot Thymeleaf模板文件調(diào)用Java類(lèi)靜態(tài)方法

    Springboot Thymeleaf模板文件調(diào)用Java類(lèi)靜態(tài)方法

    這篇文章主要介紹了Springboot Thymeleaf模板文件調(diào)用Java類(lèi)靜態(tài)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2007-09-09
  • SpringCloud?Gateway讀取Request?Body方式

    SpringCloud?Gateway讀取Request?Body方式

    這篇文章主要介紹了SpringCloud?Gateway讀取Request?Body方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 使用Java接收和處理OpenTelemetry數(shù)據(jù)的完整指南

    使用Java接收和處理OpenTelemetry數(shù)據(jù)的完整指南

    在現(xiàn)代分布式系統(tǒng)中,OpenTelemetry 成為了一種常見(jiàn)的標(biāo)準(zhǔn),用于跟蹤和監(jiān)控應(yīng)用程序的性能和行為,OTLP是 OpenTelemetry 社區(qū)定義的一種數(shù)據(jù)傳輸協(xié)議,文將介紹如何使用 Java 編寫(xiě)代碼來(lái)接收和處理 OTLP 數(shù)據(jù),需要的朋友可以參考下
    2024-04-04
  • spring boot的攔截器簡(jiǎn)單使用示例代碼

    spring boot的攔截器簡(jiǎn)單使用示例代碼

    這篇文章主要介紹了spring boot的攔截器簡(jiǎn)單使用實(shí)例代碼,需要的的朋友參考下吧
    2017-04-04

最新評(píng)論

广宁县| 兴城市| 芒康县| 色达县| 九江县| 密云县| 钦州市| 平度市| 红原县| 沈丘县| 富蕴县| 塘沽区| 凌海市| 托克托县| 五原县| 镇安县| 澎湖县| 娄底市| 南澳县| 阿尔山市| 康乐县| 卢龙县| 策勒县| 揭西县| 蓬安县| 信丰县| 铜陵市| 乐业县| 中宁县| 吴桥县| 宿松县| 滨州市| 高平市| 井研县| 普陀区| 恩平市| 开平市| 安庆市| 宜黄县| 剑阁县| 唐海县|