2016-07-12 1 views
1

У меня есть некоторые локальные службы, сопоставленные с той же базой. Эти сервисы для пользователей должны быть закрыты, но один из них используется ZooKeeper и должен быть открытым. Так я настроен:Spring Security 4 настроить матчи

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

?

P.S. Служба Open используется для ответа ZooKeeper.

ответ

2

решение:

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

это решение работает также, если ir алить одна инструкция:

@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 файлы все настройки безопасности.