我有以下配置类,我想在其中授权某些请求并拒绝所有其他请求。
@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-config-rest/dev/master,如果我尝试访问/phx-config-rest/prod/master 或任何其他 url、请求将被视为未经授权的访问。顺便说一句,这段代码是关于 Spring Cloud 配置服务器的。有什么想法吗?
最佳答案
改变
.anyRequest().authenticated()
到
.anyRequest().denyAll()
关于java - Spring Security 对某些 URL 进行授权并拒绝所有其他 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41345815/