13、SpringBoot-配置文件里密码加密

系列导航

springBoot项目打jar包

1、springboot工程新建(单模块)

2、springboot创建多模块工程

3、springboot连接数据库

4、SpringBoot连接数据库引入druid

5、SpringBoot连接数据库引入mybatis

6、SpringBoot-mybatis分页实现pagehelper

7、SpringBoot-mybatis-plus引入

8、SpringBoot 事务

9、SpringBoot-mybatis-druid多源数据多源数据

10、SpringBoot-mybatis-plus-druid多源数据

11、SpringBoot-mybatis-plus-druid多源数据事务

12、SpringBoot-mybatis-plus-ehcache

13、SpringBoot-配置文件里密码加密

14、SpringBoot-easyexcel导出excle

完结

 

springboot连接数据库,数据库的用户名、密码默认多是明文放在配置文件里,如何提高安全性不要明文写在配置文件里,不问就解决这个问题。

1、数据库中创建表

CREATE TABLE TEST_BLOCK_T   
(
  BLOCK_ID         VARCHAR2(10 BYTE) PRIMARY   KEY,    --编码
  BLOCK_NAME       VARCHAR2(200 BYTE)                    --资源名称 
); 
Insert into TEST_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('1', '哈哈哈');
COMMIT;

2、pom.xml依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--密码加密-->
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
    </dependencies>

3、工程结构

 

4、源码

package com.example.demo.controller;


import org.jasypt.encryption.StringEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    StringEncryptor stringEncryptor;


    //访问数据库的数据
    @GetMapping("/list")
    @ResponseBody
    public String index() {

        String sql = "SELECT BLOCK_NAME FROM TEST_BLOCK_T WHERE BLOCK_ID = ?";

        // 通过jdbcTemplate查询数据库
        String mobile = (String) jdbcTemplate.queryForObject(sql, new Object[]{1}, String.class);

        return "Hello " + mobile;
    }

    //对配置文件中的用户名密码加密,加密盐也是用配置文件里的
    @GetMapping("/passwd")
    @ResponseBody
    public String passwd() {

        //加密密码
        String name = stringEncryptor.encrypt("zy");
        String pwd = stringEncryptor.encrypt("1");
        System.out.println("name:"+name);
        System.out.println("pwd:"+pwd);
        return "success!";

    }

    //解密配置文件中的用户名和密码,加密盐也是用配置文件里的
    @GetMapping("/unpasswd")
    @ResponseBody
    public String unpasswd() {
        //解密密码
        String name =  stringEncryptor.decrypt("YCfAQQPOSw5Jp/uzmA8LkQ==");
        String pwd = stringEncryptor.decrypt("hOwW0zxYpHaEH/lkpHyJaA==");
        System.out.println("name:"+name);
        System.out.println("pwd:"+pwd);
        return "success!";

    }

    //对配置文件中的用户名密码加密,加密盐使用代码里的
    @GetMapping("/passwd1")
    @ResponseBody
    public String passwd1() {

        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(盐),自定义
        textEncryptor.setPassword("hello");
        //要加密的数据(数据库的用户名或密码)
        String username = textEncryptor.encrypt("zy");
        String password = textEncryptor.encrypt("1");
        System.out.println("username:"+username);
        System.out.println("password:"+password);

        return "success!";


    }




}

 

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

 

5、配置文件

# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080

# 数据库设置
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.0.100:1521:orcl
#spring.datasource.username=zy
#spring.datasource.password=1
jasypt.encryptor.password=hello
spring.datasource.username=ENC(YCfAQQPOSw5Jp/uzmA8LkQ==)
spring.datasource.password=ENC(hOwW0zxYpHaEH/lkpHyJaA==)

 

6、测试

(1)用户名密码明文连接数据库

配置文件

 

 

 

 

访问查询数据库的接口 http://localhost:8080/hello/list

返回值:Hello 哈哈哈

(2)用户名密码明文连接数据库

<1>配置文件中打开jasypt.encryptor.password的参数,让系统知道加密盐的值。

完整配置如下:

 

 

 

 

<2>调用接口 http://localhost:8080/hello/passwd  获取加密后的用户名和密码

输出的结果是:

name:YCfAQQPOSw5Jp/uzmA8LkQ==

pwd:hOwW0zxYpHaEH/lkpHyJaA==

修改配置文件如下,其中EMC()就是告诉系统里面的内容是加密过的需要解密

 

 

 

 

<3>重启项目 调用接口 http://localhost:8080/hello/list

正常返回数据结果:Hello 哈哈哈

到此给配置文件中的敏感信息加密就完成了。这样做有个缺点就是加密盐是在配置文件里的,对方如果非要解密是可以做到的,例如:

<4>密文解密

将加密后的密文放到如下代码中

 

 

 

 

调用接口http://localhost:8080/hello/unpasswd

结果:

name:zy

pwd:1

到此成功解密出了之前加密的密文。所以之前的加密只能拦住不懂这个加密的人,真正懂得人是拦不住的依然可以解密出来。那还有没有更安全一些的做法?有

 

<5>启动时指定加密盐

先将程序打成jar包 如何打包参考https://www.cnblogs.com/yclh/p/15947054.html

打包后将配置文件中的如下这段去掉,配置文件中就没有加密盐的信息了
jasypt.encryptor.password=hello
启动的jar包的时候使用如下语句,其中“hello”就是加密盐,SpringBoot_PassWord.jar就是打好的jar包

java -jar -Djasypt.encryptor.password=hello SpringBoot_PassWord.jar

 

启动成功后访问接口:http://localhost:8080/hello/list

成功返回参数:Hello 哈哈哈

 

还有没有其他更安全的方式,网上看到也有单独搞个配置文件,设置环境变量等操作起来有点麻烦,个人感觉就没有绝对安全的,只要想破解的人懂你设置的那个套路就能破解,不管你把这个加密盐藏在哪里总归是有个地方存的。所以最好的加密就是你搞得东西别人不懂,懂得人欺负不懂得人。

 

资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!
posted @ 2022-05-24 19:50  万笑佛  阅读(2148)  评论(3编辑  收藏  举报