SecurityConfig.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2020-2023 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample.config;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.context.annotation.Bean;
  19. import org.springframework.context.annotation.Configuration;
  20. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  21. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  22. import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
  23. import org.springframework.security.oauth2.client.oidc.web.logout.OidcClientInitiatedLogoutSuccessHandler;
  24. import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
  25. import org.springframework.security.web.SecurityFilterChain;
  26. import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
  27. import static org.springframework.security.config.Customizer.withDefaults;
  28. /**
  29. * @author Joe Grandja
  30. * @author Dmitriy Dubson
  31. * @since 0.0.1
  32. */
  33. @EnableWebSecurity
  34. @Configuration(proxyBeanMethods = false)
  35. public class SecurityConfig {
  36. @Autowired
  37. private ClientRegistrationRepository clientRegistrationRepository;
  38. @Bean
  39. WebSecurityCustomizer webSecurityCustomizer() {
  40. return (web) -> web.ignoring().requestMatchers("/webjars/**");
  41. }
  42. // @formatter:off
  43. @Bean
  44. SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
  45. http
  46. .authorizeHttpRequests(authorize ->
  47. authorize
  48. .requestMatchers("/logged-out").permitAll()
  49. .anyRequest().authenticated()
  50. )
  51. .oauth2Login(oauth2Login ->
  52. oauth2Login.loginPage("/oauth2/authorization/messaging-client-oidc"))
  53. .oauth2Client(withDefaults())
  54. .logout(logout ->
  55. logout.logoutSuccessHandler(oidcLogoutSuccessHandler()));
  56. return http.build();
  57. }
  58. // @formatter:on
  59. private LogoutSuccessHandler oidcLogoutSuccessHandler() {
  60. OidcClientInitiatedLogoutSuccessHandler oidcLogoutSuccessHandler =
  61. new OidcClientInitiatedLogoutSuccessHandler(this.clientRegistrationRepository);
  62. // Set the location that the End-User's User Agent will be redirected to
  63. // after the logout has been performed at the Provider
  64. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}/logged-out");
  65. return oidcLogoutSuccessHandler;
  66. }
  67. }