首页 > 解决方案 > 春季启动安全性要求意外授权

问题描述

我正在使用 spring boot 进行身份验证项目。我使用了spring starter security,但我面临的问题是我想允许所有人访问某些页面。但是 spring 也要求在使用这些网页时进行身份验证。

我试过这段代码:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
            .authorizeRequests().antMatchers("/","/signUp").permitAll().anyRequest().authenticated()
            .and()
            .httpBasic();
       }

我也使用了以下代码,但也没有按照我描述的预期方式工作并要求身份验证

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
            .authorizeRequests().antMatchers("/").permitAll()
            .and()
            .httpBasic();
     }

预计无需身份验证即可访问这些页面,但它要求进行身份验证。

标签: springspring-bootauthenticationspring-security

解决方案


尝试 .antMatchers(HttpMethod.Method,"/endpoint")/**使用configure(HttpSecurity http)

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

         http
         .csrf().disable()
         .authorizeRequests()
         .antMatchers("/**","/signUp").permitAll()
         .antMatchers(HttpMethod.POST,"/endpointPOST").permitAll()
         .antMatchers(HttpMethod.GET,"/endpointGET").permitAll()
         .anyRequest().authenticated();

    }

如果您想绕过某些端点的安全过滤器链,请使用configure(WebSecurity web).

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
            .antMatchers("/signUp", "**/endpoint/**");
    }

HttpSecurity 与 WebSecurity


推荐阅读