Skip to content

Commit

Permalink
Updates SecurityConfig for certain apps to be able to run application…
Browse files Browse the repository at this point in the history
… context tests with actual checks on /internal.
  • Loading branch information
rfc3092 committed Feb 20, 2025
1 parent c8026d2 commit d946da4
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -11,12 +10,10 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;


@Order(1)
@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package no.nav.dolly.web.config;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import no.nav.testnav.libs.reactivesecurity.properties.AzureAdResourceServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
Expand All @@ -12,32 +11,38 @@
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoders;
import org.springframework.security.web.server.SecurityWebFilterChain;

import reactor.core.publisher.Mono;

@Slf4j
@Configuration
@Profile({ "prod", "dev" })
@EnableWebFluxSecurity
@RequiredArgsConstructor
public class SecurityConfig {

class SecurityConfig {

@Value("${spring.security.oauth2.resourceserver.aad.issuer-uri}")
private String aadIssuer;
private final AzureAdResourceServerProperties config;

@SneakyThrows
@Bean
public SecurityWebFilterChain configure(ServerHttpSecurity http) {

http.cors(ServerHttpSecurity.CorsSpec::disable)
SecurityWebFilterChain configure(ServerHttpSecurity http) {
return http
.cors(ServerHttpSecurity.CorsSpec::disable)
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.authorizeExchange(authorizeExchangeSpec -> authorizeExchangeSpec.anyExchange().permitAll())
.oauth2ResourceServer(oAuth2ResourceServerSpec -> oAuth2ResourceServerSpec.jwt(jwtSpec -> jwtDecoder()));
return http.build();
.authorizeExchange(spec -> spec
.anyExchange()
.permitAll())
.oauth2ResourceServer(spec -> spec.jwt(jwtSpec -> jwtDecoder()))
.build();
}

@Bean
public ReactiveJwtDecoder jwtDecoder() {
return ReactiveJwtDecoders.fromOidcIssuerLocation(aadIssuer);
@Bean("jwtDecoder")
@Profile("!test")
ReactiveJwtDecoder jwtDecoder() {
return ReactiveJwtDecoders.fromOidcIssuerLocation(config.getIssuerUri());
}

@Bean("jwtDecoder")
@Profile("test")
ReactiveJwtDecoder jwtDecoderForTest() {
return token -> Mono.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -11,12 +10,10 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;


@Order(1)
@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
public class SecurityConfig {
class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({"prod", "local"})
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({"prod", "dev"})
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -12,12 +11,10 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;


@Order(1)
@EnableWebSecurity
@Configuration
@Profile({"prod", "dev"})
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -15,8 +14,7 @@
@Order(1)
@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -12,12 +11,10 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;


@Order(1)
@EnableWebSecurity
@Configuration
@Profile({"prod", "local"})
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({ "prod", "dev" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({ "dev", "prod" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package no.nav.testnav.apps.oversiktfrontend.config;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import no.nav.testnav.libs.reactivesecurity.properties.AzureAdResourceServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
Expand All @@ -13,24 +14,27 @@

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {
@RequiredArgsConstructor
class SecurityConfig {

@Value("${spring.security.oauth2.resourceserver.aad.issuer-uri}")
private String issuer;
private final AzureAdResourceServerProperties config;

@SneakyThrows
@Bean
public SecurityWebFilterChain configure(ServerHttpSecurity http) {

http.cors(Customizer.withDefaults())
SecurityWebFilterChain configure(ServerHttpSecurity http) {
return http
.cors(Customizer.withDefaults())
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.authorizeExchange(authorizeExchangeSpec -> authorizeExchangeSpec.anyExchange().permitAll())
.oauth2ResourceServer(oAuth2ResourceServerSpec -> oAuth2ResourceServerSpec.jwt(jwtSpec -> jwtDecoder()));
return http.build();
.authorizeExchange(spec -> spec
.anyExchange()
.permitAll())
.oauth2ResourceServer(spec -> spec.jwt(jwtSpec -> jwtDecoder()))
.build();
}

@Bean
public ReactiveJwtDecoder jwtDecoder() {
return ReactiveJwtDecoders.fromOidcIssuerLocation(issuer);
ReactiveJwtDecoder jwtDecoder() {
return ReactiveJwtDecoders.fromOidcIssuerLocation(config.getIssuerUri());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand All @@ -12,8 +11,7 @@

@EnableWebSecurity
@Configuration
@Profile({ "prod", "local" })
public class SecurityConfig {
class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
Expand Down
Loading

0 comments on commit d946da4

Please sign in to comment.