使用springboot完成密码的加密解密
现今对于大多数公司来说,信息安全工作尤为重要,就像京东,阿里巴巴这样的大公司来说,信息安全是最为重要的一个话题,举个简单的例子:
就像这样的密码公开化,很容易造成一定的信息的泄露。所以今天我们要讲的就是如何来实现密码的加密和解密来提高数据的安全性。
在这首先要引入springboot融合mybatis的知识,如果有这方面不懂得同学,就要首先看一看这方面的知识:
推荐大家一个比较好的博客: 程序猿DD-翟永超 http://blog.didispace.com/springbootmybatis/
为了方便大家的学习,我直接将源代码上传:
1.pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2 <modelVersion>4.0.0</modelVersion>
3 <groupId>com.ninemax</groupId>
4 <artifactId>spring-Login-test</artifactId>
5 <version>0.0.1-SNAPSHOT</version>
6 <packaging>war</packaging>
7
8 <parent>
9 <groupId>org.springframework.boot</groupId>
10 <artifactId>spring-boot-starter-parent</artifactId>
11 <version>1.3.2.RELEASE</version>
12 <relativePath/>
13 </parent>
14
15 <properties>
16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17 <java.version>1.8</java.version>
18 </properties>
19
20 <dependencies>
21
22 <dependency>
23 <groupId>org.springframework.boot</groupId>
24 <artifactId>spring-boot-starter</artifactId>
25 </dependency>
26
27 <dependency>
28 <groupId>org.springframework.boot</groupId>
29 <artifactId>spring-boot-starter-test</artifactId>
30 <scope>test</scope>
31 </dependency>
32
33 <dependency>
34 <groupId>org.mybatis.spring.boot</groupId>
35 <artifactId>mybatis-spring-boot-starter</artifactId>
36 <version>1.1.1</version>
37 </dependency>
38
39 <dependency>
40 <groupId>org.springframework.boot</groupId>
41 <artifactId>spring-boot-starter-web</artifactId>
42 </dependency>
43
44 <dependency>
45 <groupId>commons-dbcp</groupId>
46 <artifactId>commons-dbcp</artifactId>
47 </dependency>
48
49 <dependency>
50 <groupId>com.oracle</groupId>
51 <artifactId>ojdbc14</artifactId>
52 <version>10.2.0.3.0</version>
53 </dependency>
54
55
56 <dependency>
57 <groupId>org.springframework.boot</groupId>
58 <artifactId>spring-boot-starter-thymeleaf</artifactId>
59 </dependency>
60
61
62 </dependencies>
63
64 <build>
65 <plugins>
66 <plugin>
67 <groupId>org.springframework.boot</groupId>
68 <artifactId>spring-boot-maven-plugin</artifactId>
69 </plugin>
70 <plugin>
71 <groupId>org.apache.maven.plugins</groupId>
72 <artifactId>maven-surefire-plugin</artifactId>
73 <configuration>
74 <skip>true</skip>
75 </configuration>
76 </plugin>
77 </plugins>
78 </build>
79
80
81 </project>
2. AppTest.java
package com; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class AppTest {
public static void main(String[] args) {
SpringApplication.run(AppTest.class, args);
} }
3.User.java
package com.entity; public class User { private String username;
private String password; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
} }
4.UserController.java
package com.controller; import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.dao.UserDao;
import com.entity.User; @Controller
public class UserController { @Autowired
private UserDao userDao; @RequestMapping("/regist")
public String regist() {
return "regist";
} @RequestMapping("/login")
public String login() {
return "login";
} @RequestMapping("/success")
public String success(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password"); userDao.save(username, password);
return "success";
} @RequestMapping("/Loginsuccess")
public String successLogin(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password"); ///123456
User user = userDao.findByUname(username);
if(user.getPassword().equals(password)) {
return "successLogin";
}
return "failure";
}
}
5.UserDao.java
package com.dao; import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import com.entity.User; @Mapper
public interface UserDao {
@Insert("INSERT INTO LOGIN_NINE VALUES(#{username}, #{password})")
void save(@Param("username")String username,@Param("password")String password); @Select("SELECT * FROM LOGIN_NINE WHERE username= #{username}")
User findByUname(@Param("username")String username);
}
6.application.properties
spring.datasource.url=jdbc:oracle:thin:@10.236.4.251:1521:orcl
spring.datasource.username=hello
spring.datasource.password=lisa
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
application.properties
7.还有一些静态HTML
(1.)regist.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>注册</title> <style type="text/css">
h1 {
text-align:center;
font-size:35px;
color:red;
}
div {
text-align:center;
}
div input {
margin:10px;
}
</style>
</head>
<body>
<h1>注册账号</h1>
<div>
<form action="success" method="post">
用户名<input type="text" name="username"/> <br/>
密码<input type="password" name = "password"/> <br/>
<input type="submit" value="提交"/>
<input type="reset"/> </form>
</div>
</body>
</html>
(2.)login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>登录</title> <style type="text/css">
h1 {
text-align:center;
font-size:35px;
color:red;
}
div {
text-align:center;
}
div input {
margin:10px;
} </style>
</head>
<body>
<h1>欢迎登录</h1>
<div>
<form action="Loginsuccess" method="post">
请输入用户名<input type="text" name="username"/> <br/>
请输入密码<input type="password" name = "password"/> <br/>
<input type="submit" value="提交"/>
<input type="reset"/> <br/>
<a href="/regist">注册账号</a>
</form>
</div>
</body>
</html>
(3.)success.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>注册成功</title>
<style type="text/css">
h1 {
text-align:center;
font-size:60px;
color:green;
}
span {
font-size:30px;
color:green;
}
</style>
</head>
<body>
<h1>注册成功</h1>
<a href="/login">返回登录</a>
</body>
</html>
(4.)failure.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>登录失败</title> </head>
<body>
登录失败
</body>
</html>
(5.)successLogin.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>成功</title>
</head>
<body>
success
</body>
</html>
代码的格式如下:
完成了这一步的话首先运行一下AppTest看是否出错,如果有错,自己找原因,这里就不和大家讨论了,写了这么多,才要要进入正题了
本文采取的是EDS的加密解密方法,方法也很简单,不用添加额外的jar包,只需要在UserController上做出简单的修改就可以了:
*****UserController.java
package com.controller; import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.dao.UserDao;
import com.entity.User; @Controller
public class UserController { @Autowired
private UserDao userDao; @RequestMapping("/regist")
public String regist() {
return "regist";
} @RequestMapping("/login")
public String login() {
return "login";
} /**
* EDS的加密解密代码
*/
private static final byte[] DES_KEY = { 21, 1, -110, 82, -32, -85, -128, -65 };
@SuppressWarnings("restriction")
public static String encryptBasedDes(String data) {
String encryptedData = null;
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(DES_KEY);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(deskey);
// 加密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
// 加密,并把字节数组编码成字符串
encryptedData = new sun.misc.BASE64Encoder().encode(cipher.doFinal(data.getBytes()));
} catch (Exception e) {
// log.error("加密错误,错误信息:", e);
throw new RuntimeException("加密错误,错误信息:", e);
}
return encryptedData;
}
@SuppressWarnings("restriction")
public static String decryptBasedDes(String cryptData) {
String decryptedData = null;
try {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec deskey = new DESKeySpec(DES_KEY);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(deskey);
// 解密对象
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// 把字符串进行解码,解码为为字节数组,并解密
decryptedData = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cryptData)));
} catch (Exception e) {
throw new RuntimeException("解密错误,错误信息:", e);
}
return decryptedData;
} @RequestMapping("/success")
public String success(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password");
String s1 = encryptBasedDes(password);
userDao.save(username, s1);
return "success";
} @RequestMapping("/Loginsuccess")
public String successLogin(HttpServletRequest request) {
String username = request.getParameter("username");
String password = request.getParameter("password"); ///123456
User user = userDao.findByUname(username);
if(decryptBasedDes(user.getPassword()).equals(password)) {
return "successLogin";
}
return "failure";
}
}
此时,直接运行Apptest.java,然后在浏览器输入地址:localhost:8080/regist 注册新的账号(我输入的是用户名:小明 密码:123456),如图
此时查看数据库信息
你就会发现密码实现了加密
当然,下次登陆的时候直接输入相应的账号和密码即可完成登录,实现了解码的过程
不知道大家完成的怎么样了,如果出现问题,可以在下面进行留言,我会为大家进行解答.
使用springboot完成密码的加密解密的更多相关文章
- springMVC web项目 对访问数据库的用户名密码进行加密解密
在使用springMVC开发web项目中,数据库的用户名,密码一般都是配置在.properties文件中 然后在通过.xml配置文件引入.properties的变量,例如 在config.proper ...
- java关于密码的加密解密
密码的加密方法有多种,常见的为Aes.Md5 Aes加密,可逆. 其中,Md5加密是采用了散列算法,也就是哈希算法,可以进行多次散列加密.Md5加密是不可逆的,无法解密. MD5是不可逆的单向加密方式 ...
- django删除表重建&修改用户密码&base64加密解密字符串&ps aux参数说明&各种Error例子
1.django的queryset不支持负索引 AssertionError: Negative indexing is not supported. 2.django向前端JavaScript传递列 ...
- Springboot 使用过滤器进行加密解密(二)
之前写过一篇关于过滤器实现加密解密功能的文章,但是在实际开发业务中发现,还是有一些问题的,在此特地说明. 第一:过滤器走两遍的问题: 1.过滤器上,添加了两个注解 第一个:@Compent 将此F ...
- 移位密码(加密+解密)C++实现
移位密码 加密C=Ek(m)=m+k mod 26 解密m=Dk(m)=c-k mod 26 密钥空间|k|=26=|c|=|m| #include<iostream> #include& ...
- [原创]K8Cscan4.0之Base64/HEX密码批量加密解密插件以及源码
前言 今天抽空更新了Cscan,新增对C#编译的EXE动态调用,新增对PowerShell脚本动态调用(无论是否安装PowerShell) 增加一个字符串列表str.txt,用于存放任意字符串,比如帐 ...
- 各种加密解密函数(URL加密解密、sha1加密解密、des加密解密)
原文:各种加密解密函数(URL加密解密.sha1加密解密.des加密解密) 普通hash函数如md5.sha1.base64等都是不可逆函数.虽然我们利用php可以利用这些函数写出可逆函数来.但是跨语 ...
- Spring-Boot数据库密码加密配置
springboot集成mysql/oracle时需要在yml/properties中配置数据库信息,用户名密码是肯定有的,所以就涉及到密码的加密,当然不加密也是可以的,正如某位大佬所说的,不加密就像 ...
- SpringBoot项目配置文件中密码的加密
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/15565862.html 版权声明:本文为博主原创文章,转载请附上博文链接! 公众号:追梦1819 ...
随机推荐
- 在Pycharm中使用jupyter笔记本
在Pycharm中使用jupyter笔记本 我在Pycharm中使用jupyter笔记本,发现新的Jupyter更新中,增加了令牌. 随着创建的虚拟环境启动的所有设置,并将URL设置为127.0.0. ...
- nodejs后台集成富文本编辑器(ueditor)
1 下载ueditor nodejs版本 2 复制public目录下面的文件 到项目静态资源public文件夹下 3 在项目根目录创建ueditor文件夹 要复制进来的内容为 4 在根目录的 uedi ...
- python 的日志logging模块学习
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...
- 【特效】select美化
select的默认样式往往很丑,为保证页面样式风格统一,需要对select进行美化.虽然其美化的插件很多,一搜一大把,但是需要引入长长的css文件和js文件实在是件头痛的事.其实select的实现原理 ...
- 张高兴的 Windows 10 IoT 开发笔记:DHT11 温湿度传感器
GitHub : https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/DHT11Demo
- 聊聊Vue.js组件间通信的几种姿势
写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...
- 【微信小程序开发】秒懂,架构及框架
今天1024程序员节,写文章庆祝!!! 今天的文章是讲微信小程序开发的,按理解把架构与框架说说.有不对之处请大神指点…… 微信小程序与web应用很像,但是原理不同,微信小程序是运行在微信应用内的,不是 ...
- ALSA和Pulseaudio
小记一下,Deadbeef如果使用ALSA作为音频输出的话,会导致其他说有使用pulseaudio的程序[如Chrome]没声音.....[但是SMplayer使用ALSA的话不会...]
- (转)Java开发中的23种设计模式详解
原文出自:http://blog.csdn.net/zhangerqing 一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型 ...
- Nodejs.热部署方法
在开发中我们修改了一点代码后要去重启服务器才能看到结果,为了省去这个过程我们以往经常使用热部署代码的方法 下面是使用"supervisor"来达到热部署能力的方法: sudo np ...