springboot+Oauth2实现自定义AuthenticationManager和认证path
更新时间:2017年09月06日 09:51:39 作者:huhanguang89
本篇文章主要介绍了springboot+Oauth2实现自定义AuthenticationManager和认证path,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本人在工作中需要构建这么一个后台框架,基于springboot,登录时认证使用自定义AuthenticationManager;同时支持Oauth2访问指定API接口,认证时的AuthenticationManager和登录规则不同。在研究了源码的基础上参考很多文章,目前基本得以解决。
@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 {
.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();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
-
-
JavaWeb实现文件上传功能详解
这篇文章主要介绍了JavaWeb实现文件上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
2022-02-02
-
-
-
Java执行cmd命令两种实现方法解析
这篇文章主要介绍了Java执行cmd命令两种实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
2020-07-07
-
java 中的乱码问题汇总及解决方案
这篇文章主要介绍了java 中的乱码问题汇总相关资料,并附解决方案,出现乱码问题有编码与解码,字节流与字符流出现乱码,等其他情况,需要的朋友可以参考下
2016-11-11
-
Java开发之Lombok指南
Lombok是一款Java开发插件,使得Java开发者可以通过其定义的一些注解来消除业务工程中冗长和繁琐的代码,它能够在编译源代码期间自动帮我们生成这些方法,并没有如反射那样降低程序的性能。下面我们来详细了解一下吧
2019-06-06
-
Java 线程池详解
本文给大家总结了java中的线程池的相关问题,非常的详细也很实用,有需要的小伙伴可以参考下。
2016-03-03
-
Mybatis-Plus 搭建与使用入门(小结)
Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,这篇文章主要介绍了Mybatis-Plus 搭建与使用入门(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
2018-06-06
-
-
Java实现FIFO任务调度队列策略
在工作中,很多高并发的场景中,我们会用到队列来实现大量的任务请求。当任务需要某些特殊资源的时候,我们还需要合理的分配资源,让队列中的任务高效且有序完成任务。本文将为大家介绍通过java实现FIFO任务调度,需要的可以参考一下
2021-12-12