java - Spring Security 对某些 URL 进行授权并拒绝所有其他 URL

标签 java spring spring-boot spring-security

我有以下配置类,我想在其中授权某些请求并拒绝所有其他请求。

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

相关文章:

java - 从 maven-antrun-plugin 调用 ant 脚本时出现 IllegalAccessError

spring-boot - Kotlin Spring : Unresolved reference => copy

java - 在android中的线程内设置TextView值

java - spring mvc中如何获取方法请求

java - 我什么时候应该在 Spring Boot 应用程序中覆盖 Spring Security 的 configure(AuthenticationManagerBuilder auth)?

进入数据库前的Java缓存数据

mysql - 如何在具有多个外键的 Spring boot/JPA 中构建实体?

java - 将json数据ajax传递给Spring boot

java - 在 do while 循环中尝试 catch

java - 泛型类型参数