spring 无法在任何请求之后配置antMatcher(多个antMatcher)

a6b3iqyw  于 2023-04-28  发布在  Spring
关注(0)|答案(5)|浏览(289)

我正在尝试配置Spring Security并得到以下错误:
原因:Java。lang.IllegalStateException:无法在任何请求后配置antMatchers
下面是我的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();
    }
}

我已经试过调用httpSecurityauthorizeRequests().anyRequest().authenticated(),就像提到的here一样,仍然不起作用。任何建议都会有帮助

m1m5dgzv

m1m5dgzv1#

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

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

t98cgbkg2#

这可能会帮助别人为同样的类型的异常这里是我的代码:-

@Override
      protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        
         http.authorizeRequests()
             .antMatchers("/**")
             .hasAnyRole()
             .and()
             .formLogin();
    }

我刚刚删除了对超级类的调用:- super。return();对我很有效。把那条线拿掉。

wqlqzqxt

wqlqzqxt3#

Authenticated should come last 
httpSecurity.csrf().disable()
                 .cors()
                .and().authorizeRequests()
                .antMatchers("xyz").permitAll()
                .antMatchers("abc")
                .authenticated()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
sg3maiej

sg3maiej4#

protected void configure(HttpSecurity http) throws Exception {
    
        http.csrf()
            .disable()
            .cors()
            .disable()
            .authorizeRequests()
            .antMatchers("/login", "/user/createUser")
            .permitAll()
            .antMatchers(HttpMethod.OPTIONS)
            .permitAll()
            .anyRequest()
            .authenticated()
            .and().exceptionHandling()
            .authenticationEntryPoint(unautorizeHandler)
            .and().sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

       http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
g6ll5ycj

g6ll5ycj5#

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

相关问题