Spring Boot 2和迁移OAuth2配置

9
我们正在将Spring Boot 1.5.7应用迁移到Spring Boot 2,我发现SecurityProperties.ACCESS_OVERRIDE_ORDER不再可用。
我们使用@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER))来强制安全配置过滤器的某个顺序,但是没有这个注释后它不再起作用了(由于安全过滤器顺序错误而获取不同状态)。是否有替代方案或配置更改以使其以旧方式工作?
我们已经实现了基本身份验证+OAuth2。
这是我们使用的OAuth2依赖项:
compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.1.0.RELEASE'

编辑:这是我的WebSecurity属性:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  private static final String LOGIN = "/login";
  private static final String LOGOUT_SUCCESS = "/login?logout";

  private final UserDetailsService userDetailsService;
  private final AuthenticationManager authenticationManager;

  public WebSecurityConfig(UserDetailsService userDetailsService, @Lazy AuthenticationManager authenticationManager) {
    this.userDetailsService = userDetailsService;
    this.authenticationManager = authenticationManager;
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {

    // @formatter:off
    http
      // enable cors
      .cors().and()
      .requestMatchers().antMatchers("/oauth/**", "/*").and()
      // These from the above are secured by the following way
      .authorizeRequests().antMatchers("/").permitAll()
      // These from the rest are secured by the following way
      .anyRequest().authenticated().and()
      // Set login page
      .formLogin().loginPage(LOGIN).permitAll().defaultSuccessUrl(PROFILE)
      // Set logout handling
      .and().logout().logoutSuccessUrl(LOGOUT_SUCCESS);
      // @formatter:on

  }

  @Override
  public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.parentAuthenticationManager(authenticationManager);
    auth.userDetailsService(userDetailsService);
  }

}

访问REST接口/user时,如果没有有效的令牌,我期望得到401 - 未授权。然而实际上,我得到了302 - 重定向到 /login,这意味着基本身份验证优先级更高。我不确定如何解决这个问题,因为无论我尝试使用的顺序都不起作用。

2个回答

2

1
以前,这被定义为SecurityProperties.BASIC_AUTH_ORDER - 2,其计算结果为2147483640,但比一个晦涩的整数值更具表现力,因此更容易让读者理解。 - daiscog

2

所以,问题不在我的WebSecurity配置中,而是更加复杂一些。Spring Security 5默认要求clientSecret使用BCrypt进行加密,我没有这样做。另外,添加AuthenicationManager bean也解决了问题。

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
           return super.authenticationManagerBean();
}

我有一个示例项目,在github上具备此功能,但我将对其进行改进以修复一些附加问题。


那么顺序是什么?哪个过滤器先被调用?OAuth 还是 Basic?谢谢。 - akuma8
首先,基本身份验证是必要的,因为您需要提供clientId和secret来获取令牌。之后,就只剩下OAuth了。 - Smajl
1
我正在尝试暴露AuthenticationManager,但它显示“在尝试解析AuthenticationManager时检测到依赖项循环”。你也遇到过这个问题吗? - Robin van Breukelen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接