MD5:
在我们日常开发中,有一些表字段,不适合明文存储,比如各个系统登录所使用的密码,这样做可以防止,一旦数据库遭遇黑客攻击,不至于导致密码等重要数据的外泄。由于现存的Md5字典中可以查询出一些常见的Md5密文,为了防止Md5密文被破解,在加密过程中,还需要对Md5密文进行加“盐”操作,从而使Md5密文无法被轻易破解。
代码实现:
1 采用springboot +mybiats +md5实现简单登录注册
首先pom文件
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.apache.commons
commons-lang3
3.6
mysql
mysql-connector-java
5.1.47
org.projectlombok
lombok
1.18.6
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
2 application.properties
#mysql连接
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#编码格式
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.webflux.static-path-pattern=classpath:/templates/index.html
spring.resources.static-locations=classpath:/resources/,classpath:/static/,classpath:/templates/,classpath:/config/
mybaits.type-aliases-package=com.kd.springboot_md5
mybaits.mapper-locations=classpath:com.kd.springboot_md5.dao
spirng.mvc:default-servlet-handler
#将themilef的默认缓存禁用,热加载生效
spring.thymeleaf.cache=false
#开启驼峰命名
mybatis.configuration.mapUnderscoreToCamelCase=true
#引入配置文件
mybatis.configuration.map-underscore-to-camel-case=true
#打印sql
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
3 MD5工具类
package com.kd.springboot_md5.common;
import org.springframework.util.DigestUtils;
import java.security.MessageDigest;
/**
* @author Kodak
* @version 1.0
* @create 2019/08/04/10:30
*/
public class MD5Util {
/***
* MD5加码 生成32位md5码
*/
public static String string2MD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++) {
byteArray[i] = (byte) charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16){
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 加密解密算法 执行一次加密,两次解密
*/
public static String convertMD5(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}
// 测试主函数
public static void main(String args[]) {
String s = "Kodak";
System.out.println("原始:" + s);
System.out.println("MD5后:" + string2MD5(s));
System.out.println("加密的:" + convertMD5(s));
System.out.println("解密的:" + convertMD5(convertMD5(s)));
}
}
4 controller
package com.kd.springboot_md5.controller;
import com.kd.springboot_md5.common.MD5Util;
import com.kd.springboot_md5.dao.UserInfoDao;
import com.kd.springboot_md5.entity.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
/**
* @author Kodak
* @version 1.0
* @create 2019/08/04/10:37
*/
@Controller
public class LoginController {
@Resource
private UserInfoDao userInfoDao;
@RequestMapping("register")
public String register(UserInfo userInfo){
String pwd= MD5Util.string2MD5(userInfo.getPassword());
userInfo.setPassword(pwd);
userInfoDao.insertUser(userInfo);
return "login";
}
@GetMapping("toregister")
public String toregister(){
return "register.html";
}
@RequestMapping("login")
public String login(UserInfo userInfo){
String pwd=MD5Util.convertMD5(MD5Util.convertMD5(userInfo.getPassword()));
userInfo.setPassword(pwd);
userInfoDao.queryUser(userInfo);
return "index";
}
}
5 entity实体bean 使用了lombok插件生成get/set方法
package com.kd.springboot_md5.entity;
import lombok.Data;
/**
* @author Kodak
* @version 1.0
* @create 2019/08/04/11:27
*/
@Data
public class UserInfo {
private int id;
private String username;
private String password;
}
6 dao 层
package com.kd.springboot_md5.dao;
import com.kd.springboot_md5.entity.UserInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @author Kodak
* @version 1.0
* @create 2019/08/04/11:29
*/
public interface UserInfoDao {
@Select("select * from user_info where username=#{username} and password=#{password}")
public List queryUser(UserInfo userInfo);
@Insert("insert into user_info values(null,#{username},#{password})")
public boolean insertUser(UserInfo userInfo);
}
结果
数据库保存的密码