日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java中如何加密配置文件中的數(shù)據(jù)庫(kù)賬號(hào)和密碼?

相信很多人的項(xiàng)目里面配置文件都是類似這樣寫的

成都創(chuàng)新互聯(lián)主營(yíng)株洲網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App定制開發(fā),株洲h5重慶小程序開發(fā)搭建,株洲網(wǎng)站營(yíng)銷推廣歡迎株洲等地區(qū)企業(yè)咨詢

############### Mysql配置 #########################
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/abc?useUnicode=true&characterEncoding=utf-8&useTimezone=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

spring.datasource.url 中配置了數(shù)據(jù)庫(kù)的鏈接地址和端口,spring.datasource.username 配置了賬號(hào)以及spring.datasource.password 中配置了密碼。

如果是這樣的寫法,無(wú)非就是在裸奔,任何一個(gè)能接觸到這個(gè)文件的人,都能夠用 MySQL 的客戶端工具進(jìn)行數(shù)據(jù)庫(kù)的鏈接,里面的數(shù)據(jù)毫無(wú)安全可言。當(dāng)然如果是本地或者測(cè)試環(huán)境都還可以,但是對(duì)于生產(chǎn)環(huán)境那是不能接受的,畢竟這樣的安全性太差了,一不小心被刪庫(kù)跑路還是有可能的。

相對(duì)而言,有一些經(jīng)驗(yàn)的數(shù)據(jù)庫(kù)運(yùn)維人員是不會(huì)直接提供數(shù)據(jù)庫(kù)服務(wù)的 IP 地址和端口的,而是提供域名,通過(guò)在 url 地址上面配置相應(yīng)的域名,然后通過(guò)解析域名讓其訪問數(shù)據(jù)庫(kù)服務(wù),域名地址是不對(duì)外解析的,所以生產(chǎn)環(huán)境的主機(jī)以及開發(fā)人員的本機(jī),需要進(jìn)行 hosts 的配置,將域名對(duì)應(yīng)IP 地址配置起來(lái)。

這種方式會(huì)比上面直接裸奔的形式好一點(diǎn),外人拿到代碼,沒有 hosts 配置也是不能訪問數(shù)據(jù)庫(kù)的,難度相對(duì)來(lái)說(shuō)高了一點(diǎn)。但是這樣也有個(gè)問題,那就是開發(fā)人員還是可以通過(guò) MySQL 的客戶端進(jìn)行數(shù)據(jù)的訪問,如果哪天心情不好,刪庫(kù)跑路也不是沒有可能,或者說(shuō)私自泄露數(shù)據(jù)也是有機(jī)會(huì)的。

那么很多小明就問了,有沒有一種方式,可以有效的控制這種情況呢?讓盡量少了人接觸到數(shù)據(jù)庫(kù)的數(shù)據(jù),但是同時(shí)也不能影響開發(fā)的進(jìn)度,對(duì)于開發(fā)要友好。

有問題,就會(huì)有答案,這個(gè)時(shí)候我們就需要介紹一款神器了,那就是 jasypt。jasypt 可以幫助我們?cè)谂渲梦募信渲眉用芎蟮馁~號(hào)和密碼,然后結(jié)合秘鑰,就可以完全控制數(shù)據(jù)庫(kù)的安全性。下面我們就來(lái)試一下吧。

首先有一個(gè)需要連接數(shù)據(jù)庫(kù)的 Spring Boot 服務(wù),我們先看一下,在沒有引入 jasypt 的時(shí)候,是如何使用的,代碼如下:

############### Mysql配置 #########################
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/abc?useUnicode=true&characterEncoding=utf-8&useTimezone=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

#客戶端等待連接池連接的最大毫秒數(shù)
spring.datasource.hikari.connection-timeout=20000
#最小空閑連接數(shù)
spring.datasource.hikari.minimum-idle=5
#最大連接池大小
spring.datasource.hikari.maximum-pool-size=20
#連接池中空閑的最長(zhǎng)時(shí)間毫秒
spring.datasource.hikari.idle-timeout=30000
#池中連接關(guān)閉后的最長(zhǎng)生命周期毫秒
spring.datasource.hikari.max-lifetime=1200000
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.connection-test-query=SELECT 1

UserController.java

package com.controller;

import com.mapper.UserEntity;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController {

@Autowired
private UserMapper userMapper;

@RequestMapping("add")
public int addUser(String name, String desc) throws Exception {
int id, num = 0;
UserEntity u = new UserEntity(0, name, desc, new Date());
num = userMapper.insert(u);
id = u.getId();
return id;
}

@RequestMapping("getlist")
public List listUser() throws Exception {
List UserEntitys = userMapper.getAll();
System.out.println(UserEntitys.toString());
return UserEntitys;
}
}

UserMapper.java

package com.mapper;

import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author ziyou
*/
public interface UserMapper {
/**
* 查詢操作示例
*
* @return
*/
@Select("SELECT * FROM t_user;")
@Results({
@Result(property = "id", column = "Id"),
@Result(property = "Name", column = "Name"),
@Result(property = "Desc", column = "Desc"),
@Result(property = "CreateTime", column = "CreateTime")
})
List getAll();

/**
* 插入操作示例
* 將自增ID綁定到實(shí)體,keyProperty是實(shí)體字段,keyColumn是數(shù)據(jù)庫(kù)字段
*
* @param user
* @return
*/
@Insert("INSERT INTO t_user(`Name`,`Desc`,CreateTime) VALUES(#{Name}, #{Desc}, #{CreateTime});")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "Id")
int insert(UserEntity user);

}

另外我們有一張表,表里面的數(shù)據(jù)是這樣的,接下來(lái)我們啟動(dòng)服務(wù),調(diào)用接口查詢表的數(shù)據(jù),如下所示

說(shuō)明在使用明文賬號(hào)密碼的時(shí)候,整個(gè)流程是沒有問題,接下來(lái),我們引入 jasypt ,主要分下面三個(gè)步驟

1.引入 pom 依賴



com.github.ulisesbocchio
jasypt-spring-boot-starter
1.18

2.編寫加密工具類

public class JasyptUtil {

public static void main(String[] args) {
String account = "root";
String password = "123456";
BasicTextEncryptor encryptor = new BasicTextEncryptor();
//秘鑰
//encryptor.setPassword(System.getProperty("jasypt.encryptor.password"));
encryptor.setPassword("eug83f3gG");
//密碼進(jìn)行加密
String newAccount = encryptor.encrypt(account);
String newPassword = encryptor.encrypt(password);
System.out.println("加密后賬號(hào):" + newAccount);
System.out.println("加密后密碼:" + newPassword);
}
}

因?yàn)槲覀円玫郊用芎蟮拿芪?,所以我們先需要根?jù)原始賬號(hào)密碼,以及我們指定的秘鑰來(lái)生成加密后的密文,這里我們假設(shè)本地和測(cè)試環(huán)境的秘鑰為eug83f3gG,通過(guò)上面的工具類,我們可以生成如下的密文

3.替換賬號(hào)密碼,我們將 application.properties 里面的賬號(hào)密碼用上面的密文替換,如下所示,使用 ENC()包住密文。

############### Mysql配置 #########################
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/abc?useUnicode=true&characterEncoding=utf-8&useTimezone=true&serverTimezone=GMT%2B8
spring.datasource.username=ENC(/YnTvPH7WnnqMHu+wKeccA==)
spring.datasource.password=ENC(Xv829RzVs7pd2sv72/wsbg==)

這里說(shuō)明三點(diǎn)

  1. 此時(shí)我們還不能正常啟動(dòng)服務(wù),因?yàn)榫瓦@樣的話是啟動(dòng)不成功了,賬號(hào)和密碼是錯(cuò)誤的,我們需要將秘鑰傳入,讓jasypt 給我們反向解析出正確的賬號(hào)和密碼才能進(jìn)行數(shù)據(jù)庫(kù)的鏈接;
  2. 工具類中的秘鑰保持跟生產(chǎn)環(huán)境不一樣!!!
  3. 使用 ENC() 包住密文;

接下來(lái)我們可以將秘鑰通過(guò)兩種形式傳進(jìn)程序中使用,一種是將秘鑰通過(guò)系統(tǒng)環(huán)境變量的形式進(jìn)行配置,不過(guò)不建議;第二種是通過(guò)啟動(dòng)參數(shù)將秘鑰進(jìn)行傳入;這里我們使用第二種,在 SpringBoot 項(xiàng)目的啟動(dòng)參數(shù)中,我們?cè)黾舆@樣的配置 -Djasypt.encryptor.password=eug83f3gG,然后我們?cè)僦貑?yīng)用,就可以啟動(dòng)成功了。

啟動(dòng)成功了再次訪問上面的接口,可以看到效果是一樣的。

后續(xù)在生產(chǎn)環(huán)境中,只需要在啟動(dòng)參數(shù)中傳入與本地和測(cè)試環(huán)境不一樣的秘鑰,就可以有效的防止數(shù)據(jù)庫(kù)的賬號(hào)密碼被泄露了,就連開發(fā)人員都不知道是什么,只要配置的運(yùn)維人員知道,這個(gè)安全性就高很多了,怎么樣小伙伴你學(xué)會(huì)了嗎?趕緊在自己的項(xiàng)目中用起來(lái)吧!


網(wǎng)頁(yè)題目:Java中如何加密配置文件中的數(shù)據(jù)庫(kù)賬號(hào)和密碼?
文章出自:http://www.dlmjj.cn/article/dpdidis.html