簡體   English   中英

Spring Boot 2和遷移OAuth2配置

[英]Spring Boot 2 and migrating OAuth2 configuration

我們正在將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'

編輯:這是我的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 - Unauthorized 相反,我得到302 - Redirect to /login意味着基本身份驗證具有更高的優先級。 我不知道如何解決這個問題,因為我嘗試使用的任何訂單都不起作用。

有同樣的問題。 只是為了猴子修補(稍后將調查@Order注釋的真正含義),我發現在1.5。*版本中從https://docs.spring.io/spring-boot/docs/1.5分配給ACCESS_OVERRIDE_ORDER。 10.RELEASE / api / ,似乎是@Order(2147483640) ......

所以,事實證明問題不在我的WebSecurity配置中,但它有點復雜。 Spring Security 5要求clientSecret在默認情況下使用BCrypt進行加密,這是我遺漏的。 另外,添加AuthenicationManager bean修復了該問題。

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

我在github上有一個帶有此功能的示例項目,但我會稍微改進一下來修復一些其他問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM