SpringBoot之加密


最近利用閑暇時間寫了一個博客系統,主要參考wordpress,主要目的是為了提高自己的技術能力。
寫代碼寫了兩年多,聯系到之前在學校的時候寫過的一個博客系統,發現工作中開發的系統,技術上基本一致,業務邏輯方面存在差異。
比如博客系統可能面對高並發的場景,比如某個時間段訪問量,再比如博客系統為了最大程度吸引用戶(換句話說,提高用戶粘性),在界面上美觀,使用上更加方便。通常界面美觀伴隨着前端js庫的增多,特別是一些非常好看的畫面或者圖像是極其消耗帶寬的,帶寬如果不給力的話,頁面半天打不開,同樣也對於用戶而言體驗不好。

只想表達一個意思,軟件開發萬變不離其宗,本質上基本都是一個CMS系統(也許這句話武斷了)。

話不多說,今天我要講的關於SpringBoot加密。

SSM框架和SpringBoot中加密是不一樣的,比如在SSM框架中可以使用Druid進行加密(主要對數據庫密碼或者是其它重要配置信息加密),但是在SpringBoot就不一定會適用。

關於Druid加密,可以參考我的博客園這篇文章:Druid加密

SpringBoot使用jasypt-spring-boot-starter加密,具體步驟如下:

一、導入Maven依賴(注意,我的springboot版本為1.5.9,建議最好版本別相差太多,否則會出現依賴沖突等問題)

dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>1.16</version>
</dependency>

 

   

二、編寫測試類

package cn.test;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import com.blog.springboot.Application;
import com.blog.springboot.service.UsersService;

import cn.hutool.core.util.RandomUtil;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JunitTest {


    @Test
    public void testEncrypt() throws Exception {
            StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
            EnvironmentPBEConfig config = new EnvironmentPBEConfig();
            config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法,這個算法是默認的
            config.setPassword("lyh");                        // 加密的密鑰
            standardPBEStringEncryptor.setConfig(config);
            //加密用戶信息
            String plainText = "youcong";
            String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
            //加密密碼信息
            String Enpassword = "youcong";
            String EnpasswordText = standardPBEStringEncryptor.encrypt(Enpassword);
            String db="wordpress";
            String dbEnc = standardPBEStringEncryptor.encrypt(db);
            //加密地址信息
            String DBAUrl = "jdbc:mysql://localhost:3306/blog?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";
            String DBAUrlText = standardPBEStringEncryptor.encrypt(DBAUrl);
            System.out.println("用戶:"+encryptedText);
            System.out.println("密碼:"+EnpasswordText);
            System.out.println("地址:"+DBAUrlText);
            System.out.println("db:"+dbEnc);
        }

}

 

三、在springboot的配置文件添加如下配置(這里我以application.yml配置為例)

jasypt:
  encryptor:
    password: lyh

 

 

問:為什么要加這段?
答:這里的password對應的值lyh相當於密鑰,主要用於解密。
你在單元測試中以什么作為加密,那么在yml中就以什么作為解密。

四、配置application.yml中的數據源

datasource:
  url: ENC(cY3NmQF349TpBB0z0KavaiEPNDux/mKEss0UFeA11VTFC545rHh6t1rLC46GlX1b2rm8s5lzX49JmzFE4odcSiPafGZfQvnsHl2yVlLWM3kJg5DvVI4D0l5na3RUPTio4uz1gG9nML1u9ceHuj/yPb1097ZZfbCUsLSyRoeWvhhKuPxAM5mvGLZh641ArtVfRchNcdVZ1W4=)
  username: ENC(BcbIdbvEq4yN8kezH5mDjg==)
  password: ENC(Isk3pYM71258wxWTQOt3Dg==)
  db-name: ENC(CZcfw3ZJN6TVCVxkCW9Ey6z6iAuszHO8)
  filters: log4j,wall,mergeStat1

 

問:為什么要在數據源中添加ENC?
答:這個ENC相當於解密的標識符。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM