首页 / JAVA / java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2303字,纯文字阅读大概需要4分钟。
内容图文
出现问题的原因:
内存用户验证时,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
内容总结
以上是互联网集市为您收集整理的java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错全部内容,希望文章能够帮你解决java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。