Spring Security 4自定义匹配器

时间:2016-07-12 10:00:36

标签: java spring-security matcher

我有一些本地服务映射到相同的基本网址部分。应该关闭为用户提供的服务,但其中一个服务由ZooKeeper使用,应该是开放的。 所以我配置了:

Host *
HostkeyAlgorithms +ssh-dss
PubkeyAcceptedKeyTypes +ssh-dss

此配置不起作用。我不仅为活动提供开放服务。每项服务都是开放的。如果我将配置更改为:

@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/**"

P.S。 开放服务用于ZooKeeper响应。

1 个答案:

答案 0 :(得分:3)

解决方案是:

  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();
    }
    
  3. 如果我删除一条指令,这个解决方案也有效:

        @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 文件中删除了所有安全性自定义。