maven 打包数据库加密_maven项目实现properties文件加密
发布日期:2021-06-24 11:37:47 浏览次数:3 分类:技术文章

本文共 2077 字,大约阅读时间需要 6 分钟。

最近项目里要求把配置文件的信息做一下打包加密,运行时解密,具体实现如下

由于之前项目配置用了autoconfig,我这边改不了插件的源码,于是自己新建了一个插件

名字就叫encrypt吧

pom信息:

pom

实现AbstractMojo:只做了一件事,就是把配置文件的value读出来做base64,具体的加密算法可以根据要求替换掉

packagecom.urfresh.maven.plugin;

importcom.urfresh.common.utils.encrypt.Base64Util;

importorg.apache.maven.plugin.AbstractMojo;

importorg.apache.maven.plugin.MojoExecutionException;

importjava.io.*;

importjava.util.Properties;

/**

* Goal which targetes a timestamp file.

*

* @goal encrypt

* @phase process-sources

*/

public classEncryptMojoextendsAbstractMojo {

/**

* @parameter

*/

privateFilesourceProperties;

/**

* @parameter

*/

privateStringtargetFileName;

private static finalStringLOG="log";

public voidexecute()throwsMojoExecutionException {

File source =sourceProperties;

if(!source.exists()) {

throw newIllegalArgumentException("no source found ! ");

}

File target =newFile(source.getParentFile(),targetFileName);

if(target.exists()) {

booleane = target.delete();

if(!e) {

throw newIllegalStateException("delete old fail ! ");

}

}

try{

booleans = target.createNewFile();

if(!s) {

throw newIllegalStateException("create target fail ! ");

}

}catch(IOException e) {

e.printStackTrace();

throw newMojoExecutionException("ioException",e);

}

FileWriter w =null;

Properties properties =null;

try{

w =newFileWriter(target);

properties =newProperties();

properties.load(newFileReader(source));

if(properties.isEmpty()){

return;

}

for(String name : properties.stringPropertyNames()){

String value = properties.getProperty(name);

if(!value.contains(LOG)){

value = doEncrypt(value);

}

StringBuilder sb =newStringBuilder(name);

sb.append("=").append(value);

w.write(sb.toString());

w.write('\n');

w.flush();

}

}catch(IOException e) {

throw newMojoExecutionException("Error creating file "+ target,e);

}finally{

if(w !=null) {

try{

w.close();

}catch(IOException e) {

// ignore

}

}

}

}

privateStringdoEncrypt(String s) {

returnBase64Util.encodeToString(s.getBytes());

}

}

将这个插件引入项目,并更改之前autoconfig的文件为加密后的文件

引入新的插件

更改目标文件与新插件的生成文件名一致

覆盖spring解析的默认实现并在xml中替换掉

继承原有的config

替换默认加载

到这里就可以打包发布啦

具体的加密与解密算法暂时还没有实现,后面再看吧!嘿嘿

转载地址:https://blog.csdn.net/weixin_32552195/article/details/112817153 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:hive窗口函数分组排序并取第一个值_Hive窗口函数row number的用法, 你肯定都会吧!...
下一篇:beetl 页面标签_后台如何传值到beetl的自定义标签?

发表评论

最新留言

很好
[***.229.124.182]2024年03月31日 03时02分04秒