gpt4 book ai didi

java - 配置问题(HttpSecurity http)错误 : Can't configure antMatchers after anyRequest

转载 作者:行者123 更新时间:2023-12-02 09:06:50 24 4
gpt4 key购买 nike

我正在尝试做一个例子 here link我消除了所有可能的错误,但保留了一个:

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: Can't configure antMatchers after anyRequest

在堆栈跟踪的末尾:

Caused by: java.lang.IllegalStateException: Can't configure antMatchers after anyRequest
at org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry.antMatchers(AbstractRequestMatcherRegistry.java:122)
at com.windigital.config.WebSecurityConfig.configure(WebSecurityConfig.java:37)

网络安全配置:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

private final UserDetailsService userDetailsService;

@Autowired
public WebSecurityConfig(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}

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


@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().hasAnyRole("ADMIN", "USER")
.and()
.authorizeRequests().antMatchers("/login**").permitAll()
.and()
.formLogin().loginPage("/login").loginProcessingUrl("/loginAction").permitAll()
.and()
.logout().logoutSuccessUrl("/login").permitAll()
.and()
.csrf().disable();
}
}

这个 antMatcher 的问题:

.antMatchers("/login**")

请帮助我,告诉我这个configure()方法或给我链接。谢谢!

最佳答案

我已阅读本指南 securing-web并像这样创建 htt:

http.authorizeRequests()
.antMatchers("/index", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").loginProcessingUrl("/loginAction").permitAll()
.and()
.logout().logoutSuccessUrl("/login").permitAll()
.and()
.csrf().disable();

关于java - 配置问题(HttpSecurity http)错误 : Can't configure antMatchers after anyRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59755643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com