解决There is no PasswordEncoder mapped for the id “null“问题

这篇具有很好参考价值的文章主要介绍了解决There is no PasswordEncoder mapped for the id “null“问题。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

springsecurity从4.2升级到5.0之后,做简单的登录,出现如下所示的错误:

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null" 	at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder .matches(DelegatingPasswordEncoder.java:238) 	at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:198)

根据官方文档的资料和网上解决办法,需要做一些修改。

默认情况下与4.2版本不同的是,springsecurity5.0密码加密方式采用了bcrypt的方式,而且密码直接配置在xml文件中,不光是需要使用BCryptPasswordEncoder来加密,还需要指定一个encodingId,如果不指定,就会报出如题所示的错误。

我们可以看看PasswordEncoderFactories.createDelegatingPasswordEncoder()方法:

解决There is no PasswordEncoder mapped for the id “null“问题

这就是为什么说默认情况下使用的bcrypt方式加密。

知道了用什么方式,我们就可以来改进了。

1、密码不加密,和springsecurity4.2一样,使用明文密码,那就需要配置密码验证方式为noop,配置如下。

<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder"></beans:bean>

在spring-security.xml配置文件中配置以上bean即可。

密码配置如下:

<user name="admin" password="123456" authorities="ROLE_USER"/>

这样,默认密码就不需要进行加密,原样传入,原样匹配。这种配置方式密码前面,不需要加上{noop} 

2、使用默认的密码加密方式,需要改动密码,并且密码格式如下:

{bcrypt}$2a$10$rY/0dflGbwW6L1yt4RVA4OH8aocD7tvMHoChyKY/XtS4DXKr.JbTC

在通过bcrypt方式加密之后的密文,前面加上{bcrypt}。

这里另外介绍两种方式来通过bcrypt方式加密,直接上源码:

package com.xxx.security.test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;

public class PasswordEncoder {
    public static void main(String[] args) {
        org.springframework.security.crypto.password.PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        String password = "123456";
        String password2 = encoder.encode(password);
        System.out.println(password2);
        System.out.println(encoder.matches(password, password2));
        BCryptPasswordEncoder encoder2 = new BCryptPasswordEncoder();
        String password3 = encoder2.encode(password);
        System.out.println(password3);
        System.out.println(encoder2.matches(password, password3));
    }  
}

运行以上代码,打印结果如下:

解决There is no PasswordEncoder mapped for the id “null“问题

 眼尖的你,可能一下子就发现了一个小问题,通过bcrypt方式加密的密文,除了{bcrypt}外两次结果不一样,怎么会这样?

和以往加密方式不同的是,springsecurity5.0以上版本对bcrypt加密方式进行了随机数处理,所以每次产生的结果都不一样。不管是哪种方式,我们如果使用默认的加密方式,就需要在xml中配置密码为如下的样子。记得前面有{bcrypt}。

{bcrypt}$2a$10$rY/0dflGbwW6L1yt4RVA4OH8aocD7tvMHoChyKY/XtS4DXKr.JbTC

报错的意思是说我们没有指定一个encodingId,最终在encoders map中没有匹配到encodingId。

如果我们需要使用MD5方式加密,我们就需要指定如下所示的密码:

{MD5}{pwegM3ORx1OzOc4eFzVztSPdeZit0BZPWTA5JB1Juc0=}dda279a014895996e55957976ec4cd36

同理,如果我们需要像springsecurity4.2那样,密码明文,我们可以这样配置:

<user name=”admin” password=”{noop}123456″ authorities=”ROLE_USER”/>

也无需像第一种方式那样在配置文件中增加NoOpPasswordEncoder的配置了。文章来源地址https://www.toymoban.com/news/detail-406907.html

到了这里,关于解决There is no PasswordEncoder mapped for the id “null“问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java.sql.SQLException: There is no DataSource named ‘null‘

    当配置dataSource后,即使配置文件中已经指定了JobStoreTX,实际还是使用LocalDataSourceJobStore。 application.yml配置 修改之处如下:

    2024年02月13日
    浏览(32)
  • Mybatis报错:There is no getter for property named ‘xxx‘ in ‘class xxx 的解决办法

    在mybatis中遇到这样的报错,There is no getter for property named ‘xxx’ in \\\'class xxx ,这篇博客给出了解决办法。 There is no getter for property named ‘username’ in ‘class com.zxy.pojo.User’ 最先想着是不是User实体类没有写 Getter and Setter 方法,可是发现写了。 发现数据库表字段 category_id 与实体

    2024年02月03日
    浏览(41)
  • There is no getter for property named ‘xxx‘ in ‘class java.lang.xxxx‘解决办法

    报错最前面 内容 报错原因: 使用了自定义SQL,可能含有特殊的函数或者复杂的语法,因而不被JSqlParser(SQL解析器)所支持(无法添加租户id之类的字段),以致抛出了JSQLParserException。 解决方法: Failed to process, please exclude the tableName or statementId 翻译过来就是:处理失败,请将表名

    2024年02月04日
    浏览(38)
  • [AxiosError]: There is no suitable adapter to dispatch the request since :- adapter xhr is not suppo

    最近在写一个node项目时,webpack打包之后运行打包的js文件报错:  [AxiosError]: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build     at we (C:UserslzzDocuments网盘脚本distbundle.js:2:32230)     at Ce.Se (C:Userslzz

    2024年02月03日
    浏览(43)
  • Docker无法运行java虚拟机报错There is insufficient memory for the Java Runtime

    最近遇到镜像导入到docker后无法启动容器的问题,但是上传到别的服务器上面又可以正常启动容器,报错信息大概如下: 看上去是说服务器资源不足,内存不够,但是执行free -h查看了一下内存并没有不足。 最后知道这个报错是由于docker创建的容器内核版本太低的问题,执行

    2024年01月20日
    浏览(38)
  • nacos运行报错:There is insufficient memory for the Java Runtime Environment to continue.

    内存不够用了! 查看内存使用情况: 查看运行的进程: PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小越优先被执行 NInice:值 VIRT:进程占用的虚拟内存 RES:进程占用的物理内存 SHR:进程使用的共享内存 S:进程的状态。S表示休眠,R表示正在运行,Z表示僵死状

    2024年02月03日
    浏览(30)
  • maven打包失败:the pom for XXX is missing, no dependency information available 问题解决

    问题描述:springcloud项目,idea打包pacake、compile时报错,THE POM for ... is missing,no dependency information available,此时清理缓存,和clean之后还是会报这个错。 查询报错信息是因为pom文件丢失才会报这个错,但是项目中pom文件是存在的,并非丢失。 由于是多项目管理,项目是有相互依

    2024年02月11日
    浏览(37)
  • 解决RuntimeError: CUDA error: no kernel image is available for execution on the deviceCUDA

    解决RuntimeError: CUDA error: no kernel image is available for execution on the deviceCUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect. 在服务器复现代码的时候,遇到了上述错误,解决办法如下。 .bashrc文件在服务器上初始页面的配置文件的地方 参考:

    2024年02月16日
    浏览(32)
  • [已解决]RuntimeError: CUDA error: no kernel image is available for execution on the device

    在ubuntu服务器上用python炼丹的时候遇到的两个问题,一个warning和一个runtimeErro,我的环境是用conda配置的,我就切换了一下环境,然后切回来就报这两个错误,期间啥也没干,之前重新安装opencv疯狂报错也是这种样子的。 warning warning:NVIDIA GeForce RTX 3090 with CUDA capability sm_86

    2024年02月02日
    浏览(53)
  • RuntimeError:CUDA error:no kernel image is available for execution on the device报错解决(亲测)

    调试Transformer网络,安装完timm包之后,运行程序时报错 CUDA error:no kernel image is available for execution on the device ,如图所示: 网上对于该错误说啥的都有,因为这是第一次遇到这个错误,之前训练CNN也正常,排除显卡算力低,不支持高版本CUDA问题。看来看去,这位博主说的有道

    2024年02月11日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包