【问题标题】:Spring Boot 2 and migrating OAuth2 configurationSpring Boot 2 和迁移 OAuth2 配置
【发布时间】:2018-08-12 23:20:03
【问题描述】:

我们正在将 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'

编辑:这是我的网络安全属性:

@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 - Unauthorized。相反,我得到302 - Redirect to /login,这意味着基本身份验证具有更高的优先级。我不知道如何解决这个问题,因为我尝试使用的任何命令都不起作用。

【问题讨论】:

    标签: java spring-boot spring-security


    【解决方案1】:

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

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

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

    【讨论】:

    • 那么顺序是什么?首先调用哪个过滤器? OAuth 还是 Basic?谢谢
    • 首先,basic auth 会派上用场,因为您需要提供 clientId 和 secret 来获取令牌。之后,它只是 OAuth
    • 我正在尝试公开 AuthenticationManager,但它显示“尝试解析 AuthenticationManager 时检测到依赖循环”您是否也遇到过这种情况?
    【解决方案2】:

    有同样的问题。只是为了猴子补丁(稍后将研究 @Order 注释的真正含义),我发现在 1.5.* 版本中分配给 ACCESS_OVERRIDE_ORDER 的值是从那里 https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/api/ ,这似乎是 @Order(2147483640)...

    【讨论】:

    猜你喜欢
    • 2018-01-05
    • 2018-12-11
    • 2020-07-07
    • 2023-03-18
    • 2015-04-10
    • 2018-08-25
    • 2019-10-22
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多