Md5加密的两种用法

在学习springboot的过程中想要对登陆注册功能进行密码加密,故临时总结了两种md5加密方法

一、使用spring自带的简易md5加密

        1、对从注册页面获取的用户密码进行加密

String md5password= DigestUtils.md5DigestAsHex(user.getPassword().getBytes());

        2、加密成功后在登陆模块也加上这个语句即可对登录注册模块进行简单md5加密【注意:数据库的password长度设置长一点,否则密码加密后会导致页面报500】

        由于该md5加密过于简单可被破解,故我们可对传进来的密码最前面或者最后面加入一个空格符号,使其加密后稍微复杂一点。但该加密还是过于简单容易,故此讲解第二种方法。

二、使用springboot security安全框架进行md5盐值加密

        1、首先在pom.xml下导入依赖包

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
</dependency>

       2、 创建BCryptPasswordEncoder对象

BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        3、对获取到的密码进行加密

String md5password=passwordEncoder.encode(user.getPassword());

         4、注册账户的密码被md5盐值加密后,在登陆模块进行密码校验时需要通过下面的方法进行密码校验,返回的是一个Boolean值

boolean ps=passwordEncoder.matches(user.getPassword(),queryUser.getPassword());

         根据其返回的Boolean值来判断密码校验是否成功

        5、由于加入springboot security安全框架后访问路径都会弹出一个自带的登陆验证页面,可以在config包下创建一个类来去掉该页面(至于其他的解决方案可以自己去查询)



import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf()
                .disable()
                .authorizeRequests()
                .anyRequest()
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }

}

         完结~

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值