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

springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認(rèn)證path

 更新時間:2017年09月06日 09:51:39   作者:huhanguang89  
本篇文章主要介紹了springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認(rèn)證path,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本人在工作中需要構(gòu)建這么一個后臺框架,基于springboot,登錄時認(rèn)證使用自定義AuthenticationManager;同時支持Oauth2訪問指定API接口,認(rèn)證時的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上找資料,下面是如何自定一個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();
}

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

相關(guān)文章

最新評論

巴彦淖尔市| 五莲县| 聂拉木县| 安陆市| 格尔木市| 马边| 雷山县| 务川| 阿克苏市| 曲周县| 延安市| 水城县| 莱芜市| 建昌县| 保定市| 定日县| 柘荣县| 临漳县| 湘潭市| 石柱| 景德镇市| 敖汉旗| 固原市| 克东县| 错那县| 连城县| 丰都县| 博乐市| 丹东市| 青阳县| 黔江区| 永宁县| 漾濞| 威海市| 屯昌县| 涞水县| 五莲县| 理塘县| 大洼县| 绥化市| 遂川县|