java—从属性文件保护数据库凭据的正确方法

ux6nzvsh  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(317)
I have a Database Connection class which accesses the database credentials from a property file located at src/main/resources.

**DBconfig.properties**(property file)

DB.url=jdbc:mysql://localhost:3306/studentdb
DB.username=root
DB.password=root

我的连接类是这样的(位于src/main/java),
公共类dbconnect{

private static String resources="DBconfig.properties";
private static Connection con = null;
private static String url = null;
private static String username = null;
private static String password = null;

public static void main(String[] args) {

    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Properties prop = new Properties();

    try {

        InputStream rs = loader.getResourceAsStream(resources);
        prop.load(rs);

        url = prop.getProperty("DB.url");
        username = prop.getProperty("DB.username");
        password = prop.getProperty("DB.password");

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url,username,password);
        System.out.println("Database is Connected");

    } catch (Exception e) {

        System.out.println("Database is Not Connected");
        e.printStackTrace();
    }

}

}
我的问题是,我的凭据以明文形式存在于属性文件中,我是否正确保护了凭据?它是否像我已经实现的那样安全?这是一个使用weblogic应用服务器的springmvc应用程序。谢谢

r1zk6ea1

r1zk6ea11#

您可以使用jasypt,这将允许您以加密的形式将它们存储在属性文件中。
尽管它们是加密存储的,但如果您弄乱了ram,您可能会获得对它们的访问权限(因为连接字符串将在某个时候存储在ram中)。更安全的保护自己的方法是使用数据库中的角色、过程和视图。
例如:不允许该用户创建新表,选择他想要的,允许他检索一些视图,如果你想检查登录凭据,请使用过程。。。
最后,最安全的选择是使用服务器并通过服务器处理所有事情。

相关问题