在学习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();
}
}
完结~