EnableResourceServer中断oAuth2授权服务器

Voj*_*ech 2 spring-security spring-boot spring-security-oauth2 spring-oauth2

我使用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());
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望资源服务器成为同一应用程序的一部分。目的是我需要一个/ 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我怀疑资源服务器安全链位于授权服务器安全链之前。我尝试使用注解订单对WebSecurityConfig进行注解,但未能解决我的问题:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    ...
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?请指教。提前致谢!

编辑1 我将方法configure(HttpSecurity http)添加到ResourceServerConfig中,并且在WebSecurityConfig上将Order批注的值更改为-1。现在,将应用在WebSecurityConfig中定义的安全性过滤,并忽略在ResourceServerConfig中定义的安全性。因此,当我使用有效令牌调用/ me端点时,将重定向到登录页面。

Voj*_*ech 5

问题的原因是ResourceServerConfig类中的HTTP安全性配置错误。正确的配置如下:

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .requestMatchers().antMatchers("/api/**").and()
            .authorizeRequests().anyRequest().authenticated();
}
Run Code Online (Sandbox Code Playgroud)

requestMatchers将确保开始仅在路径请求“/ API /”将通过该安全链进行处理。所有其他请求将传递到WebSecurityConfig类中定义的安全链。我在配置中丢失了此信息,因此所有请求均由ResourceServerConfig安全链处理,并且没有请求到达WebSecurityConfig安全链。