【发布时间】:2017-11-15 20:42:52
【问题描述】:
我使用 Spring Boot 版本 1.5.2.RELEASE 实现了 oAuth2 授权服务器。授权服务器支持隐式流。使用登录表单 (http://localhost:8200/login) 下面的 WebSecurityConfig 效果很好。
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JpaUserDetailsService userDetailsService;
@Bean
@Override
public UserDetailsService userDetailsServiceBean() throws Exception {
return userDetailsService;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationProvider authenticationProvider() throws Exception {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsServiceBean());
provider.setPasswordEncoder(passwordEncoder());
return provider;
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return new ProviderManager(singletonList(authenticationProvider()));
}
@Override
public void configure(WebSecurity web) {
web.ignoring()
.antMatchers("/")
.antMatchers("/docs/**")
.antMatchers("/swagger/**")
.antMatchers("/token/**")
.antMatchers("/v2/*")
.antMatchers(HttpMethod.OPTIONS, "/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().antMatchers("/login**").permitAll().anyRequest().authenticated().and()
.formLogin().loginPage("/login").permitAll().and()
.logout().permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
}
我希望资源服务器成为同一应用程序的一部分。目的是我需要一个 /me 端点,它将为我提供登录用户的详细信息和用于管理用户的端点。但是,当我在下面添加带有 EnableResourceServer 注释的 ResourceServerConfig 时,当我请求 http://localhost:8200/login 时,我开始收到错误“访问此资源需要完全身份验证”。
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
public static final String RESOURCE_ID = "proclaim-auth";
@Autowired
private ResourceServerTokenServices tokenServices;
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources
.resourceId(RESOURCE_ID)
.tokenServices(tokenServices);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/api/ **").authenticated();
}
}
我怀疑资源服务器安全链先于授权服务器安全链。我尝试使用注释 Order 来注释 WebSecurityConfig,但它没有解决我的问题:
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
...
}
我做错了什么?请指教。 提前致谢!
编辑 1 我将方法 configure(HttpSecurity http) 添加到 ResourceServerConfig 中,并将 WebSecurityConfig 上的 Order 注释的值更改为 -1。现在应用了 WebSecurityConfig 中定义的安全过滤器,而 ResourceServerConfig 中定义的安全过滤器被忽略了。因此,当我使用有效令牌调用 /me 端点时,我将被重定向到登录页面。
【问题讨论】:
标签: spring-boot spring-security spring-security-oauth2 spring-oauth2