做裝修的業(yè)務(wù)網(wǎng)站網(wǎng)絡(luò)營(yíng)銷常用的工具有哪些
文章目錄
- 前言
- Spring Cloud 第一代
- 1、創(chuàng)建config server項(xiàng)目并加入加解密key
- 2、啟動(dòng)項(xiàng)目,進(jìn)行數(shù)據(jù)加密
- 3、實(shí)際項(xiàng)目中的測(cè)試server
- Spring Cloud Alibaba
- 低版本架構(gòu)不支持,取巧實(shí)現(xiàn)
- 無(wú)加密配置,聯(lián)調(diào)環(huán)境問(wèn)題
- 加密數(shù)據(jù)源配置
- 原理探究
- 自定義加密解密器實(shí)現(xiàn)數(shù)據(jù)源加密解密配置
前言
之前就想著做一個(gè)匯總的記錄,在實(shí)際項(xiàng)目開發(fā)中,公司、客戶等群體對(duì)數(shù)據(jù)安全性問(wèn)題,都是很看重的,結(jié)合實(shí)際的開發(fā),本次做一個(gè)各項(xiàng)分布式微服務(wù)架構(gòu)的加密配置說(shuō)明匯總。
Spring Cloud 第一代
在第一代 Spring Cloud
架構(gòu)體系中,一般是將公用的,或者配置隨環(huán)境需要變動(dòng)的,采用Config
配置中心進(jìn)行集中管理。Spring Cloud Config
配置中心本身就具備配置文件的加解密配置處理
。
注意標(biāo)識(shí)
{cipher}
具體實(shí)現(xiàn)方式如下所示:
config server的加解密功能依賴Java Cryptography Extension(JCE)
本次開發(fā)測(cè)試使用的是 jdk 1.8,所以jdk 1.8 的jce 下載地址為:
https://www.oracle.com/java/technologies/javase-jce8-downloads.html
下載并解壓,將其中的jar包覆蓋到JDK/jre/lib/security目錄中。
關(guān)于 jce_policy-8.zip 我這里已經(jīng)下載好了,具體需要用到的可以去我的github中查找!
《jdk1.8 jce依賴文件》
《jce_policy安裝【java密碼擴(kuò)展無(wú)限制權(quán)限策略文件安裝】》
[注意:]
安裝jdk,需要在jre/lib/security中粘貼上述中的兩個(gè)jar文件;
同時(shí),如果安裝了jre,也需要在jre中添加jce的jar文件。
1、創(chuàng)建config server項(xiàng)目并加入加解密key
由于此時(shí)的加解密key不能被修改和覆蓋,所以需要?jiǎng)?chuàng)建
bootstrap.yml
文件進(jìn)行配置。
bootstrap.yml
### 添加加解密key
## (不能被覆蓋和修改,所以必須配置在bootstrap.yml中)
encrypt:key: xiangjiaobunana
配置好信息后,其他配置沿用之前的demo配置。
application.yml:
###服務(wù)名稱(服務(wù)注冊(cè)到eureka名稱)
spring:application:name: springcloud-config-service## Config Server 配置中心Service配置信息
## 配置文件所在的 git 倉(cāng)庫(kù)地址
spring.cloud.config.server.git.uri: https://github.com/765199214/springcloud2.0-config-service.git
## 配置文件再哪個(gè)文件夾下
spring.cloud.config.server.git.search-paths: respo
## 配置 clone-on-start 啟動(dòng)時(shí)就clone倉(cāng)庫(kù)到本地,默認(rèn)是在配置被首次請(qǐng)求時(shí),config server 才會(huì) clone git 倉(cāng)庫(kù)
spring.cloud.config.server.git.clone-on-start: true
## 配置默認(rèn) git clone 至指定的磁盤或文件夾內(nèi)(linux 只有一個(gè) / 根;windows 會(huì)進(jìn)入項(xiàng)目所在的磁盤下)
spring.cloud.config.server.git.basedir: /data/config server/
2、啟動(dòng)項(xiàng)目,進(jìn)行數(shù)據(jù)加密
Config Server 本身就為此提供了加解密操作的接口,環(huán)境配置中只需要請(qǐng)求 /encrypt與/decrypt 即可實(shí)現(xiàn)。
- 加密:
成功啟動(dòng)項(xiàng)目后,請(qǐng)求下列接口實(shí)現(xiàn)加密的輸出:
curl http://localhost:3000/encrypt -d ‘要加密的信息’
post 請(qǐng)求
加密后:
- 解密:
初步的加密解密實(shí)現(xiàn)了,此時(shí)又該如何應(yīng)用到實(shí)際的項(xiàng)目中去呢?
3、實(shí)際項(xiàng)目中的測(cè)試server
修改 github 中的文件信息。
springboot:datasources:username: rootpassword: '{cipher}38785234edc0396a0cc887cb8c737546f1fe244c5baaeae253789744d9a8484b' ## {cipher}只是一個(gè)標(biāo)記,方便springcloud去識(shí)別判斷,如果沒(méi)加標(biāo)記,則不會(huì)進(jìn)行解密操作
修改config server 中的文件夾掃描路徑:
spring.cloud.config.server.git.search-paths: respo,en*
重啟項(xiàng)目,請(qǐng)求測(cè)試:
http://localhost:3000/config-encrypt-dev.yml
查看本地緩存的git 信息得知:
[總結(jié):]
本地的文件依舊是 密文!
他只會(huì)在內(nèi)存中進(jìn)行解密操作!
[問(wèn):]如果不想在 server 中就進(jìn)行解密,而是想在client中再解密(server拿到依舊是密文,client解密),我又該如何操作?
增加下列配置信息:
spring.cloud.config.server.encrypt.enabled: false
Spring Cloud Alibaba
Spring cloud Alibaba
在國(guó)內(nèi)用的較多,相比第一代的cloud而言,Spring cloud Alibaba
的 Nacos
本身就具備eureka/Zookeper 注冊(cè)中心
、Config 配置中心
、Bus 配置消息通知
等功能。
并且第一代cloud早就未出現(xiàn)后續(xù)的維護(hù)更新操作,論安全方面,cloud alibaba更加具備。
但
Cloud Alibaba
的Naocs
,在> 2.0.4
版本中,才支持配置文件的加密
。
關(guān)于配置和使用,參考官方文檔:
Nacos 配置文件加密操作
低版本架構(gòu)不支持,取巧實(shí)現(xiàn)
如果實(shí)際開發(fā)中,選擇的架構(gòu)版本比較低(相對(duì)支持版)
或者非分布式架構(gòu)
,然道就不能支持配置的加密解密了?
本次只對(duì)
數(shù)據(jù)源
的配置進(jìn)行配置加密解密
操作,其他配置項(xiàng),可以使用自定義加解密
方式實(shí)現(xiàn)。
無(wú)加密配置,聯(lián)調(diào)環(huán)境問(wèn)題
引入pom依賴,完整如下:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.1.4.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.1.4.RELEASE</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.20</version>
</dependency>
<!-- swagger -->
<!-- swagger配置等注解 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.4.0</version>
</dependency>
<!-- swagger 在線顯示web -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.4.0</version>
</dependency>
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version>
</dependency>
<!-- fastjson -->
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version>
</dependency>
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.26</version>
</dependency>
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.26</version>
</dependency><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.6.1</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.0</version>
</dependency>
<!-- Wrappers 鏈?zhǔn)秸Z(yǔ)法 -->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-core</artifactId><version>3.4.0</version><scope>compile</scope>
</dependency>
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><scope>test</scope>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId><scope>test</scope>
</dependency>
其中核心點(diǎn):
<dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.6.1</version>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.0</version>
</dependency>
<!-- Wrappers 鏈?zhǔn)秸Z(yǔ)法 -->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-core</artifactId><version>3.4.0</version><scope>compile</scope>
</dependency>
數(shù)據(jù)源配置
server:port: 80
spring:datasource:dynamic:datasource:xiangjiao:url: jdbc:mysql://xxx:3306/flyway?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0開始支持SPI可省略此配置primary: xiangjiao # 主連接strict: false #嚴(yán)格匹配數(shù)據(jù)源,默認(rèn)false. true未匹配到指定數(shù)據(jù)源時(shí)拋異常,false使用默認(rèn)數(shù)據(jù)源
編寫調(diào)試查詢接口
import cn.xj.model.CommonResult;
import cn.xj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/test1")
public class TestController {@AutowiredUserService userServiceImpl;@GetMapping("/test1")public CommonResult<String> test1(Integer id){return CommonResult.success(userServiceImpl.test1(id));}
}
服務(wù)層
import cn.xj.dao.UserMapper;
import cn.xj.model.Users;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;@Service
public class UserService {@AutowiredUserMapper userMapper;public String test1(Integer id){List<Users> users = userMapper.selectList(Wrappers.lambdaQuery(Users.class).eq(Users::getUserId,id));Users users1 = users.get(0);return users1.getUserName();}
}
dao層
import cn.xj.model.Users;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;@Mapper
@Repository
public interface UserMapper extends BaseMapper<Users> {
}
請(qǐng)求測(cè)試:
http://localhost/test1/test1?id=2
加密數(shù)據(jù)源配置
和SpringCloud
第一代中的{cipher}
標(biāo)識(shí)一樣,dynamic datasource
當(dāng)然也需要一個(gè)標(biāo)識(shí)用來(lái)區(qū)分加密和不加密的配置內(nèi)容,默認(rèn)使用ENC(xxxxx)
區(qū)別。
在
3.5.0
版本開始,可以自定義配置,這個(gè)等會(huì)再說(shuō)。
dynamic-datasource
支持對(duì)數(shù)據(jù)源配置的url
、username
、password
進(jìn)行加密配置。加密的方法可以參考下面的代碼。
import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;public class Demo {public static void main(String[] args) throws Exception {String password = "root";//使用默認(rèn)的publicKey ,建議還是使用下面的自定義String encodePassword = CryptoUtils.encrypt(password);System.out.println(encodePassword);}//自定義publicKeypublic static void main(String[] args) throws Exception {String[] arr = CryptoUtils.genKeyPair(512);System.out.println("privateKey: " + arr[0]);System.out.println("publicKey: " + arr[1]);System.out.println("url: " + CryptoUtils.encrypt(arr[0], "jdbc:mysql://127.0.0.1:3306/order"));System.out.println("username: " + CryptoUtils.encrypt(arr[0], "root"));System.out.println("password: " + CryptoUtils.encrypt(arr[0], "123456"));}
}
對(duì)上面無(wú)加密
的數(shù)據(jù)信息進(jìn)行加密,加密后的yml配置如下所示:
注意: 加密后的密文在配置前,需要增加標(biāo)識(shí)。
server:port: 80
spring:datasource:dynamic:datasource:xiangjiao:url: jdbc:mysql://xxxx:3306/flyway?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8username: ENC(VZamSTMi224AH6RUtJGXNldiDp/XEL2ozRhBUu/o9ChodT4JEb9kE/j0EFhXKbjsfvLVacUW0AUzetA6OrNJug==)password: ENC(VZamSTMi224AH6RUtJGXNldiDp/XEL2ozRhBUu/o9ChodT4JEb9kE/j0EFhXKbjsfvLVacUW0AUzetA6OrNJug==)driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0開始支持SPI可省略此配置primary: xiangjiao # 主連接strict: false #嚴(yán)格匹配數(shù)據(jù)源,默認(rèn)false. true未匹配到指定數(shù)據(jù)源時(shí)拋異常,false使用默認(rèn)數(shù)據(jù)源
再次重啟項(xiàng)目,測(cè)試
原理探究
在dynamic-datasource
中的com.baomidou.dynamic.datasource.event.EncDataSourceInitEvent
中,定義了正則表達(dá)式,當(dāng)識(shí)別到密文用ENC(xxx)
包裝時(shí),會(huì)觸發(fā)解析明文操作,進(jìn)行數(shù)據(jù)配置的還原與包裝。
當(dāng)然這里可以自定義其他加密
方式,只需要按照官方源碼中的例子,定義解析類,然后注入到spring容器中,如下:
這里的@ConditionalOnMissingBean
注解起了關(guān)鍵性的作用。
如果不存在
DataSourceInitEvent
對(duì)象的實(shí)例bean,才會(huì)注入官方默認(rèn)的EncDataSourceInitEvent
實(shí)例!
自定義加密解密器實(shí)現(xiàn)數(shù)據(jù)源加密解密配置
下面以CCM
加密解密作為一個(gè)自定義操作。
導(dǎo)入CCM所需要的jar包。
并加載至當(dāng)前項(xiàng)目中。
編寫CCM
加密解密的工具類。
import org.bouncycastle.jce.provider.BouncyCastleProvider;import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.Security;public class CcmUtils {static byte[] keyBytes = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16};static byte[] nonce = {'a','b','c','d','e','f','g','h','i','j','k','l'};public static void main(String[] args) throws Exception {byte[] bytes = "root".getBytes();System.out.println(new String(bytes,"utf-8")); // rootString s = DatatypeConverter.printHexBinary(bytes);System.out.println(s); // 726F6F74// 加密byte[] roots = encrypt("root");// 3914D177814A01A1String s1 = DatatypeConverter.printHexBinary(roots);// 解密String decrypt = decrypt(s1);System.out.println(decrypt);}// 加密public static byte[] encrypt(String str) throws Exception {Security.addProvider(new BouncyCastleProvider());//32 mac 4字節(jié) 32位GCMParameterSpec parameterSpec = new GCMParameterSpec(32, nonce);Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding");SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, parameterSpec);System.out.println(DatatypeConverter.printHexBinary(cipher.doFinal(str.getBytes())));return cipher.doFinal(str.getBytes());}// 解密public static String decrypt(String str) throws Exception {Security.addProvider(new BouncyCastleProvider());//32 mac 4字節(jié) 32位GCMParameterSpec parameterSpec = new GCMParameterSpec(32, nonce);Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding");SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, parameterSpec);// String 轉(zhuǎn) 十六進(jìn)制的 數(shù)組// 3914D177814A01A1 -> 0x39,0x14byte[] cipStr = HexString2Bytes(str);System.out.println(DatatypeConverter.printHexBinary(cipher.doFinal(cipStr)));return new String(cipher.doFinal(cipStr),"utf-8");}// 十六進(jìn)制字符串 轉(zhuǎn) 十六進(jìn)制 byte 數(shù)組public static byte[] HexString2Bytes(String src) {byte[] ret = new byte[src.length() / 2];byte[] tmp = src.getBytes();for (int i = 0; i < tmp.length / 2; i++) {ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);}return ret;}public static byte uniteBytes(byte src0, byte src1) {byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();_b0 = (byte) (_b0 << 4);byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();byte ret = (byte) (_b0 ^ _b1);return ret;}// 十六進(jìn)制的byte數(shù)組 轉(zhuǎn) 十六進(jìn)制字符串public static String bytesToHex(byte[] bytes) {StringBuilder sb = new StringBuilder();for (byte b : bytes) {sb.append(String.format("%02x", b));}return sb.toString();}
}
root 加密后的字符串如下:
3914D177814A01A1
編寫新的配置密文解析器,并定義加密頭標(biāo)識(shí)CCM(xxxx)
。
import cn.xj.util.CcmUtils;
import com.baomidou.dynamic.datasource.event.DataSourceInitEvent;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;import javax.sql.DataSource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;@Slf4j
public class CcmDataSourceInitEvent implements DataSourceInitEvent {/*** 自定義標(biāo)識(shí)*/private static final Pattern ENC_PATTERN = Pattern.compile("^CCM\\((.*)\\)$");@Overridepublic void beforeCreate(DataSourceProperty dataSourceProperty) {String publicKey = dataSourceProperty.getPublicKey();if (StringUtils.hasText(publicKey)) {dataSourceProperty.setUrl(decrypt(dataSourceProperty.getUrl()));dataSourceProperty.setUsername(decrypt( dataSourceProperty.getUsername()));dataSourceProperty.setPassword(decrypt(dataSourceProperty.getPassword()));}}@Overridepublic void afterCreate(DataSource dataSource) {}/*** 字符串解密*/private String decrypt(String cipherText) {if (StringUtils.hasText(cipherText)) {Matcher matcher = ENC_PATTERN.matcher(cipherText);if (matcher.find()) {try {return CcmUtils.decrypt(matcher.group(1));} catch (Exception e) {log.error("DynamicDataSourceProperties.decrypt error ", e);}}}return cipherText;}
}
修改配置文件,如下所示:
server:port: 80
spring:datasource:dynamic:datasource:xiangjiao:url: jdbc:mysql://xxxx:3306/flyway?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8username: CCM(3914D177814A01A1)password: CCM(3914D177814A01A1)driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0開始支持SPI可省略此配置primary: xiangjiao # 主連接strict: false #嚴(yán)格匹配數(shù)據(jù)源,默認(rèn)false. true未匹配到指定數(shù)據(jù)源時(shí)拋異常,false使用默認(rèn)數(shù)據(jù)源
重啟項(xiàng)目,訪問(wèn)請(qǐng)求測(cè)試能否查詢到對(duì)應(yīng)的數(shù)據(jù):