java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"報錯


出現問題的原因:

  內存用戶驗證時,Spring boot 2.0.1引用的security 依賴是 spring security 5.X版本,此版本需要提供一個PasswordEncorder的實例,否則后台匯報錯誤:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
並且頁面毫無響應。

 

解決方法:

創建PasswordEncorder的實現類MyPasswordEncoder。

代碼一:

 1 package com.mmall.demo;
 2 
 3 import org.springframework.security.crypto.password.PasswordEncoder;
 4 
 5 public class MyPasswordEncoder implements PasswordEncoder {
 6     @Override
 7     public String encode(CharSequence rawPassword) {
 8         return rawPassword.toString();
 9     }
10 
11     @Override
12     public boolean matches(CharSequence rawPassword, String encodedPassword) {
13         return encodedPassword.equals(rawPassword);
14     }
15 }
View Code

代碼二:

 

 1 package com.mmall.demo;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
 5 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 6 import org.springframework.security.config.annotation.web.builders.WebSecurity;
 7 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 8 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 9 
10 @Configuration
11 @EnableWebSecurity
12 public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
13 
14     @Override
15     protected void configure(AuthenticationManagerBuilder auth) throws Exception {
16         auth.inMemoryAuthentication().
17                 passwordEncoder(new MyPasswordEncoder()).
18                 withUser("admin").password("123456").roles("ADMIN");
19     }
20 
21     @Override
22     protected void configure(HttpSecurity http) throws Exception {
23        http.authorizeRequests()
24                .antMatchers("/").permitAll()
25                .anyRequest().authenticated()
26                .and()
27                .logout().permitAll()
28                .and()
29                .formLogin();
30        http.csrf().disable();
31     }
32 
33     @Override
34     public void configure(WebSecurity web) throws Exception {
35         web.ignoring().antMatchers("/js/**","/css/**","/image/**");
36     }
37 
38 
39 }
View Code

 


免責聲明!

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



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