博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot集成Jasypt安全框架
阅读量:6987 次
发布时间:2019-06-27

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

Jasypt安全框架提供了Spring的集成,主要是实现

PlaceholderConfigurerSupport类或者其子类。

在Sring 3.1之后,则推荐使用PropertySourcesPlaceholderConfigurer类作为属性替换配置类,这里Spring集成Jasypt则使用Jasypt对属性替换配置类的实现。EncryptablePropertySourcesPlaceholderConfigurer。

在Spring中集成比较容易,而且Jasypt官方也给出了配置Bean的方式和使用Jasypt标签的XML方式,而Spring boot集成就稍微有点不一样,需要创建一个自动配置类,并且创建一个注入PlaceholderConfigurerSupport的jasypt实现了的Bean .

下面是一个使用示例:

import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;import org.jasypt.spring31.properties.EncryptablePropertySourcesPlaceholderConfigurer;import org.springframework.boot.autoconfigure.AutoConfigureOrder;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.Ordered; import org.springframework.core.io.ClassPathResource; /** * Author : secondriver * Date : 2016/5/26 */ @Configuration @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) public class EncryptPropertyPlaceholderAutoConfiguration { private static final String SECURITY_PROPERTIES_FILE = "security.properties"; @Bean @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM); encryptor.setPassword("security"); EncryptablePropertySourcesPlaceholderConfigurer configurer = new EncryptablePropertySourcesPlaceholderConfigurer(encryptor); configurer.setLocation(new ClassPathResource(SECURITY_PROPERTIES_FILE)); return configurer; } }

配置文件的写入和Spring XML的基本类似。application.yml相当于applicationContext.xml,security.properties就是要进行属性替换的配置文件。

application.yml:

spring:  datasource:    url: jdbc:mysql://localhost:3306/abc?useSSL=false username: root password: ${jdbc.password}

security.properties:

jdbc.password=ENC(jWgGELCkuxRuCI2Aqa6cF9VCxYpuKEZr)

创建数据源的时候在使用属性参数时,会对ENC()中的内容进行解密,达到认证成功,创建数据源完成。

http://www.tuicool.com/articles/YZJNzy3

 

转载地址:http://pkhpl.baihongyu.com/

你可能感兴趣的文章
http 长连接和短连接介绍
查看>>
E-STP
查看>>
(JMX读书笔记)-JMX基本概念
查看>>
grep的语法和用法
查看>>
【hadoop】25.MapReduce-shuffle之分组
查看>>
Apache CarbonData:大数据生态一种新的高性能数据格式
查看>>
用Docker构建⼀个区块链工作和开发环境(上)
查看>>
Macbook Pro 关闭SIP 方法
查看>>
centos下统计目录下所有文件的的个数
查看>>
(26)改变自动扫描的包【从零开始学Spring Boot】
查看>>
论Linux系统学习的奇淫异巧
查看>>
从零开始开发微信小程序(二):开发一个简单的小程序
查看>>
如何在国内愉快的安装 Kubernetes v1.6.2
查看>>
Mysql GTID 模式详解
查看>>
es6函数总结
查看>>
Nodejs--readline(逐行读取)
查看>>
QT创建与QT无关的纯C++程序和动态/静态库
查看>>
为网建公司注入专业前端力量
查看>>
Vbox下虚拟机linux系统安装tomcat
查看>>
Mysql 多表合并统计
查看>>