java - 在anyRequest之后无法配置antMatchers(多个antMatcher)

标签 java spring spring-security

我正在尝试配置Spring Security并出现以下错误:

Caused by: java.lang.IllegalStateException: Can't configure antMatchers after anyRequest



这是我的SecurityConfig类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

            auth.userDetailsService(userDetailsService).passwordEncoder(encodePWD());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{

        http
            .csrf().disable();
        http
            .httpBasic()
                .and()
            .authorizeRequests()
                .antMatchers("/rest/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .authorizeRequests()
                .antMatchers("/secure/**").hasAnyRole("ADMIN")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .permitAll();

        http
            .authorizeRequests()
                .antMatchers("/login").permitAll();
    }

    @Bean
    public BCryptPasswordEncoder encodePWD(){
        return new BCryptPasswordEncoder();
    }
}

我已经尝试过像here那样调用httpSecurityauthorizeRequests().anyRequest().authenticated()
还是没用
...任何建议都会有所帮助。

最佳答案

修改规则如下。 .anyRequest().authenticated()只能使用一次。

    http
        .httpBasic()
            .and()
        .authorizeRequests()
            .antMatchers("/rest/**").permitAll()
            .and()
        .authorizeRequests()
            .antMatchers("/secure/**").hasAnyRole("ADMIN")
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .permitAll();

关于java - 在anyRequest之后无法配置antMatchers(多个antMatcher),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60123616/

相关文章:

java - 当一个线程初始化该值而其他线程只是读取该值时,是否存在并发问题

java - 将 Facebook 身份验证集成到 FacebookApp 中的 Spring Security

java - 四元数的作用类似于欧拉

java - 未找到带有 URI 调度程序错误的 HTTP 请求的映射

java.net.ConnectException : Connection refused: connect openshift

java - Spring Cache - @CachePut 和 @CacheEvict 之间的真正区别

java - Spring Security 3.1 重定向到登录不起作用

unit-testing - 如何模拟acegi身份验证服务进行单元测试?

java - 数据插入外部 sqlite 数据库但不保存在 android studio 中

java - 在 Flutter 中从 Play 商店获取产品时应用内购买崩溃