Skip to content

Commit

Permalink
spring 2.2.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulises Bocchio authored and Ulises Bocchio committed Nov 24, 2019
1 parent 9029e55 commit 4303c3c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,28 @@ There are 3 ways to integrate `jasypt-spring-boot` in your project:
- Adding `jasypt-spring-boot` to your classpath and declaring individual encryptable property sources with `@EncrytablePropertySource`

## What's new?
Update 9/8/2019: Version 2.1.2 Release Includes: jasypt 1.9.3 rollback with IV Generators (thanks [tkalmar](https://github.com/tkalmar)), interpolation inside `ENC()` and `${}` blocks (thanks [ttulka](https://github.com/ttulka)), and fixes for relaxed bindings, fail on custom bean not found, filters, and double app listener

Update 1/8/2019: Version 2.1.1 Release Including [Asymmetric Encryption](#asymmetric-encryption)<br/> and support for JSB96 with IV Generators (Thanks [@melloware](https://github.com/melloware)!!)

Update 7/17/2018: Version 2.1.0 Release Including [Filters](#using-filters)

Update 3/17/2018: Version 2.0.0 has been released supporting Spring Boot 2.0.X.RELEASE. [SemVer](https://semver.org/) adopted.

Update 7/18/2015: `jasypt-spring-boot` is now in Maven Central!<br/>
### Update 11/24/2019: Version 3.0.0 Release Includes
* Spring Boot 1.5.X No longer supported
* Changed default encryption to PBEWITHHMACSHA512ANDAES_256 (Thanks [@rupert-madden-abbott](https://github.com/rupert-madden-abbott))
* Switched properties cache to HashMap to avoid concurrency issues (Thanks [@krm1312](https://github.com/krm1312))
* Higher priority for Properties post processor (Thanks [@ttulka](https://github.com/ttulka))
* Jasypt Spring Boot [Maven Plugin](#maven-plugin) (Thanks [@rupert-madden-abbott](https://github.com/rupert-madden-abbott))
### Update 9/8/2019: Version 2.1.2 Release Includes
* jasypt 1.9.3 rollback with IV Generators (thanks [@tkalmar](https://github.com/tkalmar))
* interpolation inside `ENC()` and `${}` blocks (thanks [@ttulka](https://github.com/ttulka))
* fixes for relaxed bindings, fail on custom bean not found, filters, and double app listener

### Update 1/8/2019: Version 2.1.1 Release Including
* [Asymmetric Encryption](#asymmetric-encryption)
* and support for JSB96 with IV Generators (Thanks [@melloware](https://github.com/melloware)!!)

### Update 7/17/2018: Version 2.1.0 Release Including
* [Filters](#using-filters)

### Update 3/17/2018: Version 2.0.0 has been released supporting
* Spring Boot 2.0.X.RELEASE. [SemVer](https://semver.org/) adopted.

### Update 7/18/2015: `jasypt-spring-boot` is now in Maven Central!

## What to do First?
Use one of the following 3 methods (briefly explained above):
Expand Down
2 changes: 1 addition & 1 deletion jasypt-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>jasypt-spring-boot-parent</artifactId>
<groupId>com.github.ulisesbocchio</groupId>
<version>2.1.3-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</parent>

<artifactId>jasypt-maven-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion jasypt-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-parent</artifactId>
<version>2.1.3-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</parent>

<artifactId>jasypt-spring-boot-starter</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion jasypt-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-parent</artifactId>
<version>2.1.3-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</parent>

<artifactId>jasypt-spring-boot</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.core.Conventions;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.CompositePropertySource;
Expand All @@ -29,6 +32,7 @@
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -41,6 +45,8 @@
@Slf4j
public class EncryptablePropertySourceBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {

private static final String CONFIGURATION_CLASS_ATTRIBUTE =
Conventions.getQualifiedAttributeName(ConfigurationClassPostProcessor.class, "configurationClass");
private ConfigurableEnvironment env;

public EncryptablePropertySourceBeanFactoryPostProcessor(ConfigurableEnvironment env) {
Expand All @@ -64,6 +70,7 @@ private List<PropertySourceLoader> initPropertyLoaders() {

private void loadEncryptablePropertySource(AnnotationAttributes encryptablePropertySource, ConfigurableEnvironment env, ResourceLoader resourceLoader, EncryptablePropertyResolver resolver, EncryptablePropertyFilter propertyFilter, MutablePropertySources propertySources, List<PropertySourceLoader> loaders) throws BeansException {
try {
log.info("Loading Encryptable Property Source '{}'", encryptablePropertySource.getString("name"));
PropertySource ps = createPropertySource(encryptablePropertySource, env, resourceLoader, resolver, propertyFilter, loaders);
propertySources.addLast(ps);
log.info("Created Encryptable Property Source '{}' from locations: {}", ps.getName(), Arrays.asList(encryptablePropertySource.getStringArray("value")));
Expand Down Expand Up @@ -101,30 +108,34 @@ private String generateName(String name) {
}

private Stream<AnnotationAttributes> getEncryptablePropertySourcesMetadata(ConfigurableListableBeanFactory beanFactory) {
Stream<AnnotationAttributes> source = getBeanDefinitionsForAnnotation(beanFactory, com.ulisesbocchio.jasyptspringboot.annotation.EncryptablePropertySource.class);
Stream<AnnotationAttributes> sources = getBeanDefinitionsForAnnotation(beanFactory, EncryptablePropertySources.class)
.flatMap(map -> Arrays.stream((AnnotationAttributes[]) map.get("value")));
return Stream.concat(source, sources);
return getBeanDefinitionsForAnnotation(beanFactory, com.ulisesbocchio.jasyptspringboot.annotation.EncryptablePropertySource.class, EncryptablePropertySources.class);
}

private Stream<AnnotationAttributes> getBeanDefinitionsForAnnotation(ConfigurableListableBeanFactory bf, Class<? extends Annotation> annotation) {
return Arrays.stream(bf.getBeanNamesForAnnotation(annotation))
private Stream<AnnotationAttributes> getBeanDefinitionsForAnnotation(ConfigurableListableBeanFactory bf, Class<? extends Annotation> annotation, Class<? extends Annotation> repeatable) {
return Stream.concat(Arrays.stream(bf.getBeanNamesForAnnotation(annotation)), Arrays.stream(bf.getBeanNamesForAnnotation(repeatable)))
.distinct()
.map(bf::getBeanDefinition)
.filter(bd -> bd instanceof AnnotatedBeanDefinition)
.map(bd -> (AnnotatedBeanDefinition) bd)
.filter(bd -> bd.getAttribute(CONFIGURATION_CLASS_ATTRIBUTE) != null && bd instanceof AbstractBeanDefinition)
.map(AnnotatedBeanDefinition::getMetadata)
.filter(md -> md.hasAnnotation(annotation.getName()))
.map(md -> (AnnotationAttributes) md.getAnnotationAttributes(annotation.getName()));
.filter(am -> am.hasAnnotation(annotation.getName()) || am.hasAnnotation(repeatable.getName()))
.flatMap(am -> Optional
.ofNullable((AnnotationAttributes) am.getAnnotationAttributes(annotation.getName()))
.map(Stream::of)
.orElseGet(() -> Optional
.ofNullable((AnnotationAttributes) am.getAnnotationAttributes(repeatable.getName()))
.map(ram -> Arrays.stream(ram.getAnnotationArray("value")))
.orElseGet(Stream::empty)));
}

private Optional<List<PropertySource<?>>> loadPropertySource(List<PropertySourceLoader> loaders, Resource resource, String sourceName) throws IOException {
return Optional.of(resource)
.filter(this::isFile)
.map(res -> loaders.stream()
.flatMap(res -> loaders.stream()
.filter(loader -> canLoadFileExtension(loader, resource))
.findFirst()
.map(loader -> load(loader, sourceName, resource))
.orElse(null));
.map(loader -> load(loader, sourceName, resource)));
}

@SneakyThrows
Expand All @@ -134,7 +145,7 @@ private List<PropertySource<?>> load(PropertySourceLoader loader, String sourceN

private boolean canLoadFileExtension(PropertySourceLoader loader, Resource resource) {
return Arrays.stream(loader.getFileExtensions())
.anyMatch(extension -> resource.getFilename().toLowerCase().endsWith("." + extension.toLowerCase()));
.anyMatch(extension -> Objects.requireNonNull(resource.getFilename()).toLowerCase().endsWith("." + extension.toLowerCase()));
}

private boolean isFile(Resource resource) {
Expand All @@ -144,6 +155,6 @@ private boolean isFile(Resource resource) {

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
return Ordered.LOWEST_PRECEDENCE - 100;
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-parent</artifactId>
<packaging>pom</packaging>
<version>2.1.3-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
<name>Jasypt Spring Boot Parent</name>
<description>Jasypt Spring Boot Parent</description>
<url>https://github.com/ulisesbocchio/jasypt-spring-boot</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring.boot.version>2.1.0.RELEASE</spring.boot.version>
<spring.boot.version>2.2.1.RELEASE</spring.boot.version>
<spring.cloud.version>Finchley.RELEASE</spring.cloud.version>
<jasypt.version>1.9.3</jasypt.version>
<maven.compiler.version>3.7.0</maven.compiler.version>
Expand Down

0 comments on commit 4303c3c

Please sign in to comment.