SpringSecurity常见报错处理:Can't configure antMatchers after anyRequest、There is no PasswordEncoder mapped for the id "null"

一、报错: java.lang.IllegalStateException: Can‘t configure antMatchers after anyRequest

  启动springboot项目,直接编译报错,内容:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: 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.NullPointerException

  下面还有报错:

nested exception is java.lang.IllegalStateException: Can‘t configure antMatchers after anyRequest

  解决方案:

  开始配了半天,一直报这个:Can't configure antMatchers after anyRequest...的错误,上StackOverflow看了以下别人的,照着代码调整了下还是没有解决。然后想难道是super.configure(http)里搞的鬼,点进去看了下,结果还真是,就赶紧去掉了。

  子类重写方法时, 如果用不到父类的方法, 一定要去掉自动生成的super。

二、认证密码没加密,报错:java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

  认证密码需要加密才行,如果没有使用下面的密码加密,就会报上面错误

// 认证的密码得加密才行
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
    .withUser("gwf").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
原文地址:https://www.cnblogs.com/goloving/p/14880518.html