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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM