WebSecurityConfig.java

  1. package no.nav.data.common.security;

  2. import no.nav.data.common.security.azure.AADStatelessAuthenticationFilter;
  3. import no.nav.data.common.security.dto.AppRole;
  4. import no.nav.data.common.web.UserFilter;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Profile;
  8. import org.springframework.http.HttpMethod;
  9. import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
  10. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  12. import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
  13. import org.springframework.security.config.http.SessionCreationPolicy;
  14. import org.springframework.security.web.SecurityFilterChain;
  15. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

  16. @Configuration
  17. @EnableWebSecurity
  18. @EnableMethodSecurity(jsr250Enabled = true)
  19. public class WebSecurityConfig {

  20.     private final UserFilter userFilter = new UserFilter();

  21.     private final AADStatelessAuthenticationFilter aadAuthFilter;

  22.     public WebSecurityConfig(AADStatelessAuthenticationFilter aadAuthFilter) {
  23.         this.aadAuthFilter = aadAuthFilter;
  24.     }

  25.     @Bean
  26.     @Profile("test")
  27.     public SecurityFilterChain testSecurityFilterChain(HttpSecurity httpSecurity) throws Exception {
  28.         httpSecurity
  29.                 .csrf(AbstractHttpConfigurer::disable)
  30.                 .logout(AbstractHttpConfigurer::disable)
  31.                 .sessionManagement((sessionManagement) ->
  32.                         sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
  33.         addFilters(httpSecurity);

  34.         return httpSecurity.build();
  35.     }

  36.     @Bean
  37.     @Profile("!test")
  38.     public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
  39.         httpSecurity
  40.                 .csrf(AbstractHttpConfigurer::disable)
  41.                 .logout(AbstractHttpConfigurer::disable)
  42.                 .sessionManagement((sessionManagement) ->
  43.                         sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
  44.                 .authorizeHttpRequests((auth) -> auth
  45.                         .requestMatchers(allowAllEndpoints()).permitAll()
  46.                         .requestMatchers(HttpMethod.GET, getAndOptionsEndpoints()).permitAll()
  47.                         .requestMatchers(HttpMethod.OPTIONS, getAndOptionsEndpoints()).permitAll()
  48.                         .requestMatchers(adminOnlyEndpoints()).hasRole(AppRole.ADMIN.name())
  49.                         .requestMatchers(HttpMethod.POST, "/resource/multi").permitAll()
  50.                         .requestMatchers("/logout").authenticated()
  51.                         .requestMatchers("/**").hasRole(AppRole.WRITE.name())
  52.                 );

  53.         addFilters(httpSecurity);

  54.         return httpSecurity.build();
  55.     }

  56.     String[] allowAllEndpoints() {
  57.         return new String[]{
  58.                 "/login",
  59.                 "/oauth2/callback",
  60.                 "/userinfo",
  61.                 "/internal/**",
  62.                 "/swagger*/**"
  63.         };
  64.     }

  65.     String[] getAndOptionsEndpoints() {
  66.         return new String[]{
  67.                 "/team/**",
  68.                 "/productarea/**",
  69.                 "/cluster/**",
  70.                 "/naisteam/**",
  71.                 "/resource/**",
  72.                 "/org/**",
  73.                 "/location/**",
  74.                 "/locationTwo/**",
  75.                 "/member/**",
  76.                 "/tag/**",
  77.                 "/contactaddress/**",
  78.                 "/dash/**",
  79.                 "/settings/**",
  80.                 "/notification/**",
  81.                 "/integration/pcat/**"
  82.         };
  83.     }

  84.     String[] adminOnlyEndpoints() {
  85.         return new String[]{
  86.                 "/audit/**",
  87.                 "/settings/**",
  88.                 "/admin/**",
  89.                 "/location/**"
  90.         };
  91.     }

  92.     private void addFilters(HttpSecurity http) {
  93.         // In lightweight mvc tests where authfilter isn't initialized
  94.         if (aadAuthFilter != null) {
  95.             http.addFilterBefore(aadAuthFilter, UsernamePasswordAuthenticationFilter.class);
  96.         }
  97.         http.addFilterAfter(userFilter, UsernamePasswordAuthenticationFilter.class);
  98.     }
  99. }