Spring security 自定义密码验证(二)

本文介绍如何在Spring Security中实现自定义密码验证,包括使用PasswordEncoder接口和AuthenticationProvider。虽然实现PasswordEncoder方便,但可能不如AuthenticationProvider灵活,尤其是在处理数据库中的salt时。文章提到了Spring Security内置的Md5PasswordEncoder和ShaPasswordEncoder作为加密选项,并简要提及了配置和相关类的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第二种自定义密码验证的方式是,实现PasswordEncoder,但个人感觉没实现AuthenticationProvider更加可扩展,方便。因为我加不了存在数据库中的salt进去。

在PasswordEncoder的实现类当中,可以选择用spring security自带的Md5PasswordEncoder,ShaPasswordEncoder进行加密。

Spring security配置主类:

/**
 * Created by fjc on 2018-04-17.
 */
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Autowired
    @Qualifier("customUserDetailsService")
    UserDetailsService userDetailsService;

    @Autowired
    CustomSuccessHandler customSuccessHandler;


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new CustomPasswordEncoder();
    }

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(userDetailsService);
        authenticationProvider.setPasswordEncoder(passwordEncoder());
        re
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值