SecurityConfig.groovy 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. import boot.SavingAuthenticationProvider
  6. import org.springframework.context.annotation.Bean
  7. import org.springframework.context.annotation.Configuration
  8. import org.springframework.core.annotation.Order
  9. import org.springframework.security.config.annotation.web.builders.HttpSecurity
  10. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
  11. import org.springframework.security.web.SecurityFilterChain
  12. @Configuration
  13. @EnableWebSecurity
  14. class SecurityConfig {
  15. @Bean
  16. SavingAuthenticationProvider savingAuthenticationProvider() {
  17. return new SavingAuthenticationProvider()
  18. }
  19. /**
  20. * Following configuration is required for unauthorised call tests (form would redirect, we need 401)
  21. */
  22. @Bean
  23. @Order(1)
  24. SecurityFilterChain apiWebSecurity(HttpSecurity http, SavingAuthenticationProvider savingAuthenticationProvider) {
  25. return http
  26. .csrf().disable()
  27. .securityMatcher("/basicsecured/**")
  28. .authorizeHttpRequests()
  29. .requestMatchers("/basicsecured/**").authenticated()
  30. .and()
  31. .httpBasic()
  32. .and()
  33. .authenticationProvider(savingAuthenticationProvider)
  34. .build()
  35. }
  36. /**
  37. * Following configuration is required in order to get form login, needed by password tests
  38. */
  39. @Bean
  40. SecurityFilterChain formLoginWebSecurity(HttpSecurity http, SavingAuthenticationProvider savingAuthenticationProvider) {
  41. return http
  42. .csrf().disable()
  43. .authorizeHttpRequests()
  44. .requestMatchers("/formsecured/**").authenticated()
  45. .anyRequest().permitAll()
  46. .and()
  47. .formLogin()
  48. .and()
  49. .authenticationProvider(savingAuthenticationProvider)
  50. .build()
  51. }
  52. }