-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
actuator, security, openapi, web validation, zalando
- Loading branch information
Showing
12 changed files
with
182 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
.../obya/blueprint/customer/Application.java → .../java/edu/obya/blueprint/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
...stomer/web/CustomerExceptionHandling.java → ...edu/obya/blueprint/ExceptionHandling.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package edu.obya.blueprint.customer.web; | ||
package edu.obya.blueprint; | ||
|
||
import edu.obya.blueprint.customer.web.CustomerAdviceTrait; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.zalando.problem.spring.web.advice.ProblemHandling; | ||
import org.zalando.problem.spring.web.advice.security.SecurityAdviceTrait; | ||
import org.zalando.problem.spring.web.advice.validation.OpenApiValidationAdviceTrait; | ||
|
||
@ControllerAdvice | ||
public class CustomerExceptionHandling implements | ||
public class ExceptionHandling implements | ||
ProblemHandling, | ||
OpenApiValidationAdviceTrait, | ||
CustomerAdviceTrait { | ||
CustomerAdviceTrait, | ||
SecurityAdviceTrait { | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/edu/obya/blueprint/WebSecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package edu.obya.blueprint; | ||
|
||
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.factory.PasswordEncoderFactories; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.provisioning.InMemoryUserDetailsManager; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
import static org.springframework.security.config.Customizer.withDefaults; | ||
|
||
@EnableGlobalMethodSecurity(prePostEnabled=true) | ||
@EnableWebSecurity | ||
@Configuration | ||
public class WebSecurityConfiguration { | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
http | ||
.authorizeHttpRequests((authz) -> authz | ||
// Matches /customers, /customers/, /customers/123 | ||
.mvcMatchers("/customers/**").hasAnyRole("USER","ADMIN") | ||
.requestMatchers(EndpointRequest.to("health","info","metrics","loggers")).permitAll() | ||
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasAnyRole("ADMIN") | ||
.anyRequest().authenticated() | ||
) | ||
.httpBasic(withDefaults()) | ||
.csrf().disable(); | ||
return http.build(); | ||
} | ||
|
||
@Bean | ||
public WebSecurityCustomizer webSecurityCustomizer() { | ||
// By-passing Security | ||
return (web) -> web.ignoring().antMatchers("/resources/**"); | ||
} | ||
|
||
@Profile("dev") | ||
@Bean | ||
public UserDetailsService userDetailsService() { | ||
// Uses BCrypt as a default "best practice" encoding scheme for now | ||
PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); | ||
|
||
return new InMemoryUserDetailsManager( | ||
User.builder() | ||
.username("test") | ||
.password(passwordEncoder.encode("test")) | ||
.roles("USER") | ||
.build(), | ||
User.builder() | ||
.username("admin") | ||
.password(passwordEncoder.encode("admin")) | ||
.roles("ADMIN") | ||
.build() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/edu/obya/blueprint/customer/web/CustomerHealthCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package edu.obya.blueprint.customer.web; | ||
|
||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CustomerHealthCheck implements HealthIndicator { | ||
@Override | ||
public Health health() { | ||
return Health.up().withDetail("heartRate", "50").build(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/edu/obya/blueprint/customer/web/CustomerSummary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package edu.obya.blueprint.customer.web; | ||
|
||
import edu.obya.blueprint.customer.domain.Customer; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
|
||
@Builder | ||
@Value | ||
public class CustomerSummary { | ||
String id; | ||
String fullName; | ||
String firstName; | ||
String lastName; | ||
|
||
public static CustomerSummary from(Customer customer) { | ||
return CustomerSummary.builder() | ||
.id(customer.getId().getId().toString()) | ||
.firstName(customer.getFirstName()) | ||
.lastName(customer.getLastName()) | ||
.fullName(String.format("%s %s", customer.getFirstName(), customer.getLastName())) | ||
.build(); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters