FederatedIdentityAuthenticationEntryPoint.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.security;
  17. import java.io.IOException;
  18. import jakarta.servlet.ServletException;
  19. import jakarta.servlet.http.HttpServletRequest;
  20. import jakarta.servlet.http.HttpServletResponse;
  21. import org.springframework.http.server.ServletServerHttpRequest;
  22. import org.springframework.security.core.AuthenticationException;
  23. import org.springframework.security.oauth2.client.registration.ClientRegistration;
  24. import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
  25. import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter;
  26. import org.springframework.security.web.AuthenticationEntryPoint;
  27. import org.springframework.security.web.DefaultRedirectStrategy;
  28. import org.springframework.security.web.RedirectStrategy;
  29. import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
  30. import org.springframework.web.util.UriComponentsBuilder;
  31. /**
  32. * An {@link AuthenticationEntryPoint} for initiating the login flow to an
  33. * external provider using the {@code idp} query parameter, which represents the
  34. * {@code registrationId} of the desired {@link ClientRegistration}.
  35. *
  36. * @author Steve Riesenberg
  37. * @since 1.1
  38. */
  39. public final class FederatedIdentityAuthenticationEntryPoint implements AuthenticationEntryPoint {
  40. private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
  41. private String authorizationRequestUri = OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI
  42. + "/{registrationId}";
  43. private final AuthenticationEntryPoint delegate;
  44. private final ClientRegistrationRepository clientRegistrationRepository;
  45. public FederatedIdentityAuthenticationEntryPoint(String loginPageUrl, ClientRegistrationRepository clientRegistrationRepository) {
  46. this.delegate = new LoginUrlAuthenticationEntryPoint(loginPageUrl);
  47. this.clientRegistrationRepository = clientRegistrationRepository;
  48. }
  49. @Override
  50. public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
  51. String idp = request.getParameter("idp");
  52. if (idp != null) {
  53. ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId(idp);
  54. if (clientRegistration != null) {
  55. String redirectUri = UriComponentsBuilder.fromHttpRequest(new ServletServerHttpRequest(request))
  56. .replaceQuery(null)
  57. .replacePath(this.authorizationRequestUri)
  58. .buildAndExpand(clientRegistration.getRegistrationId())
  59. .toUriString();
  60. this.redirectStrategy.sendRedirect(request, response, redirectUri);
  61. return;
  62. }
  63. }
  64. this.delegate.commence(request, response, authenticationException);
  65. }
  66. public void setAuthorizationRequestUri(String authorizationRequestUri) {
  67. this.authorizationRequestUri = authorizationRequestUri;
  68. }
  69. }