簡體   English   中英

Spring Security 4自定義匹配器

[英]Spring Security 4 customize matchers

我有一些本地服務映射到相同的基本URL部分。 為用戶提供的服務應關閉,但ZooKeeper使用其中一項服務並應將其打開。 所以我配置:

@Configuration
@EnableWebSecurity(debug = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
        .withUser("user").password("us").roles("USER");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/inner/service/**").hasRole("USER")
        .and().antMatcher("/inner/service/event/bus").csrf().disable().anonymous()
        .and().formLogin().and().logout().and().httpBasic();
  }
}

此配置不起作用。 我不僅為活動提供服務。 每個服務都是開放的。 如果我將配置更改為:

@Configuration
@EnableWebSecurity(debug = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
        .withUser("user").password("us").roles("USER");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/inner/service/**").hasRole("USER")
        .and().formLogin().and().logout().and().httpBasic();
  }
}

所有服務均關閉。

是否可以在任何地方配置匿名請求

"/**"

通過路徑認證的服務

"/inner/service/**"

開一

"/inner/service/event/bus"

PS Open服務用於ZooKeeper響應。

解決方案是:

  1. 移至其他路徑(以提供不交叉的通行證)

     "/inner/service/event/bus" // for example "/inner/event/bus" 
  2. 更改http安全配置:

     @Override protected void configure(HttpSecurity http) throws Exception { // to ignore csrf filter for zookeeper client api http.csrf().ignoringAntMatchers("/inner/event/bus"); // configure the HttpSecurity http.antMatcher("/inner/service/**").authorizeRequests() // configure open resources. this configuration can be missed because of root level security (see line before) .antMatchers("/", "/index.html", "/inner/event/bus", "view.html").permitAll() // configure role for specified api .antMatchers("/inner/service/**").hasRole("USER") // to use default login and logout api .and().formLogin().and().logout().logoutSuccessUrl("/").and().httpBasic(); } 

如果我刪除一條指令,此解決方案也有效:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/inner/event/bus");
        http.antMatcher("/inner/service/**").authorizeRequests()
            .antMatchers("/inner/service/**").hasRole("USER")
            .and().formLogin().and().logout().logoutSuccessUrl("/").and().httpBasic();
    }

還有一點( 我認為很重要 )。 我已經從* .yml文件中刪除了所有安全性自定義項。

暫無
暫無

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

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