SecurityConfig.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2020-2024 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.sociallogin;
  17. import org.springframework.context.annotation.Bean;
  18. import org.springframework.context.annotation.Configuration;
  19. import org.springframework.core.annotation.Order;
  20. import org.springframework.http.MediaType;
  21. import org.springframework.security.config.Customizer;
  22. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  23. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  24. import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
  25. import org.springframework.security.web.SecurityFilterChain;
  26. import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
  27. import org.springframework.security.web.util.matcher.MediaTypeRequestMatcher;
  28. @Configuration
  29. @EnableWebSecurity
  30. public class SecurityConfig {
  31. @Bean // <1>
  32. @Order(1)
  33. public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
  34. throws Exception {
  35. OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
  36. OAuth2AuthorizationServerConfigurer.authorizationServer();
  37. // @formatter:off
  38. http
  39. .securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
  40. .with(authorizationServerConfigurer, (authorizationServer) ->
  41. authorizationServer
  42. .oidc(Customizer.withDefaults()) // Enable OpenID Connect 1.0
  43. )
  44. // Redirect to the OAuth 2.0 Login endpoint when not authenticated
  45. // from the authorization endpoint
  46. .exceptionHandling((exceptions) -> exceptions
  47. .defaultAuthenticationEntryPointFor( // <2>
  48. new LoginUrlAuthenticationEntryPoint("/oauth2/authorization/my-client"),
  49. new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
  50. )
  51. );
  52. // @formatter:on
  53. return http.build();
  54. }
  55. @Bean // <3>
  56. @Order(2)
  57. public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
  58. throws Exception {
  59. // @formatter:off
  60. http
  61. .authorizeHttpRequests((authorize) -> authorize
  62. .anyRequest().authenticated()
  63. )
  64. // OAuth2 Login handles the redirect to the OAuth 2.0 Login endpoint
  65. // from the authorization server filter chain
  66. .oauth2Login(Customizer.withDefaults()); // <4>
  67. // @formatter:on
  68. return http.build();
  69. }
  70. }