springboot+Oauth2實(shí)現(xiàn)自定義AuthenticationManager和認(rèn)證path
本人在工作中需要構(gòu)建這么一個(gè)后臺(tái)框架,基于springboot,登錄時(shí)認(rèn)證使用自定義AuthenticationManager;同時(shí)支持Oauth2訪問(wèn)指定API接口,認(rèn)證時(shí)的AuthenticationManager和登錄規(guī)則不同。在研究了源碼的基礎(chǔ)上參考很多文章,目前基本得以解決。
@Configuration public class OAuth2Configuration { @SpringBootApplication @RestController @EnableResourceServer @Configuration @EnableAuthorizationServer protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter implements EnvironmentAware { private static final String ENV_OAUTH = "authentication.oauth."; private static final String PROP_CLIENTID = "clientid"; private static final String PROP_SECRET = "secret"; private static final String PROP_TOKEN_VALIDITY_SECONDS = "tokenValidityInSeconds"; private RelaxedPropertyResolver propertyResolver; @Autowired private DataSource dataSource; @Bean public TokenStore tokenStore() { return new JdbcTokenStore(dataSource); } // @Autowired // @Qualifier("authenticationManagerBean") // private AuthenticationManager authenticationManager; @Autowired @Qualifier("daoAuhthenticationOauthProvider") private AuthenticationProvider daoAuhthenticationOauthProvider; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { // @formatter:off endpoints .tokenStore(tokenStore()) .authenticationManager(new AuthenticationManager(){ @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { // TODO Auto-generated method stub return daoAuhthenticationOauthProvider.authenticate(authentication); } }); // @formatter:on } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients .inMemory() .withClient(propertyResolver.getProperty(PROP_CLIENTID)) .scopes("read", "write") .authorities(Authorities.ROLE_CHANNEL.name()) .authorizedGrantTypes("password", "refresh_token") .secret(propertyResolver.getProperty(PROP_SECRET)) .accessTokenValiditySeconds(propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 1800)); } @Override public void setEnvironment(Environment environment) { this.propertyResolver = new RelaxedPropertyResolver(environment, ENV_OAUTH); } @Configuration @EnableResourceServer protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http .antMatcher("/api/dev/**") .authorizeRequests() .anyRequest() .hasRole("DEVELEPOR") .and() .antMatcher("/api/channel/**") .authorizeRequests() .anyRequest() .hasRole("CHANNEL"); } } } }
以上是Oauth2的主要配置,SecurityConfiguration的配置就不貼了,大家可以去github上找資料,下面是如何自定一個(gè)daoAuhthenticationProvider。
@Bean(name="daoAuhthenticationProvider") public AuthenticationProvider daoAuhthenticationProvider() { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); daoAuthenticationProvider.setUserDetailsService(userDetailsService); daoAuthenticationProvider.setHideUserNotFoundExceptions(false); daoAuthenticationProvider.setPasswordEncoder(passwordEncoder); return daoAuthenticationProvider; } @Bean(name="daoAuhthenticationOauthProvider") public AuthenticationProvider daoAuhthenticationOauthProvider() { DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); daoAuthenticationProvider.setUserDetailsService(userDetailsOauthService); daoAuthenticationProvider.setHideUserNotFoundExceptions(false); daoAuthenticationProvider.setPasswordEncoder(passwordEncoder); return daoAuthenticationProvider; } @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(daoAuhthenticationProvider()); // auth.authenticationProvider(daoAuhthenticationProvider1()); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot+element-ui實(shí)現(xiàn)多文件一次上傳功能
這篇文章主要介紹了springboot+element-ui多文件一次上傳功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06jenkins+maven+svn自動(dòng)部署和發(fā)布的詳細(xì)圖文教程
Jenkins是一個(gè)開(kāi)源的、可擴(kuò)展的持續(xù)集成、交付、部署的基于web界面的平臺(tái)。這篇文章主要介紹了jenkins+maven+svn自動(dòng)部署和發(fā)布的詳細(xì)圖文教程,需要的朋友可以參考下2020-09-09java文件的簡(jiǎn)單讀寫操作方法實(shí)例分析
這篇文章主要介紹了java文件的簡(jiǎn)單讀寫操作方法,結(jié)合實(shí)例形式分析了java文件流進(jìn)行讀寫操作的方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-05-05Java線程的并發(fā)工具類實(shí)現(xiàn)原理解析
本文給大家講解Java線程的并發(fā)工具類的一些知識(shí),通過(guò)適用場(chǎng)景分析大數(shù)據(jù)量統(tǒng)計(jì)類任務(wù)的實(shí)現(xiàn)原理和封裝,多個(gè)示例代碼講解的非常詳細(xì),對(duì)java線程并發(fā)工具類相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)下吧2021-06-06超級(jí)詳細(xì)Java?JDK環(huán)境配置教程(Mac?版)
這篇文章詳細(xì)講解了在MacOS上安裝JDK及配置Java環(huán)境的步驟,包括下載JDK安裝包、安裝JDK、查詢安裝路徑以及配置環(huán)境變量,旨在為初學(xué)者提供一份保姆級(jí)的安裝指南,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10Spring?Boot?多數(shù)據(jù)源處理事務(wù)的思路詳解
這篇文章主要介紹了Spring?Boot?多數(shù)據(jù)源如何處理事務(wù),本文單純就是技術(shù)探討,要從實(shí)際應(yīng)用中來(lái)說(shuō)的話,我并不建議這樣去玩分布式事務(wù)、也不建議這樣去玩多數(shù)據(jù)源,畢竟分布式事務(wù)主要還是用在微服務(wù)場(chǎng)景下,對(duì)Spring?Boot?多數(shù)據(jù)源事務(wù)相關(guān)知識(shí)感興趣的朋友參考下本文2022-06-06