這篇文章主要介紹了Springboot實現(xiàn)密碼加密解密的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供錫林郭勒盟網(wǎng)站建設、錫林郭勒盟做網(wǎng)站、錫林郭勒盟網(wǎng)站設計、錫林郭勒盟網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、錫林郭勒盟企業(yè)網(wǎng)站模板建站服務,十余年錫林郭勒盟做網(wǎng)站經驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
現(xiàn)今對于大多數(shù)公司來說,信息安全工作尤為重要,就像京東,阿里巴巴這樣的大公司來說,信息安全是最為重要的一個話題,舉個簡單的例子:
就像這樣的密碼公開化,很容易造成一定的信息的泄露。所以今天我們要講的就是如何來實現(xiàn)密碼的加密和解密來提高數(shù)據(jù)的安全性。
在這首先要引入springboot融合mybatis的知識,如果有這方面不懂得同學,就要首先看一看這方面的知識:
推薦大家一個比較好的博客: 程序猿DD-翟永超 http://blog.didispace.com/springbootmybatis/
為了方便大家的學習,我直接將源代碼上傳:
1.pom.xml
4.0.0 com.ninemax spring-Login-test 0.0.1-SNAPSHOT war org.springframework.boot spring-boot-starter-parent 1.3.2.RELEASE UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 org.springframework.boot spring-boot-starter-web commons-dbcp commons-dbcp com.oracle ojdbc14 10.2.0.3.0 org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-surefire-plugin true
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
7.還有一些靜態(tài)HTML
(1.)regist.html
注冊 注冊賬號
(2.)login.html
登錄 歡迎登錄
(3.)success.html
注冊成功 注冊成功
返回登錄
(4.)failure.html
登錄失敗 登錄失敗
(5.)successLogin.html
成功 success
代碼的格式如下:
完成了這一步的話首先運行一下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算法要求有一個可信任的隨機數(shù)源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(DES_KEY); // 創(chuàng)建一個密匙工廠,然后用它把DESKeySpec轉換成一個SecretKey對象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(deskey); // 加密對象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key, sr); // 加密,并把字節(jié)數(shù)組編碼成字符串 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算法要求有一個可信任的隨機數(shù)源 SecureRandom sr = new SecureRandom(); DESKeySpec deskey = new DESKeySpec(DES_KEY); // 創(chuàng)建一個密匙工廠,然后用它把DESKeySpec轉換成一個SecretKey對象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(deskey); // 解密對象 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key, sr); // 把字符串進行解碼,解碼為為字節(jié)數(shù)組,并解密 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),如圖
此時查看數(shù)據(jù)庫信息
你就會發(fā)現(xiàn)密碼實現(xiàn)了加密。
當然,下次登陸的時候直接輸入相應的賬號和密碼即可完成登錄,實現(xiàn)了解碼的過程。
springboot一種全新的編程規(guī)范,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務于框架的框架,服務范圍是簡化配置文件。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Springboot實現(xiàn)密碼加密解密的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!