1. 程式人生 > 其它 >SpringSecurity遇到問題:Can't configure antMatchers after anyRequest、There is no PasswordEncoder mapped for the id "null"

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"); }