springboot+Oauth2——自定义AuthenticationManager和认证path

来源:互联网 发布:淘宝怎么上架宝贝教程 编辑:程序博客网 时间:2024/06/16 03:12

本人在工作中需要构建这么一个后台框架,基于springboot,登录时认证使用自定义AuthenticationManager;同时支持Oauth2访问指定API接口,认证时的AuthenticationManager和登录规则不同。在研究了源码的基础上参考很多文章,目前基本得以解决。



以上是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();
}



1 0