|
15 | 15 | */ |
16 | 16 | package com.example; |
17 | 17 |
|
| 18 | +import java.util.function.Supplier; |
| 19 | + |
18 | 20 | import java.util.Collections; |
19 | 21 | import java.util.List; |
20 | 22 | import java.util.Map; |
21 | | -import javax.servlet.http.HttpServletRequest; |
22 | | - |
23 | 23 | import org.springframework.boot.SpringApplication; |
24 | 24 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
25 | 25 | import org.springframework.context.annotation.Bean; |
26 | 26 | import org.springframework.http.HttpStatus; |
27 | 27 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
28 | | -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
29 | 28 | import org.springframework.security.core.annotation.AuthenticationPrincipal; |
30 | 29 | import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; |
31 | 30 | import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
|
37 | 36 | import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
38 | 37 | import org.springframework.security.oauth2.core.OAuth2Error; |
39 | 38 | import org.springframework.security.oauth2.core.user.OAuth2User; |
| 39 | +import org.springframework.security.web.SecurityFilterChain; |
40 | 40 | import org.springframework.security.web.authentication.HttpStatusEntryPoint; |
41 | 41 | import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; |
42 | 42 | import org.springframework.security.web.csrf.CookieCsrfTokenRepository; |
| 43 | +import org.springframework.security.web.csrf.CsrfToken; |
| 44 | +import org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler; |
| 45 | +import org.springframework.security.web.csrf.CsrfTokenRequestHandler; |
| 46 | +import org.springframework.security.web.csrf.XorCsrfTokenRequestAttributeHandler; |
43 | 47 | import org.springframework.stereotype.Controller; |
| 48 | +import org.springframework.util.StringUtils; |
44 | 49 | import org.springframework.web.bind.annotation.GetMapping; |
45 | 50 | import org.springframework.web.bind.annotation.ResponseBody; |
46 | 51 | import org.springframework.web.reactive.function.client.WebClient; |
47 | 52 |
|
| 53 | +import jakarta.servlet.http.HttpServletRequest; |
| 54 | +import jakarta.servlet.http.HttpServletResponse; |
| 55 | + |
48 | 56 | import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient; |
49 | 57 |
|
50 | 58 | @SpringBootApplication |
51 | 59 | @Controller |
52 | | -public class SocialApplication extends WebSecurityConfigurerAdapter { |
| 60 | +public class SocialApplication { |
53 | 61 |
|
54 | 62 | @Bean |
55 | 63 | public WebClient rest(ClientRegistrationRepository clients, OAuth2AuthorizedClientRepository authz) { |
@@ -100,36 +108,76 @@ public String error(HttpServletRequest request) { |
100 | 108 | return message; |
101 | 109 | } |
102 | 110 |
|
103 | | - @Override |
104 | | - protected void configure(HttpSecurity http) throws Exception { |
| 111 | + |
| 112 | + @Bean |
| 113 | + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
105 | 114 | SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler("/"); |
106 | 115 |
|
107 | 116 | // @formatter:off |
108 | | - http.antMatcher("/**") |
109 | | - .authorizeRequests(a -> a |
110 | | - .antMatchers("/", "/error", "/webjars/**").permitAll() |
| 117 | + http |
| 118 | + .authorizeHttpRequests(auth -> auth |
| 119 | + .requestMatchers("/", "/index.html", "/error", "/webjars/**").permitAll() |
111 | 120 | .anyRequest().authenticated() |
112 | 121 | ) |
113 | 122 | .exceptionHandling(e -> e |
114 | 123 | .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)) |
115 | 124 | ) |
116 | | - .csrf(c -> c |
117 | | - .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) |
| 125 | + .csrf(csrf -> csrf |
| 126 | + .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) |
| 127 | + .csrfTokenRequestHandler(new SpaCsrfTokenRequestHandler()) |
118 | 128 | ) |
119 | 129 | .logout(l -> l |
120 | 130 | .logoutSuccessUrl("/").permitAll() |
121 | 131 | ) |
122 | | - .oauth2Login(o -> o |
| 132 | + .oauth2Login(oauth -> oauth |
| 133 | + .defaultSuccessUrl("/", true) // Always redirect to home after login |
123 | 134 | .failureHandler((request, response, exception) -> { |
124 | 135 | request.getSession().setAttribute("error.message", exception.getMessage()); |
125 | 136 | handler.onAuthenticationFailure(request, response, exception); |
126 | 137 | }) |
127 | | - ); |
| 138 | + ); |
128 | 139 | // @formatter:on |
| 140 | + return http.build(); |
129 | 141 | } |
130 | 142 |
|
131 | 143 | public static void main(String[] args) { |
132 | 144 | SpringApplication.run(SocialApplication.class, args); |
133 | 145 | } |
134 | 146 |
|
135 | 147 | } |
| 148 | + |
| 149 | +final class SpaCsrfTokenRequestHandler implements CsrfTokenRequestHandler { |
| 150 | + private final CsrfTokenRequestHandler plain = new CsrfTokenRequestAttributeHandler(); |
| 151 | + private final CsrfTokenRequestHandler xor = new XorCsrfTokenRequestAttributeHandler(); |
| 152 | + |
| 153 | + @Override |
| 154 | + public void handle(HttpServletRequest request, HttpServletResponse response, Supplier<CsrfToken> csrfToken) { |
| 155 | + /* |
| 156 | + * Always use XorCsrfTokenRequestAttributeHandler to provide BREACH protection of |
| 157 | + * the CsrfToken when it is rendered in the response body. |
| 158 | + */ |
| 159 | + this.xor.handle(request, response, csrfToken); |
| 160 | + /* |
| 161 | + * Render the token value to a cookie by causing the deferred token to be loaded. |
| 162 | + */ |
| 163 | + csrfToken.get(); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public String resolveCsrfTokenValue(HttpServletRequest request, CsrfToken csrfToken) { |
| 168 | + String headerValue = request.getHeader(csrfToken.getHeaderName()); |
| 169 | + /* |
| 170 | + * If the request contains a request header, use CsrfTokenRequestAttributeHandler |
| 171 | + * to resolve the CsrfToken. This applies when a single-page application includes |
| 172 | + * the header value automatically, which was obtained via a cookie containing the |
| 173 | + * raw CsrfToken. |
| 174 | + * |
| 175 | + * In all other cases (e.g. if the request contains a request parameter), use |
| 176 | + * XorCsrfTokenRequestAttributeHandler to resolve the CsrfToken. This applies |
| 177 | + * when a server-side rendered form includes the _csrf request parameter as a |
| 178 | + * hidden input. |
| 179 | + */ |
| 180 | + return (StringUtils.hasText(headerValue) ? this.plain : this.xor).resolveCsrfTokenValue(request, csrfToken); |
| 181 | + } |
| 182 | +} |
| 183 | + |
0 commit comments