2016-07-12 20 views
1

Ich habe einige lokale Dienste auf gleiche Basis-URL-Teil zugeordnet. Diese Dienste für Benutzer sollten geschlossen werden, aber einer davon wird von ZooKeeper verwendet und sollte geöffnet sein. So i konfiguriert:Spring Security 4 anpassen matchers

@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(); 
    } 
} 

diese Konfiguration nicht funktioniert. Ich habe einen offenen Service nicht nur für Veranstaltungen. Jeder der Dienste ist offen. Wenn ich die Konfiguration ändere zu:

@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(); 
    } 
} 

Alle Dienste sind geschlossen als.

Ist es möglich, durch den Weg anonyme Anforderungen überall

"/**" 

authentifizierten Dienste zu konfigurieren

"/inner/service/**" 

mit offenen einem

"/inner/service/event/bus" 

?

P.S. Open Service wird für die ZooKeeper-Antwort verwendet.

Antwort

2

Lösung ist:

@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(); 
} 

Diese Lösung funktioniert:

  1. Wechsel zu anderen Weg

    "/inner/service/event/bus" 
    // for example 
    "/inner/event/bus" 
    
  2. Änderung http Sicherheitskonfiguration (uncrossed Pässe zur Verfügung zu stellen) auch wenn ir emove eine Anweisung:

    @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(); 
        } 
    

    Und noch ein wichtiger Punkt (Ich denke, wichtig). Ich habe von meiner * .yml Dateien alle Sicherheitsanpassungen entfernt.

Verwandte Themen