javascript
Spring Boot集成Jasypt安全框架
Jasypt安全框架提供了Spring的集成,主要是實(shí)現(xiàn)
PlaceholderConfigurerSupport類或者其子類。
在Sring 3.1之后,則推薦使用PropertySourcesPlaceholderConfigurer類作為屬性替換配置類,這里Spring集成Jasypt則使用Jasypt對(duì)屬性替換配置類的實(shí)現(xiàn)。EncryptablePropertySourcesPlaceholderConfigurer。
在Spring中集成比較容易,而且Jasypt官方也給出了配置Bean的方式和使用Jasypt標(biāo)簽的XML方式,而Spring boot集成就稍微有點(diǎn)不一樣,需要?jiǎng)?chuàng)建一個(gè)自動(dòng)配置類,并且創(chuàng)建一個(gè)注入PlaceholderConfigurerSupport的jasypt實(shí)現(xiàn)了的Bean .
下面是一個(gè)使用示例:
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相當(dāng)于applicationContext.xml,security.properties就是要進(jìn)行屬性替換的配置文件。
application.yml:
spring:datasource:url: jdbc:mysql://localhost:3306/abc?useSSL=false username: root password: ${jdbc.password}security.properties:
jdbc.password=ENC(jWgGELCkuxRuCI2Aqa6cF9VCxYpuKEZr)創(chuàng)建數(shù)據(jù)源的時(shí)候在使用屬性參數(shù)時(shí),會(huì)對(duì)ENC()中的內(nèi)容進(jìn)行解密,達(dá)到認(rèn)證成功,創(chuàng)建數(shù)據(jù)源完成。
http://www.tuicool.com/articles/YZJNzy3
?
總結(jié)
以上是生活随笔為你收集整理的Spring Boot集成Jasypt安全框架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML 颜色表示
- 下一篇: Spring4.3+Webscket 实