jasypt 与spring结合的两种配置方式

我们在开发一套系统的时候需要连接数据库,而在连接数据库的后配置文件通常是要加密的,假如使用spring +jasypt来实现数据库连接的加密,那么有两种方式可以配置

 

 

第一种,加密时的KEY可以采用动态的,而非写死的,这样会更安全一些,但是同时也会产生一些麻烦。

 

class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">




class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">

 

这种方式需要在系统级别设置环境变量APP_ENCRYPTION_PASSWORD

 

可以根据需求设定不同的值。

 

class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">




class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">

这种配置直接可以把值写死在系统配置上面。两种配置方式各有需求,根据实际情况来选择。

package com.hzmc.capaa.util.config;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

public class ConfigEncryptor {
	public static String readValue(String filePath, String key) {
		Properties props = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(
					filePath));
			props.load(new FileInputStream(filePath));
			String value = props.getProperty(key);
			in.close();
			return value;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	// 读取properties的全部信息
	@SuppressWarnings("unchecked")
	public static void readProperties(String filePath) {
		Properties props = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(
					filePath));
			props.load(in);
			Enumeration en = props.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String property = props.getProperty(key);
				System.out.println(key + " : " + property);
			}
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 写入properties信息
	public static void writeProperties(String filePath, String parameterName,
			String parameterValue) {
		Properties props = new Properties();
		try {
			File file = new File(filePath);
			if (file.exists()) {
				InputStream fis = new FileInputStream(file);
				props.load(fis);
				fis.close();
			}
			OutputStream fos = new FileOutputStream(file);
			props.setProperty(parameterName, "ENC(" + parameterValue + ")");

			props.store(fos, null);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 写入properties信息
	public static void writeProperty(String filePath, String parameterName,
			String parameterValue) {
		Properties props = new Properties();
		try {
			File file = new File(filePath);
			if (file.exists()) {
				InputStream fis = new FileInputStream(file);
				props.load(fis);
				fis.close();
			}
			OutputStream fos = new FileOutputStream(file);
			props.setProperty(parameterName, parameterValue);

			props.store(fos, null);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws IOException {

		final String outputPropertiesFileName = "system.properties";
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
		/* 从输入流中读取数据,并把它存在缓存中 */
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));
		String key = getParameter(args, "-key");
		String jdbcUrl = getParameter(args, "-jdbcurl");
		String username = getParameter(args, "-username");
		String password = getParameter(args, "-password");
		String driver = getParameter(args, "-driver");

		// 如果参数没有在命令行中输入,则交互式的要求用户输入
		if (key == null) {
			System.out.println("Please Enter CAPAA Startup Key");
			key = reader.readLine();
			encryptor.setPassword(key);
		} else {
			encryptor.setPassword(key);
		}

		if (jdbcUrl == null) {
			System.out.println("Please Enter JDBC URL:");
			jdbcUrl = reader.readLine();
		}

		if (username == null) {
			System.out.println("Please Enter JDBC Username:");
			username = reader.readLine();
		}

		if (password == null) {
			System.out.println("Please Enter JDBC Password:");
			password = reader.readLine();
		}

		if (driver == null) {
			System.out.println("Please Enter JDBC Driver:");
			driver = reader.readLine();
		}

		// System.out.println("key=======" + key);
		// System.out.println("url=======" + jdbcUrl);
		// System.out.println("driver=========" + driver);
		// System.out.println("user========" + username);
		// System.out.println("password=======" + password);

		String drivers = encryptor.encrypt(driver);
		String url = encryptor.encrypt(jdbcUrl);
		String user = encryptor.encrypt(username);
		String passwords = encryptor.encrypt(password);

		ConfigEncryptor.writeProperties(outputPropertiesFileName,
				"jdbc.driverClass", drivers);
		ConfigEncryptor.writeProperties(outputPropertiesFileName,
				"jdbc.jdbcUrl", url);
		ConfigEncryptor.writeProperties(outputPropertiesFileName, "jdbc.user",
				user);
		ConfigEncryptor.writeProperties(outputPropertiesFileName,
				"jdbc.password", passwords);

		System.out.println("url=======" + url);
		System.out.println("driver=========" + drivers);
		System.out.println("user========" + user);
		System.out.println("password=======" + passwords);

	}

	private static String getParameter(String[] args, String string) {
		for (int i = 0; i < args.length; i++) {
			String arg = args[i];
			if (arg.equals(string) && (i < args.length - 1)
					&& (!args[i + 1].startsWith("-"))) {
				System.out.println(args[i + 1]);
				return args[i + 1];
			}
		}
		return null;
	}
}

 



	
	
	
	

	
		
	    
	    
	    
	    
		

	    
	

	
		
		
			
		
	
	
	
	
	
	
	
	
	
	
		
		
	
	
	
		
		
		
		
			
				/login.jspx?error=2
				/login.jspx?error=3
				/login.jspx?error=4
				/login.jspx?error=5
			
		
	
	
	
		
	
	
	
	
	
	
		   
		   
	
		  
    
	  	 
	
	

 

你可能感兴趣的:(jsp,java,struts,jsf,spring,jstl,c:tag,标签库)