【发布时间】:2015-04-16 17:07:11
【问题描述】:
我一直在尝试使用 Dave Syer 的指南和 JHipster 的一些灵感来实现 OAuth2 身份验证服务器。但我无法弄清楚这一切是如何协同工作的。
当我使用 ResourceServerConfigurerAdapter 时,使用 WebSecurityConfigurerAdapter 的安全设置似乎被覆盖了。
@Configuration
@EnableResourceServer
public class OAuth2ResourceConfig extends ResourceServerConfigurerAdapter {
private TokenExtractor tokenExtractor = new BearerTokenExtractor();
@Override
public void configure(HttpSecurity http) throws Exception {
http
.addFilterAfter(contextClearer(), AbstractPreAuthenticatedProcessingFilter.class)
.authorizeRequests()
.anyRequest().authenticated().and().httpBasic();
}
private OncePerRequestFilter contextClearer() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (tokenExtractor.extract(request) == null) {
SecurityContextHolder.clearContext();
}
filterChain.doFilter(request, response);
}
};
}
@Component
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
private final AuthenticationManager authenticationManager;
@Autowired
public CustomWebSecurityConfigurerAdapter(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.parentAuthenticationManager(authenticationManager);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login").permitAll()
.and()
.authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.and()
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests().anyRequest().authenticated();
}
}
这是取自几个不同示例的代码,因此它们可能无法很好地混合。但是我找不到 OAuth2 的好的文档/示例列表(不像 Spring Boot,它有一个很棒的文档),所以我在理解它们如何组合在一起时遇到了问题。 如果我不将 loginForm 添加到 ResourceServerConfigurerAdapter,它只会让我未经授权。但我在 WebSecurityConfigurererAdapter 中将其定义为 permitAll()。
这是 AuthorizationServerConfigurerAdapter:
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("acme")
.secret("acmesecret")
.authorizedGrantTypes("authorization_code", "refresh_token",
"password").scopes("openid");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager).accessTokenConverter(jwtAccessTokenConverter);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}
}
我做错了什么?我是否必须在 ResourceServerConfigurerAdapter 中设置所有安全性?我还需要 WebSecurityConfigurerAdapter 吗?
如果有人知道任何指南、教程、博客或任何类似的东西可以帮助我了解它是如何工作的,那将不胜感激。
亲切的问候,肯尼斯。
【问题讨论】:
-
据我所知,您的
OAuth2ResourceConfig是多余的。只是说。 -
有什么症状(你走的是什么路,你看到了什么)?使用 curl(-v 查看标题)和 Spring Security 的 DEBUG 日志记录应该告诉您您需要知道的一切。
-
症状是它基本上忽略了WebSecurityConfigurerAdapter。但是在阅读了下面的解释之后,我对它的工作原理有了更多了解。
-
我没有得到你的 CustomWebSecurityConfigurerAdapter 配置来让我进入登录表单,我有 404。你是如何测试它的?
标签: spring spring-security spring-security-oauth2