最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot實現(xiàn)密碼的加密解密

 更新時間:2017年11月15日 10:10:26   作者:卡索的意志  
這篇文章主要為大家詳細介紹了Springboot實現(xiàn)密碼的加密解密,具有一定的參考價值,感興趣的小伙伴們可以參考一下

現(xiàn)今對于大多數(shù)公司來說,信息安全工作尤為重要,就像京東,阿里巴巴這樣的大公司來說,信息安全是最為重要的一個話題,舉個簡單的例子:

就像這樣的密碼公開化,很容易造成一定的信息的泄露。所以今天我們要講的就是如何來實現(xiàn)密碼的加密和解密來提高數(shù)據(jù)的安全性。

在這首先要引入springboot融合mybatis的知識,如果有這方面不懂得同學,就要首先看一看這方面的知識:

推薦大家一個比較好的博客: 程序猿DD-翟永超 http://blog.didispace.com/springbootmybatis/

為了方便大家的學習,我直接將源代碼上傳:

1.pom.xml

<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">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.ninemax</groupId>
 <artifactId>spring-Login-test</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 
   <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

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

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

    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>

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

    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
    </dependency>

    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc14</artifactId>
      <version>10.2.0.3.0</version>
    </dependency>
    
    
     <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
    
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
 
</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

7.還有一些靜態(tài)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="提交"/> &nbsp;
      <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="提交"/> &nbsp;
      <input type="reset"/>   <br/>
      <a href="/regist" rel="external nofollow" >注冊賬號</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" rel="external nofollow" >返回登錄</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算法要求有一個可信任的隨機數(shù)源
        SecureRandom sr = new SecureRandom();
        DESKeySpec deskey = new DESKeySpec(DES_KEY);
        // 創(chuàng)建一個密匙工廠,然后用它把DESKeySpec轉(zhuǎn)換成一個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轉(zhuǎn)換成一個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)了加密。

當然,下次登陸的時候直接輸入相應(yīng)的賬號和密碼即可完成登錄,實現(xiàn)了解碼的過程。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 為什么說HashMap線程不安全

    為什么說HashMap線程不安全

    本文主要介紹了為什么說HashMap線程不安全,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • java文件上傳下載功能實現(xiàn)代碼

    java文件上傳下載功能實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了java文件上傳下載功能實現(xiàn)代碼,具有一定的參考價值,感興趣的朋友可以參考一下
    2016-06-06
  • springboot-dubbo cannot be cast to問題及解決

    springboot-dubbo cannot be cast to問題及解決

    這篇文章主要介紹了springboot-dubbo cannot be cast to問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • JAVA如何使用Math類操作數(shù)據(jù)

    JAVA如何使用Math類操作數(shù)據(jù)

    這篇文章主要介紹了JAVA如何使用Math類操作數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • 使用Spring MVC實現(xiàn)雙向數(shù)據(jù)綁定

    使用Spring MVC實現(xiàn)雙向數(shù)據(jù)綁定

    Spring MVC是一個廣泛用于構(gòu)建Java Web應(yīng)用程序的框架,它提供了眾多功能,包括雙向數(shù)據(jù)綁定,在這篇文章中,我們將向Java新手介紹如何使用Spring MVC實現(xiàn)雙向數(shù)據(jù)綁定,以及為什么這個特性如此重要,需要的朋友可以參考下
    2024-01-01
  • java實現(xiàn)統(tǒng)一異常處理的示例

    java實現(xiàn)統(tǒng)一異常處理的示例

    一個全局異常處理類需要處理三類異常1.業(yè)務(wù)類異常,2.運行時異常 ,3.Error,本文給大家介紹java實現(xiàn)統(tǒng)一異常處理的示例,感興趣的朋友一起看看吧
    2021-06-06
  • Java Spring Bean的生命周期管理詳解

    Java Spring Bean的生命周期管理詳解

    這篇文章主要為大家介紹了Java Spring Bean的生命周期管理,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • SpringBoot項目集成日志的實現(xiàn)方法

    SpringBoot項目集成日志的實現(xiàn)方法

    這篇文章主要介紹了SpringBoot項目集成日志的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • SpringBoot整合Mongodb實現(xiàn)增刪查改的方法

    SpringBoot整合Mongodb實現(xiàn)增刪查改的方法

    這篇文章主要介紹了SpringBoot整合Mongodb實現(xiàn)簡單的增刪查改,MongoDB是一個以分布式數(shù)據(jù)庫為核心的數(shù)據(jù)庫,因此高可用性、橫向擴展和地理分布是內(nèi)置的,并且易于使用。況且,MongoDB是免費的,開源的,感興趣的朋友跟隨小編一起看看吧
    2022-05-05
  • 淺談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時、持久、游離)

    淺談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時、持久、游離)

    下面小編就為大家?guī)硪黄獪\談Hibernate中的三種數(shù)據(jù)狀態(tài)(臨時、持久、游離)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論

梓潼县| 景谷| 遂平县| 河间市| 福建省| 双鸭山市| 田东县| 甘德县| 阿尔山市| 保定市| 陇川县| 永兴县| 南木林县| 霞浦县| 视频| 祁门县| 扶风县| 象山县| 济源市| 孟津县| 三都| 蒲江县| 莱西市| 原平市| 常州市| 台中市| 富川| 浦东新区| 金川县| 金沙县| 宁强县| 中西区| 惠东县| 西青区| 瑞安市| 龙海市| 肥西县| 唐海县| 旬邑县| 江永县| 乌拉特前旗|