|
53 | 53 | import org.springframework.security.config.annotation.web.configurers.oauth2.client.OAuth2LoginConfigurer;
|
54 | 54 | import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
|
55 | 55 | import org.springframework.security.config.annotation.web.configurers.openid.OpenIDLoginConfigurer;
|
| 56 | +import org.springframework.security.config.annotation.web.configurers.saml2.Saml2LoginConfigurer; |
56 | 57 | import org.springframework.security.core.Authentication;
|
57 | 58 | import org.springframework.security.core.context.SecurityContext;
|
58 | 59 | import org.springframework.security.core.context.SecurityContextHolder;
|
59 | 60 | import org.springframework.security.core.userdetails.UserDetailsService;
|
| 61 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration; |
| 62 | +import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository; |
60 | 63 | import org.springframework.security.web.DefaultSecurityFilterChain;
|
61 | 64 | import org.springframework.security.web.PortMapper;
|
62 | 65 | import org.springframework.security.web.PortMapperImpl;
|
|
75 | 78 | import org.springframework.web.filter.CorsFilter;
|
76 | 79 | import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
|
77 | 80 |
|
78 |
| -import javax.servlet.Filter; |
79 |
| -import javax.servlet.http.HttpServletRequest; |
80 | 81 | import java.util.ArrayList;
|
81 | 82 | import java.util.List;
|
82 | 83 | import java.util.Map;
|
| 84 | +import javax.servlet.Filter; |
| 85 | +import javax.servlet.http.HttpServletRequest; |
83 | 86 |
|
84 | 87 | /**
|
85 | 88 | * A {@link HttpSecurity} is similar to Spring Security's XML <http> element in the
|
@@ -1857,6 +1860,191 @@ public HttpSecurity formLogin(Customizer<FormLoginConfigurer<HttpSecurity>> form
|
1857 | 1860 | return HttpSecurity.this;
|
1858 | 1861 | }
|
1859 | 1862 |
|
| 1863 | + /** |
| 1864 | + * Configures authentication support using an SAML 2.0 Service Provider. |
| 1865 | + * <br> |
| 1866 | + * <br> |
| 1867 | + * |
| 1868 | + * The "authentication flow" is implemented using the <b>Web Browser SSO Profile, using POST and REDIRECT bindings</b>, |
| 1869 | + * as documented in the <a target="_blank" href="https://docs.oasis-open.org/security/saml/">SAML V2.0 Core,Profiles and Bindings</a> |
| 1870 | + * specifications. |
| 1871 | + * <br> |
| 1872 | + * <br> |
| 1873 | + * |
| 1874 | + * As a prerequisite to using this feature, is that you have a SAML v2.0 Identity Provider to provide an assertion. |
| 1875 | + * The representation of the Service Provider, the relying party, and the remote Identity Provider, the asserting party |
| 1876 | + * is contained within {@link RelyingPartyRegistration}. |
| 1877 | + * <br> |
| 1878 | + * <br> |
| 1879 | + * |
| 1880 | + * {@link RelyingPartyRegistration}(s) are composed within a |
| 1881 | + * {@link RelyingPartyRegistrationRepository}, |
| 1882 | + * which is <b>required</b> and must be registered with the {@link ApplicationContext} or |
| 1883 | + * configured via <code>saml2Login().relyingPartyRegistrationRepository(..)</code>. |
| 1884 | + * <br> |
| 1885 | + * <br> |
| 1886 | + * |
| 1887 | + * The default configuration provides an auto-generated login page at <code>"/login"</code> and |
| 1888 | + * redirects to <code>"/login?error"</code> when an authentication error occurs. |
| 1889 | + * The login page will display each of the identity providers with a link |
| 1890 | + * that is capable of initiating the "authentication flow". |
| 1891 | + * <br> |
| 1892 | + * <br> |
| 1893 | + * |
| 1894 | + * <p> |
| 1895 | + * <h2>Example Configuration</h2> |
| 1896 | + * |
| 1897 | + * The following example shows the minimal configuration required, using SimpleSamlPhp as the Authentication Provider. |
| 1898 | + * |
| 1899 | + * <pre> |
| 1900 | + * @Configuration |
| 1901 | + * public class Saml2LoginConfig { |
| 1902 | + * |
| 1903 | + * @EnableWebSecurity |
| 1904 | + * public static class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter { |
| 1905 | + * @Override |
| 1906 | + * protected void configure(HttpSecurity http) throws Exception { |
| 1907 | + * http |
| 1908 | + * .authorizeRequests() |
| 1909 | + * .anyRequest().authenticated() |
| 1910 | + * .and() |
| 1911 | + * .saml2Login(); |
| 1912 | + * } |
| 1913 | + * } |
| 1914 | + * |
| 1915 | + * @Bean |
| 1916 | + * public RelyingPartyRegistrationRepository relyingPartyRegistrationRepository() { |
| 1917 | + * return new InMemoryRelyingPartyRegistrationRepository(this.getSaml2RelyingPartyRegistration()); |
| 1918 | + * } |
| 1919 | + * |
| 1920 | + * private RelyingPartyRegistration getSaml2RelyingPartyRegistration() { |
| 1921 | + * //remote IDP entity ID |
| 1922 | + * String idpEntityId = "https://simplesaml-for-spring-saml.cfapps.io/saml2/idp/metadata.php"; |
| 1923 | + * //remote WebSSO Endpoint - Where to Send AuthNRequests to |
| 1924 | + * String webSsoEndpoint = "https://simplesaml-for-spring-saml.cfapps.io/saml2/idp/SSOService.php"; |
| 1925 | + * //local registration ID |
| 1926 | + * String registrationId = "simplesamlphp"; |
| 1927 | + * //local entity ID - autogenerated based on URL |
| 1928 | + * String localEntityIdTemplate = "{baseUrl}/saml2/service-provider-metadata/{registrationId}"; |
| 1929 | + * //local signing (and decryption key) |
| 1930 | + * Saml2X509Credential signingCredential = getSigningCredential(); |
| 1931 | + * //IDP certificate for verification of incoming messages |
| 1932 | + * Saml2X509Credential idpVerificationCertificate = getVerificationCertificate(); |
| 1933 | + * return RelyingPartyRegistration.withRegistrationId(registrationId) |
| 1934 | + * * .remoteIdpEntityId(idpEntityId) |
| 1935 | + * * .idpWebSsoUrl(webSsoEndpoint) |
| 1936 | + * * .credential(signingCredential) |
| 1937 | + * * .credential(idpVerificationCertificate) |
| 1938 | + * * .localEntityIdTemplate(localEntityIdTemplate) |
| 1939 | + * * .build(); |
| 1940 | + * } |
| 1941 | + * } |
| 1942 | + * </pre> |
| 1943 | + * |
| 1944 | + * <p> |
| 1945 | + * |
| 1946 | + * @since 5.2 |
| 1947 | + * @return the {@link Saml2LoginConfigurer} for further customizations |
| 1948 | + * @throws Exception |
| 1949 | + */ |
| 1950 | + public Saml2LoginConfigurer<HttpSecurity> saml2Login() throws Exception { |
| 1951 | + return getOrApply(new Saml2LoginConfigurer<>()); |
| 1952 | + } |
| 1953 | + |
| 1954 | + /** |
| 1955 | + * Configures authentication support using an SAML 2.0 Service Provider. |
| 1956 | + * <br> |
| 1957 | + * <br> |
| 1958 | + * |
| 1959 | + * The "authentication flow" is implemented using the <b>Web Browser SSO Profile, using POST and REDIRECT bindings</b>, |
| 1960 | + * as documented in the <a target="_blank" href="https://docs.oasis-open.org/security/saml/">SAML V2.0 Core,Profiles and Bindings</a> |
| 1961 | + * specifications. |
| 1962 | + * <br> |
| 1963 | + * <br> |
| 1964 | + * |
| 1965 | + * As a prerequisite to using this feature, is that you have a SAML v2.0 Identity Provider to provide an assertion. |
| 1966 | + * The representation of the Service Provider, the relying party, and the remote Identity Provider, the asserting party |
| 1967 | + * is contained within {@link RelyingPartyRegistration}. |
| 1968 | + * <br> |
| 1969 | + * <br> |
| 1970 | + * |
| 1971 | + * {@link RelyingPartyRegistration}(s) are composed within a |
| 1972 | + * {@link RelyingPartyRegistrationRepository}, |
| 1973 | + * which is <b>required</b> and must be registered with the {@link ApplicationContext} or |
| 1974 | + * configured via <code>saml2Login().relyingPartyRegistrationRepository(..)</code>. |
| 1975 | + * <br> |
| 1976 | + * <br> |
| 1977 | + * |
| 1978 | + * The default configuration provides an auto-generated login page at <code>"/login"</code> and |
| 1979 | + * redirects to <code>"/login?error"</code> when an authentication error occurs. |
| 1980 | + * The login page will display each of the identity providers with a link |
| 1981 | + * that is capable of initiating the "authentication flow". |
| 1982 | + * <br> |
| 1983 | + * <br> |
| 1984 | + * |
| 1985 | + * <p> |
| 1986 | + * <h2>Example Configuration</h2> |
| 1987 | + * |
| 1988 | + * The following example shows the minimal configuration required, using SimpleSamlPhp as the Authentication Provider. |
| 1989 | + * |
| 1990 | + * <pre> |
| 1991 | + * @Configuration |
| 1992 | + * public class Saml2LoginConfig { |
| 1993 | + * |
| 1994 | + * @EnableWebSecurity |
| 1995 | + * public static class OAuth2LoginSecurityConfig extends WebSecurityConfigurerAdapter { |
| 1996 | + * @Override |
| 1997 | + * protected void configure(HttpSecurity http) throws Exception { |
| 1998 | + * http |
| 1999 | + * .authorizeRequests() |
| 2000 | + * .anyRequest().authenticated() |
| 2001 | + * .and() |
| 2002 | + * .saml2Login(withDefaults()); |
| 2003 | + * } |
| 2004 | + * } |
| 2005 | + * |
| 2006 | + * @Bean |
| 2007 | + * public RelyingPartyRegistrationRepository relyingPartyRegistrationRepository() { |
| 2008 | + * return new InMemoryRelyingPartyRegistrationRepository(this.getSaml2RelyingPartyRegistration()); |
| 2009 | + * } |
| 2010 | + * |
| 2011 | + * private RelyingPartyRegistration getSaml2RelyingPartyRegistration() { |
| 2012 | + * //remote IDP entity ID |
| 2013 | + * String idpEntityId = "https://simplesaml-for-spring-saml.cfapps.io/saml2/idp/metadata.php"; |
| 2014 | + * //remote WebSSO Endpoint - Where to Send AuthNRequests to |
| 2015 | + * String webSsoEndpoint = "https://simplesaml-for-spring-saml.cfapps.io/saml2/idp/SSOService.php"; |
| 2016 | + * //local registration ID |
| 2017 | + * String registrationId = "simplesamlphp"; |
| 2018 | + * //local entity ID - autogenerated based on URL |
| 2019 | + * String localEntityIdTemplate = "{baseUrl}/saml2/service-provider-metadata/{registrationId}"; |
| 2020 | + * //local signing (and decryption key) |
| 2021 | + * Saml2X509Credential signingCredential = getSigningCredential(); |
| 2022 | + * //IDP certificate for verification of incoming messages |
| 2023 | + * Saml2X509Credential idpVerificationCertificate = getVerificationCertificate(); |
| 2024 | + * return RelyingPartyRegistration.withRegistrationId(registrationId) |
| 2025 | + * * .remoteIdpEntityId(idpEntityId) |
| 2026 | + * * .idpWebSsoUrl(webSsoEndpoint) |
| 2027 | + * * .credential(signingCredential) |
| 2028 | + * * .credential(idpVerificationCertificate) |
| 2029 | + * * .localEntityIdTemplate(localEntityIdTemplate) |
| 2030 | + * * .build(); |
| 2031 | + * } |
| 2032 | + * } |
| 2033 | + * </pre> |
| 2034 | + * |
| 2035 | + * <p> |
| 2036 | + * |
| 2037 | + * @since 5.2 |
| 2038 | + * @param saml2LoginCustomizer the {@link Customizer} to provide more options for |
| 2039 | + * the {@link Saml2LoginConfigurer} |
| 2040 | + * @return the {@link HttpSecurity} for further customizations |
| 2041 | + * @throws Exception |
| 2042 | + */ |
| 2043 | + public HttpSecurity saml2Login(Customizer<Saml2LoginConfigurer<HttpSecurity>> saml2LoginCustomizer) throws Exception { |
| 2044 | + saml2LoginCustomizer.customize(getOrApply(new Saml2LoginConfigurer<>())); |
| 2045 | + return HttpSecurity.this; |
| 2046 | + } |
| 2047 | + |
1860 | 2048 | /**
|
1861 | 2049 | * Configures authentication support using an OAuth 2.0 and/or OpenID Connect 1.0 Provider.
|
1862 | 2050 | * <br>
|
|
0 commit comments