色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Spring cloud oauth2如何搭建認證資源中心

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

一 認證中心搭建

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

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

配置spring security

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

配置OAuth2認證中心

/** * OAuth2授權服務器 */@EnableAuthorizationServer //聲明OAuth2認證中心@Configurationpublic class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired @Qualifier('authenticationManagerBean') private AuthenticationManager authenticationManager; @Autowired private DataSource dataSource; @Autowired private UserDetailsService userDetailsService; @Autowired private PasswordEncoder passwordEncoder; /** * 這個方法主要是用于校驗注冊的第三方客戶端的信息,可以存儲在數據庫中,默認方式是存儲在內存中,如下所示,注釋掉的代碼即為內存中存儲的方式 */ @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') //驗證類型.redirectUris('http://www.baidu.com');/*redirectUris 關于這個配置項,是在 OAuth2協議中,認證成功后的回調地址,此值同樣可以配置多個*/ //數據庫配置,需要建表// clients.withClientDetails(clientDetailsService());// clients.jdbc(dataSource); } // 聲明 ClientDetails實現 private ClientDetailsService clientDetailsService() { return new JdbcClientDetailsService(dataSource); } /** * 控制token端點信息 */ @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(); //存儲內存中 //new RedisTokenStore(connectionFactory); //存儲redis中 } //配置獲取token策略和檢查策略 @Override public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.tokenKeyAccess('permitAll()') //獲取token請求不進行攔截.checkTokenAccess('isAuthenticated()') //驗證通過返回token信息.allowFormAuthenticationForClients(); // 允許 客戶端使用client_id和client_secret獲取token }}

二 測試獲取Token

默認獲取token接口圖中2所示,這里要說明一點,參數key千萬不能有空格,尤其是client_這兩個

Spring cloud oauth2如何搭建認證資源中心

三 需要保護的資源服務配置

yml配置客戶端信息以及認中心地址

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

配置認證中心地址即可

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

編寫權限控制

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

四 測試權限

不使用token

Spring cloud oauth2如何搭建認證資源中心

使用token

Spring cloud oauth2如何搭建認證資源中心

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产另类视频 | a级男女性高爱潮高清试 | 久草中文视频 | 91久久夜色精品国产网站 | 国产三级观看 | 一级一级一片免费 | 国产成人亚洲合集青青草原精品 | 免费看一级欧美毛片视频 | 久久久久综合一本久道 | 日本在线观看一级高清片 | 欧美一级特黄特黄毛片 | 黄色a∨ | 亚洲欧美视频一区 | 亚洲日韩aⅴ在线视频 | 国产91精品久久久久久久 | 日韩一级特黄毛片在线看 | 天堂影院jav成人天堂免费观看 | 中国国产成人精品久久 | 高清成人爽a毛片免费网站 高清大学生毛片一级 | 国产精品李雅在线观看 | 久久久婷 | avav在线看 | 全部免费国产潢色一级 | 国产欧美在线观看不卡一 | 日韩国产一区二区 | 国产精品每日更新在线观看 | 欧美成人国产一区二区 | 在线免费观看成年人视频 | 男女性关系视频免费观看软件 | 欧美一级毛片免费看 | 牲欧美 | 亚洲干综合| 国产一级视频在线观看 | 欧美高清在线精品一区 | 国产成人精品日本亚洲语音2 | 91精品国产91久久久久久青草 | 欧美精品午夜毛片免费看 | 欧美高清一区二区三区欧美 | 美女双腿打开让男人桶爽网站 | 欧美日本一区亚洲欧美一区 | 成人欧美一区二区三区视频 |