springsecurity 提示:There is no PasswordEncoder mapped for the id "null"

2023-11-06

用户角色权限系统(springboot + springsecurity + mysql) 提示如下错误信息:

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

    at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:238) ~[spring-security-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:198) ~[spring-security-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$LazyPasswordEncoder.matches(WebSecurityConfigurerAdapter.java:593) ~[spring-security-config-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:86) ~[spring-security-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:166) ~[spring-security-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174) ~[spring-security-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) ~[spring-security-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:124) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178) ~[spring-security-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_40]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_40]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.32.jar:8.5.32]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_40]

产生错误的代码片段原因:

package com.zzg.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;

import com.zzg.security.user.CustomerUserService;

@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
	@Bean
	UserDetailsService customUserService() {
		return new CustomerUserService();
	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		// 错误代码片段,未设置密码编辑器
		auth.userDetailsService(customUserService())
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
	//  允许所有用户访问"/"和"/index.html"
		         http.authorizeRequests()
		                 .antMatchers("/", "/index.html").permitAll()
		                 .anyRequest().authenticated()   // 其他地址的访问均需验证权限
		                 .and()
		                 .formLogin()
		                 .loginPage("/login.html")   //  登录页
		                 //.successForwardUrl("/index") // 登入成功后,跳转至指定页面
		                 .defaultSuccessUrl("/index")   // 访问指定页面,用户未登入,跳转至登入页面,如果登入成功,跳转至用户访问指定页面,用户访问登入页面,默认的跳转页面
		                 .failureUrl("/login-error.html").permitAll()
		                 .and()
		                 .logout()
		                 .logoutSuccessUrl("/index.html"); 
	}
}

正确的代码片段:

package com.zzg.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;

import com.zzg.security.user.CustomerUserService;

@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
	@Bean
	UserDetailsService customUserService() {
		return new CustomerUserService();
	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		// 设置密码为明文
		auth.userDetailsService(customUserService()).passwordEncoder(NoOpPasswordEncoder.getInstance());
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
	//  允许所有用户访问"/"和"/index.html"
		         http.authorizeRequests()
		                 .antMatchers("/", "/index.html").permitAll()
		                 .anyRequest().authenticated()   // 其他地址的访问均需验证权限
		                 .and()
		                 .formLogin()
		                 .loginPage("/login.html")   //  登录页
		                 //.successForwardUrl("/index") // 登入成功后,跳转至指定页面
		                 .defaultSuccessUrl("/index")   // 访问指定页面,用户未登入,跳转至登入页面,如果登入成功,跳转至用户访问指定页面,用户访问登入页面,默认的跳转页面
		                 .failureUrl("/login-error.html").permitAll()
		                 .and()
		                 .logout()
		                 .logoutSuccessUrl("/index.html"); 
	}
}

springsecurity 实现自定义密码编码器,实现org.springframework.security.crypto.password.PasswordEncoder 接口,springsecurity 默认实现密码编码器,如下截图:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

springsecurity 提示:There is no PasswordEncoder mapped for the id "null" 的相关文章

随机推荐

  • idea2021版本新建web项目(详细教程)

    打开idea右上角的文件 新建项目 选中java模块 下一步 取名 下一步 打开后是个空白 到这一步 右键选中untitled打开添加框架支持 选中web应用程序 一定要勾选创建web xml 然后点击确定 接下来 找到右上角的添加配置 点
  • matplotlib绘图横坐标或纵坐标文本显示不全

    import matplotlib pyplot as plt x 1 2 3 4 y 1 4 9 6 labels Frogs Hogs Bogs Slogs plt plot x y You can specify a rotation
  • unity使用Tcp/UDP协议网络通信实现(Socket简单应用)

    一 TCP协议 服务器端 1 打开vs 创建一个c 的控制台应用程序 代码如下 记得把ip换成自己电脑ip using System using System Collections Generic using System Linq us
  • INS/GNSS组合导航(七)角速度坐标系变换与欧拉角转换

    注意 角速度与角速度率有严格区别 反映在以下两点 正交的三个角速度 角速度矢量 与欧拉角速率之间的关系如下 欧拉角速率并不是纯粹的正交矢量 而是一个与旋转顺序相关而且非正交的三个矢量 积分欧拉角速率得到的是欧拉角的大小 又称卡丹角 积分角速
  • 股票预测_机器学习预测股票

    2 机器学习技术综述 集成多种人工智能系统的机器学习技术尝试通过对历史数据的学习提取数据模式 这一过程被称为训练或学习 其目的在于实现后续基于新数据的预测 Xiao Xiao Lu and Wang 2013 pp 99 100 使用机器学
  • 【Java】类和对象

    前言 面向对象编程的特性 封装 继承 多态 在Java中 最基本的封装单元是类 一个类的定义为具有相似特征对象的一种抽象 根据类的继承 父类只定义各子类所需的属性和方法 多态是类中同一名称的行为 可以有多种不同的功能 文章目录 前言 一 类
  • Mybatis Generator 配置详解

    许多人在Java项目中都会到使用Mybatis Generator这个工具包 这里把这个工具的配置完整列一下 gt
  • spss常态检验_利用SPSS检验数据是否符合正态分布

    利用SPSS检验数据是否符合正态分布 正态分布也叫常态分布 在我们后面说的很多东西都需要数据呈正态分布 下面的图就是正态分布曲线 中间隆起 对称向两边下降 下面我们来看一组数据 并检验 期初平均分 数据是否呈正态分布 此数据已在SPSS里输
  • Sentinel-持久化

    直接使用dashboard和sentinel配置各种规则时 默认是存在了内存中 如果服务器重启那么数据就会丢失 从而Sentinel提供了5中持久化的方式 将各种配置数据进行持久化 若服务器重启就重新加载持久化的数据 防止数据丢失 1 持久
  • 使用adb查看安装包的apk路径与清除安装包数据与缓存操作实例

    adb shell pm path
  • 【Stata】CGSS数据清理:Codebook速成法

    对数据使用者来说 了解一个调查数据基本情况的常见途径就是查看该数据的codebook 对数据所有者 提供方来说 制作一份详细的codebook是其数据管理工作中不可或缺的一环 2016年上半年CGSS项目组把CGSS2003年到CGSS20
  • 解决Ubuntu安装后无法联网的问题-网卡驱动为安装成功

    转载自 http blog csdn net ifmvo article details 54023628 t0 起因 屁话 最近由于公司电脑不够用 所以暂时使用自己的笔记本做开发 i5 4G win7这配置看个视频 听个音乐还好 可我是做
  • 【动手学习pytorch笔记】9.卷积神经网络基础(卷积层,填充和步长,多输入输出通道,池化层)

    卷积层 二维互相关运算 import torch from torch import nn from d2l import torch as d2l def corr2d X K 计算二维互相关运算 h w K shape Y torch
  • 【LeetCode刷题】94、二叉树的中序遍历(C++)

    94 二叉树的中序遍历 原题连接 https leetcode cn problems binary tree inorder traversal 问题描述 给定一个二叉树的根节点 root 返回 它的 中序 遍历 示例 1 输入 root
  • react——事件绑定

    react有两种写法 一种是类组件 也叫有状态组件 一种是函数式组件 也叫无状态组件 而后引入了react hooks 函数式组件也能修改状态 在这两种组件中的事件绑定的原理一样 写法不一样 这篇文章主要就是记录这两种组件中的事件绑定写法
  • 迁移学习一、基本使用

    非完全原创 但遗失了原文链接 看到可更改 迁移学习 一 迁移学习方法介绍 1 微调网络的方法 微调网络的方法实现迁移学习 更改最后一层全连接 并且微调训练网络 2 将模型看成特征提取器 将模型看成特征提取器 如果一个模型的预训练模型非常的好
  • 实现model中的文件上传FTP(一)

    由于在django的model中配置了filefield或者imagefield配置了upload to参数只能将用户上传的文件上传到项目本地 就算重定向到项目外也只是直接读取文件系统 这样对未来的项目迁移或者资源管理都会造成很大的困扰 而
  • HJ2 计算某字符出现次数

    知识点字符串哈希 描述 写出一个程序 接受一个由字母 数字和空格组成的字符串 和一个字符 然后输出输入字符串中该字符的出现次数 不区分大小写字母 数据范围 1 n 1000 输入描述 第一行输入一个由字母和数字以及空格组成的字符串 第二行输
  • 设计模式梳理——访问者模式

    一 概述 访问者模式 Visitor 表示一个作用于某对象结构中的各元素的操作 它使你可以在不改变个元素的类的前提下作用于这些元素的新操作 二 UML图示 三 代码实现 我们都知道财务都是有账本的 这个账本就可以作为一个对象结构 而它其中的
  • springsecurity 提示:There is no PasswordEncoder mapped for the id "null"

    用户角色权限系统 springboot springsecurity mysql 提示如下错误信息 java lang IllegalArgumentException There is no PasswordEncoder mapped