springsecurity进行登陆认证时There is no PasswordEncoder mapped for the id “null“ 错误处理

在升级到SpringSecurity6.0后,遇到登陆认证错误NoPasswordEncodermappedforidnull.原因是需要指定密码加密方式。解决方案是在DaoAuthenticationProvider中添加PasswordEncoder,例如使用BCryptPasswordEncoder。配置完成后,认证过程将能正确处理加密的密码。
摘要由CSDN通过智能技术生成

问题描述

在使用springsecurity6.0版本进行登陆认证时,出现如下错误:

There is no PasswordEncoder mapped for the id "null"

在这里插入图片描述


原因分析:

在springsecurity5.0之后引入了许多加密方式,因此在校验是需要指定一种加密方式
在我的securityConfig中,authenticationProvider的代码如下:

@Bean
    public AuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
        return daoAuthenticationProvider;
    }

因此在构建是需要添加加密方式


解决方案:

在构建的authenticationProvider中需要填加密码加密方式:

代码如下:

@Bean
    public AuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return daoAuthenticationProvider;
    }

附上我的Springsecurityconfig全部代码:

代码如下:

package com.lp.framework.security.config;

import com.lp.framework.security.core.SpringSecurityUserDetailsServiceImpl;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

/**
 * @author LuoPing
 * @project IntelliJ IDEA
 * @Package lpblog
 * @Date 2023/2/18 23:57
 */
@Configuration
@EnableWebSecurity
public class SpringSecurityConfigurer{

    @Resource
    SpringSecurityUserDetailsServiceImpl userDetailsService;
  
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        //设置请求必须进行权限认证
        http.authorizeHttpRequests(authz ->{authz
                        .requestMatchers("/login").permitAll()// 放行登录接口
                        .anyRequest().authenticated();// 其余的都需要权限校验
                })
                .authenticationProvider(authenticationProvider())
                //关闭csrf防御,相似于防火墙,不关闭上面的设置不会真正生效。// 防跨站请求伪造
                .csrf().disable();

        return http.build();
    }

    @Bean
//    密码加密,不然没法做密码加密对比
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    /**
     * 获取AuthenticationManager(认证管理器),登录时认证使用
     */
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
//        return authenticationConfiguration.getAuthenticationManager();
//        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
//        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
//        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return config.getAuthenticationManager();
    }

    @Bean
    public AuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return daoAuthenticationProvider;
    }
}

附:SpringSecurity6.0版本学习视频链接 (外语视频)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十一*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值