1515 */
1616package com .example ;
1717
18- import java .io .IOException ;
1918import java .security .Principal ;
2019import java .util .ArrayList ;
2120import java .util .LinkedHashMap ;
2221import java .util .List ;
2322import java .util .Map ;
2423
2524import javax .servlet .Filter ;
26- import javax .servlet .FilterChain ;
27- import javax .servlet .ServletException ;
28- import javax .servlet .http .Cookie ;
29- import javax .servlet .http .HttpServletRequest ;
30- import javax .servlet .http .HttpServletResponse ;
3125
3226import org .springframework .beans .factory .annotation .Autowired ;
3327import org .springframework .boot .SpringApplication ;
5347import org .springframework .security .oauth2 .config .annotation .web .configuration .ResourceServerConfigurerAdapter ;
5448import org .springframework .security .web .authentication .LoginUrlAuthenticationEntryPoint ;
5549import org .springframework .security .web .authentication .www .BasicAuthenticationFilter ;
56- import org .springframework .security .web .csrf .CsrfFilter ;
57- import org .springframework .security .web .csrf .CsrfToken ;
58- import org .springframework .security .web .csrf .CsrfTokenRepository ;
59- import org .springframework .security .web .csrf .HttpSessionCsrfTokenRepository ;
50+ import org .springframework .security .web .csrf .CookieCsrfTokenRepository ;
6051import org .springframework .web .bind .annotation .RequestMapping ;
6152import org .springframework .web .bind .annotation .RestController ;
6253import org .springframework .web .filter .CompositeFilter ;
63- import org .springframework .web .filter .OncePerRequestFilter ;
64- import org .springframework .web .util .WebUtils ;
6554
6655@ SpringBootApplication
6756@ RestController
@@ -82,29 +71,23 @@ public Map<String, String> user(Principal principal) {
8271
8372@ Override
8473protected void configure (HttpSecurity http ) throws Exception {
85- // @formatter:off
86- http .antMatcher ("/**" )
87- .authorizeRequests ()
88- .antMatchers ("/" , "/login**" , "/webjars/**" ).permitAll ()
89- .anyRequest ().authenticated ()
90- .and ().exceptionHandling ().authenticationEntryPoint (new LoginUrlAuthenticationEntryPoint ("/" ))
91- .and ().logout ().logoutSuccessUrl ("/" ).permitAll ()
92- .and ().csrf ().csrfTokenRepository (csrfTokenRepository ())
93- .and ().addFilterAfter (csrfHeaderFilter (), CsrfFilter .class )
94- .addFilterBefore (ssoFilter (), BasicAuthenticationFilter .class );
74+ // @formatter:off
75+ http .antMatcher ("/**" ).authorizeRequests ().antMatchers ("/" , "/login**" , "/webjars/**" ).permitAll ().anyRequest ()
76+ .authenticated ().and ().exceptionHandling ()
77+ .authenticationEntryPoint (new LoginUrlAuthenticationEntryPoint ("/" )).and ().logout ()
78+ .logoutSuccessUrl ("/" ).permitAll ().and ().csrf ()
79+ .csrfTokenRepository (CookieCsrfTokenRepository .withHttpOnlyFalse ()).and ()
80+ .addFilterBefore (ssoFilter (), BasicAuthenticationFilter .class );
9581// @formatter:on
9682}
9783
9884@ Configuration
9985@ EnableResourceServer
100- protected static class ResourceServerConfiguration
101- extends ResourceServerConfigurerAdapter {
86+ protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
10287@ Override
10388public void configure (HttpSecurity http ) throws Exception {
10489// @formatter:off
105- http
106- .antMatcher ("/me" )
107- .authorizeRequests ().anyRequest ().authenticated ();
90+ http .antMatcher ("/me" ).authorizeRequests ().anyRequest ().authenticated ();
10891// @formatter:on
10992}
11093}
@@ -114,8 +97,7 @@ public static void main(String[] args) {
11497}
11598
11699@ Bean
117- public FilterRegistrationBean oauth2ClientFilterRegistration (
118- OAuth2ClientContextFilter filter ) {
100+ public FilterRegistrationBean oauth2ClientFilterRegistration (OAuth2ClientContextFilter filter ) {
119101FilterRegistrationBean registration = new FilterRegistrationBean ();
120102registration .setFilter (filter );
121103registration .setOrder (-100 );
@@ -144,47 +126,17 @@ private Filter ssoFilter() {
144126}
145127
146128private Filter ssoFilter (ClientResources client , String path ) {
147- OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter =
148- new OAuth2ClientAuthenticationProcessingFilter (path );
149- OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate (client .getClient (),
150- oauth2ClientContext );
129+ OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter (
130+ path );
131+ OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate (client .getClient (), oauth2ClientContext );
151132oAuth2ClientAuthenticationFilter .setRestTemplate (oAuth2RestTemplate );
152- UserInfoTokenServices tokenServices = new UserInfoTokenServices (
153- client .getResource (). getUserInfoUri (), client . getClient ().getClientId ());
133+ UserInfoTokenServices tokenServices = new UserInfoTokenServices (client . getResource (). getUserInfoUri (),
134+ client .getClient ().getClientId ());
154135tokenServices .setRestTemplate (oAuth2RestTemplate );
155136oAuth2ClientAuthenticationFilter .setTokenServices (tokenServices );
156137return oAuth2ClientAuthenticationFilter ;
157138}
158139
159- private Filter csrfHeaderFilter () {
160- return new OncePerRequestFilter () {
161- @ Override
162- protected void doFilterInternal (HttpServletRequest request ,
163- HttpServletResponse response , FilterChain filterChain )
164- throws ServletException , IOException {
165- CsrfToken csrf = (CsrfToken ) request
166- .getAttribute (CsrfToken .class .getName ());
167- if (csrf != null ) {
168- Cookie cookie = WebUtils .getCookie (request , "XSRF-TOKEN" );
169- String token = csrf .getToken ();
170- if (cookie == null
171- || token != null && !token .equals (cookie .getValue ())) {
172- cookie = new Cookie ("XSRF-TOKEN" , token );
173- cookie .setPath ("/" );
174- response .addCookie (cookie );
175- }
176- }
177- filterChain .doFilter (request , response );
178- }
179- };
180- }
181-
182- private CsrfTokenRepository csrfTokenRepository () {
183- HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository ();
184- repository .setHeaderName ("X-XSRF-TOKEN" );
185- return repository ;
186- }
187-
188140}
189141
190142class ClientResources {
0 commit comments