打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
springboot+Oauth2——自定义AuthenticationManager和认证path
@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");
}
}

}

}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
认证鉴权与API权限控制在微服务架构中的设计与实现:授权码模式 | Aoho''s Blog
OAuth2.0 最直观配置
Spring Security(4):自定义配置
SpringCloud SpringBoot OAuth2 Spring Security Redi...
Oauth2 JWT登出(黑名单方案)
SAP云平台和SAP C4C之间的OAuth2.0配置
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服