2016-12-27 83 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()