Spring Boot 2和迁移OAuth2配置

Sma*_*ajl 7 java spring-security spring-boot

我们正在将Spring Boot 1.5.7应用程序迁移到Spring Boot 2,我注意到它SecurityProperties.ACCESS_OVERRIDE_ORDER不再可用了.

我们正在使用@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER))强制某个安全配置过滤器的顺序,如果没有这个注释它就不能工作(由于安全过滤器的顺序错误,因此获得不同的状态).是否有一些替换或配置更改,以使其以旧方式工作?

我们有基本的auth + OAuth2.

这是我们使用的OAuth2依赖项:

compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.1.0.RELEASE'
Run Code Online (Sandbox Code Playgroud)

编辑:这是我的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);
  }

}
Run Code Online (Sandbox Code Playgroud)

/user通过REST 访问时,我希望401 - Unauthorized没有有效的令牌.相反,我302 - Redirect to /login意味着基本身份验证具有更高的优先级.我不知道如何解决这个问题,因为我尝试使用的任何订单都不起作用.

Sma*_*ajl 2

所以,事实证明问题不在我的 WebSecurity 配置中,但它有点复杂。Spring Security 5 要求 clientSecret 默认使用 BCrypt 加密,而我缺少这一点。另外,添加AuthenicationManagerbean 解决了这个问题。

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
           return super.authenticationManagerBean();
}
Run Code Online (Sandbox Code Playgroud)

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