|
| 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.config; |
| 17 | + |
| 18 | +import java.util.LinkedHashMap; |
| 19 | + |
| 20 | +import org.springframework.beans.factory.annotation.Value; |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Configuration; |
| 23 | +import org.springframework.http.HttpStatus; |
| 24 | +import org.springframework.http.MediaType; |
| 25 | +import org.springframework.security.config.Customizer; |
| 26 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 27 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 28 | +import org.springframework.security.web.AuthenticationEntryPoint; |
| 29 | +import org.springframework.security.web.SecurityFilterChain; |
| 30 | +import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint; |
| 31 | +import org.springframework.security.web.authentication.HttpStatusEntryPoint; |
| 32 | +import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; |
| 33 | +import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; |
| 34 | +import org.springframework.security.web.authentication.logout.CompositeLogoutHandler; |
| 35 | +import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler; |
| 36 | +import org.springframework.security.web.authentication.logout.LogoutHandler; |
| 37 | +import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; |
| 38 | +import org.springframework.security.web.csrf.CookieCsrfTokenRepository; |
| 39 | +import org.springframework.security.web.csrf.CsrfLogoutHandler; |
| 40 | +import org.springframework.security.web.csrf.CsrfTokenRepository; |
| 41 | +import org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler; |
| 42 | +import org.springframework.security.web.util.matcher.MediaTypeRequestMatcher; |
| 43 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Joe Grandja |
| 47 | + * @since 1.4 |
| 48 | + */ |
| 49 | +@Configuration(proxyBeanMethods = false) |
| 50 | +@EnableWebSecurity |
| 51 | +public class SecurityConfig { |
| 52 | + |
| 53 | + @Value("${app.base-uri}") |
| 54 | + private String appBaseUri; |
| 55 | + |
| 56 | + @Bean |
| 57 | + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 58 | + CookieCsrfTokenRepository cookieCsrfTokenRepository = CookieCsrfTokenRepository.withHttpOnlyFalse(); |
| 59 | + CsrfTokenRequestAttributeHandler csrfTokenRequestAttributeHandler = new CsrfTokenRequestAttributeHandler(); |
| 60 | + /* |
| 61 | + IMPORTANT: |
| 62 | + Set the csrfRequestAttributeName to null, to opt-out of deferred tokens, resulting in the CsrfToken to be loaded on every request. |
| 63 | + If it does not exist, the CookieCsrfTokenRepository will automatically generate a new one and add the Cookie to the response. |
| 64 | + See the reference: https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html#deferred-csrf-token |
| 65 | + */ |
| 66 | + csrfTokenRequestAttributeHandler.setCsrfRequestAttributeName(null); |
| 67 | + |
| 68 | + // @formatter:off |
| 69 | + http |
| 70 | + .authorizeHttpRequests(authorize -> |
| 71 | + authorize |
| 72 | + .anyRequest().authenticated() |
| 73 | + ) |
| 74 | + .csrf(csrf -> |
| 75 | + csrf |
| 76 | + .csrfTokenRepository(cookieCsrfTokenRepository) |
| 77 | + .csrfTokenRequestHandler(csrfTokenRequestAttributeHandler) |
| 78 | + ) |
| 79 | + .cors(Customizer.withDefaults()) |
| 80 | + .exceptionHandling(exceptionHandling -> |
| 81 | + exceptionHandling |
| 82 | + .authenticationEntryPoint(authenticationEntryPoint()) |
| 83 | + ) |
| 84 | + .oauth2Login(oauth2Login -> |
| 85 | + oauth2Login |
| 86 | + .successHandler(new SimpleUrlAuthenticationSuccessHandler(this.appBaseUri))) |
| 87 | + .logout(logout -> |
| 88 | + logout |
| 89 | + .addLogoutHandler(logoutHandler(cookieCsrfTokenRepository)) |
| 90 | + .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK)) |
| 91 | + ) |
| 92 | + .oauth2Client(Customizer.withDefaults()); |
| 93 | + // @formatter:on |
| 94 | + return http.build(); |
| 95 | + } |
| 96 | + |
| 97 | + private AuthenticationEntryPoint authenticationEntryPoint() { |
| 98 | + AuthenticationEntryPoint authenticationEntryPoint = |
| 99 | + new LoginUrlAuthenticationEntryPoint("/oauth2/authorization/messaging-client-oidc"); |
| 100 | + MediaTypeRequestMatcher textHtmlMatcher = |
| 101 | + new MediaTypeRequestMatcher(MediaType.TEXT_HTML); |
| 102 | + textHtmlMatcher.setUseEquals(true); |
| 103 | + |
| 104 | + LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints = new LinkedHashMap<>(); |
| 105 | + entryPoints.put(textHtmlMatcher, authenticationEntryPoint); |
| 106 | + |
| 107 | + DelegatingAuthenticationEntryPoint delegatingAuthenticationEntryPoint = new DelegatingAuthenticationEntryPoint(entryPoints); |
| 108 | + delegatingAuthenticationEntryPoint.setDefaultEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)); |
| 109 | + return delegatingAuthenticationEntryPoint; |
| 110 | + } |
| 111 | + |
| 112 | + private LogoutHandler logoutHandler(CsrfTokenRepository csrfTokenRepository) { |
| 113 | + return new CompositeLogoutHandler( |
| 114 | + new SecurityContextLogoutHandler(), |
| 115 | + new CsrfLogoutHandler(csrfTokenRepository)); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments