2016-12-27 63 views
0

我有下面的配置類,我想授權某些請求並拒絕所有其他請求。Spring安全授權某些URL並拒絕所有其他人

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .httpBasic() 
       .and() 
      .authorizeRequests() 
       .antMatchers("/phx-config-rest/dev/master").hasRole("DEV") 
       .anyRequest().authenticated() 
       .and() 
      .csrf() 
       .disable(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth. 
      inMemoryAuthentication() 
       .withUser("devuser") 
       .password("dev") 
       .roles("DEV"); 
    } 
} 

按這個代碼我的印象是,Spring將只允許我使用用戶devuser的'和訪問/ PHX-配置,其餘的/ dev /主如果我嘗試訪問/ PHX-配置,休息/ prod/master或任何其他網址,請求將被視爲未經授權的訪問。順便說一句,這段代碼片段就是關於Spring雲配置服務器的。任何想法?

回答

3

變化

.anyRequest().authenticated() 

.anyRequest().denyAll()