Properties使用程序传递方式传递参数

Properties使用程序传递方式传递参数

使用场景:在真实的开发环境中,数据库的用户密码是对开发人员和其他人员保密的。运维人员为了保密,一般都需要把用户和密码经过加密成为密文后,配置到properties文件中。对于开发人员和其他人员而言,就不知道真是的用户密码了。

现:使用了Base64Util工具类进行解密,那么我们在创建 SqlSessionFactory前,就需要把用户名和密码解密,然后把解密后的字符串重置到properties属性中。如下:

package com.itlearn.test;

import com.itlearn.dao.IUserDao;
import com.itlearn.domain.User;
import com.itlearn.utils.Base64Util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;

public class MybatisTest {

    @Test
    public void mybatis_Properties() throws Exception{
        String resource = "applicationConfig.xml";
        InputStream inputStream = null;
        InputStream in = Resources.getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(in);
        //获得加密的数据
        String username = properties.getProperty("jdbc.username");
        String password = properties.getProperty("jdbc.password");
        String url = properties.getProperty("jdbc.url");
        String driver = properties.getProperty("jdbc.driver");
        //解密后重新设定会properties
        properties.put("jdbc.username", Base64Util.deCoded(username));
        properties.put("jdbc.password", Base64Util.deCoded(password));
        properties.put("jdbc.url", Base64Util.deCoded(url));
        properties.put("jdbc.driver", Base64Util.deCoded(driver));

        InputStream stream = Resources.getResourceAsStream(resource);
        //使用SqlSessionFactoryBuilder().build(stream,properties)创建sqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream,properties);
        SqlSession session = factory.openSession();
        IUserDao dao = session.getMapper(IUserDao.class);
        List<User> users = dao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
        session.close();
        stream.close();
        in.close();

    }

对应的properties文件配置如下:

jdbc.driver=Y29tLm15c3FsLmpkYmMuRHJpdmVy
jdbc.username=cm9vdA==
jdbc.password=cm9vdA==
jdbc.url=amRiYzpteXNxbDovL2xvY2FsaG9zdDozMzA2L215YmF0aXNkYg==

使用的Base64Util工具类如下:

package com.itlearn.utils;

import java.util.Base64;

public class Base64Util {

    /**
     * 该方法对字符串进行加密
     * @param str:要进行加密的字符串
     * @return :返回加密和的字符串
     */
    public static String enCode(String str){
        byte[] bytes = str.getBytes();
        return Base64.getEncoder().encodeToString(bytes);
    }

    /**
     * 该方法对字符串进行解密
     * @param encode:经过加密后的字符串,即要解密的字符串
     * @return :返回解密后的字符串
     */
    public static String deCoded(String encode){
        byte[] decode = Base64.getDecoder().decode(encode);
        String decodeStr = new String(decode);
        return decodeStr;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值