SpringSecurity遇到問題:Can't configure antMatchers after anyRequest、There is no PasswordEncoder mapped for the id "null"
阿新 • • 發佈:2021-06-13
一、報錯: 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
解決方案:
二、認證密碼沒加密,報錯: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");
}