Skip to content

Commit

Permalink
Auto configurations conditional on web application
Browse files Browse the repository at this point in the history
Fixes #405
  • Loading branch information
whiskeysierra committed Dec 4, 2019
1 parent b454348 commit 9b7d800
Show file tree
Hide file tree
Showing 22 changed files with 372 additions and 135 deletions.
16 changes: 1 addition & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.zalando</groupId>
<artifactId>problem-spring-parent</artifactId>
<version>0.26.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Problem: Spring Parent</name>
<url>https://github.com/zalando/problem-spring-web</url>

<organization>
<name>Zalando SE</name>
</organization>

<inceptionYear>2015</inceptionYear>

<licenses>
<license>
<name>MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>

<modules>
<module>problem-violations</module>
<module>problem-spring-common</module>
<module>problem-spring-web</module>
<module>problem-spring-web-starter</module>
<module>problem-spring-web-autoconfigure</module>
</modules>

<developers>
<developer>
<name>Lukas Niemeier</name>
Expand All @@ -47,13 +39,11 @@
<organizationUrl>https://tech.zalando.com/</organizationUrl>
</developer>
</developers>

<scm>
<url>https://github.com/zalando/problem-spring-web</url>
<connection>scm:git:[email protected]:zalando/problem-spring-web.git</connection>
<developerConnection>scm:git:[email protected]:zalando/problem-spring-web.git</developerConnection>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand All @@ -68,7 +58,6 @@
<spring-boot.version>2.2.1.RELEASE</spring-boot.version>
<junit-jupiter.version>5.5.2</junit-jupiter.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apiguardian</groupId>
Expand Down Expand Up @@ -267,7 +256,6 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -351,6 +339,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<failIfNoTests>true</failIfNoTests>
<parallel>classesAndMethods</parallel>
<threadCount>1</threadCount>
<perCoreThreadCount>true</perCoreThreadCount>
Expand Down Expand Up @@ -416,7 +405,6 @@
</plugin>
</plugins>
</build>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand All @@ -427,7 +415,6 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<profiles>
<profile>
<id>webflux</id>
Expand Down Expand Up @@ -500,5 +487,4 @@
</build>
</profile>
</profiles>

</project>
9 changes: 0 additions & 9 deletions problem-spring-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-parent</artifactId>
<version>0.26.0-SNAPSHOT</version>
</parent>

<artifactId>problem-spring-common</artifactId>

<name>Problem: Spring Common</name>
<description>Spring common Advices for Problems</description>


<dependencies>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-violations</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand All @@ -39,5 +31,4 @@
</exclusions>
</dependency>
</dependencies>

</project>
24 changes: 17 additions & 7 deletions problem-spring-web-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
<artifactId>problem-spring-parent</artifactId>
<version>0.26.0-SNAPSHOT</version>
</parent>

<artifactId>problem-spring-web-autoconfigure</artifactId>

<name>Problem: Spring Web Autoconfiguration</name>
<description>Spring autoconfiguration module for Problem Spring Web</description>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -63,12 +58,27 @@
<version>${spring-security.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.zalando.problem.spring.web.autoconfigure;

import org.apiguardian.api.API;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.zalando.problem.spring.web.advice.ProblemHandling;

import static org.apiguardian.api.API.Status.INTERNAL;

@API(status = INTERNAL)
@ControllerAdvice
final class ExceptionHandling implements ProblemHandling {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.zalando.problem.spring.web.autoconfigure;

import org.apiguardian.api.API;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
import org.zalando.problem.spring.web.advice.AdviceTrait;

import static org.apiguardian.api.API.Status.INTERNAL;

@API(status = INTERNAL)
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(WebSecurityConfigurer.class)
public class ProblemAutoConfiguration {

@Bean
@ConditionalOnMissingBean(AdviceTrait.class)
public AdviceTrait exceptionHandling() {
return new ExceptionHandling();
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package org.zalando.problem.spring.web.autoconfigure;

import org.apiguardian.api.API;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zalando.problem.ProblemModule;
import org.zalando.problem.violations.ConstraintViolationProblemModule;

import static org.apiguardian.api.API.Status.INTERNAL;

/**
* Registers Jackson modules when WebMvc AutoConfig is enabled
* Registers Problem Jackson modules when {@link WebMvcAutoConfiguration} is
* enabled.
*/
@API(status = INTERNAL)
@Configuration
@ConditionalOnWebApplication
@ConditionalOnMissingBean(JacksonModulesRegistratorWithoutMvcAutoConfig.class)
public class JacksonModulesRegistrator {
@ConditionalOnMissingBean(ProblemJacksonWebMvcAutoConfiguration.class)
public class ProblemJacksonAutoConfiguration {

@Bean
public ProblemModule problemModule() {
return new ProblemModule();
Expand All @@ -23,4 +30,5 @@ public ProblemModule problemModule() {
public ConstraintViolationProblemModule constraintViolationProblemModule() {
return new ConstraintViolationProblemModule();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.zalando.problem.spring.web.autoconfigure;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apiguardian.api.API;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
Expand All @@ -9,25 +10,34 @@
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.zalando.problem.ProblemModule;
import org.zalando.problem.violations.ConstraintViolationProblemModule;

import java.util.List;

import static org.apiguardian.api.API.Status.INTERNAL;

/**
* Registers jackson modules when using @EnableWebMvc, which deactivates WebMvc AutoConfig
* Registers Problem Jackson modules when using {@link EnableWebMvc}, which
* deactivates {@link WebMvcAutoConfiguration}.
*/
@API(status = INTERNAL)
@Configuration
@ConditionalOnWebApplication
@ConditionalOnBean(WebMvcConfigurationSupport.class)
@AutoConfigureBefore(WebMvcAutoConfiguration.class)
public class JacksonModulesRegistratorWithoutMvcAutoConfig implements WebMvcConfigurer {
public class ProblemJacksonWebMvcAutoConfiguration implements WebMvcConfigurer {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
.modules(new ProblemModule(), new ConstraintViolationProblemModule()).build();
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {
final ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
.modules(
new ProblemModule(),
new ConstraintViolationProblemModule())
.build();

converters.add(new MappingJackson2HttpMessageConverter(mapper));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.zalando.problem.spring.web.autoconfigure.security;

import lombok.AllArgsConstructor;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.zalando.problem.spring.web.advice.AdviceTrait;
import org.zalando.problem.spring.web.advice.security.SecurityProblemSupport;
import org.zalando.problem.spring.web.autoconfigure.ProblemAutoConfiguration;

import static org.apiguardian.api.API.Status.INTERNAL;

@API(status = INTERNAL)
@AllArgsConstructor(onConstructor = @__(@Autowired))
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(WebSecurityConfigurer.class)
@ConditionalOnBean(WebSecurityConfiguration.class)
@Import(SecurityProblemSupport.class)
//subtract random, uncommon number to reduce chances of collision with a user-selected order
@Order(Ordered.LOWEST_PRECEDENCE - 21)
@AutoConfigureAfter(SecurityAutoConfiguration.class)
@AutoConfigureBefore(ProblemAutoConfiguration.class)
public class ProblemSecurityAutoConfiguration extends WebSecurityConfigurerAdapter {

private final SecurityProblemSupport support;

@Override
public void configure(final HttpSecurity http) throws Exception {
http.exceptionHandling()
.authenticationEntryPoint(support)
.accessDeniedHandler(support);
}

@Bean
@ConditionalOnMissingBean(AdviceTrait.class)
public AdviceTrait securityExceptionHandling() {
return new SecurityExceptionHandling();
}

}
Loading

0 comments on commit 9b7d800

Please sign in to comment.