Skip to content

Commit a7afe5c

Browse files
committedNov 21, 2018
Changes for spring boot 2.1.0 upgrade
1 parent a81feca commit a7afe5c

File tree

5 files changed

+33
-25
lines changed

5 files changed

+33
-25
lines changed
 

‎spring-social/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>2.0.4.RELEASE</version>
17+
<version>2.1.0.RELEASE</version>
1818
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

‎spring-social/src/main/java/com/example/springsocial/config/SecurityConfig.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,24 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
4343
@Autowired
4444
private OAuth2AuthenticationFailureHandler oAuth2AuthenticationFailureHandler;
4545

46+
@Autowired
47+
private HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository;
48+
4649
@Bean
4750
public TokenAuthenticationFilter tokenAuthenticationFilter() {
4851
return new TokenAuthenticationFilter();
4952
}
5053

54+
/*
55+
By default, Spring OAuth2 uses HttpSessionOAuth2AuthorizationRequestRepository to save
56+
the authorization request. But, since our service is stateless, we can't save it in
57+
the session. We'll save the request in a Base64 encoded cookie instead.
58+
*/
59+
@Bean
60+
public HttpCookieOAuth2AuthorizationRequestRepository cookieAuthorizationRequestRepository() {
61+
return new HttpCookieOAuth2AuthorizationRequestRepository();
62+
}
63+
5164
@Override
5265
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
5366
authenticationManagerBuilder
@@ -118,13 +131,4 @@ protected void configure(HttpSecurity http) throws Exception {
118131
// Add our custom Token based authentication filter
119132
http.addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
120133
}
121-
122-
/*
123-
By default, Spring OAuth2 uses HttpSessionOAuth2AuthorizationRequestRepository to save
124-
the authorization request. But, since our service is stateless, we can't save it in
125-
the session. We'll save the request in a Base64 encoded cookie instead.
126-
*/
127-
private AuthorizationRequestRepository<OAuth2AuthorizationRequest> cookieAuthorizationRequestRepository() {
128-
return new HttpCookieOAuth2AuthorizationRequestRepository();
129-
}
130134
}

‎spring-social/src/main/java/com/example/springsocial/security/oauth2/HttpCookieOAuth2AuthorizationRequestRepository.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import com.nimbusds.oauth2.sdk.util.StringUtils;
55
import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository;
66
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
7+
import org.springframework.stereotype.Component;
8+
79
import javax.servlet.http.HttpServletRequest;
810
import javax.servlet.http.HttpServletResponse;
911

12+
@Component
1013
public class HttpCookieOAuth2AuthorizationRequestRepository implements AuthorizationRequestRepository<OAuth2AuthorizationRequest> {
1114
public static final String OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME = "oauth2_auth_request";
1215
public static final String REDIRECT_URI_PARAM_COOKIE_NAME = "redirect_uri";
@@ -36,19 +39,11 @@ public void saveAuthorizationRequest(OAuth2AuthorizationRequest authorizationReq
3639

3740
@Override
3841
public OAuth2AuthorizationRequest removeAuthorizationRequest(HttpServletRequest request) {
39-
/*
40-
Ideally, the saved OAuth2AuthorizationRequest should be removed in this method.
41-
42-
Since we're saving the OAuth2AuthorizationRequest in cookies, we need access to the HttpServletResponse to clear them.
43-
But that is not passed to this method.
44-
45-
Therefore, We'll clear the cookies in OAuth2AuthenticationSuccessHandler instead.
46-
*/
47-
return loadAuthorizationRequest(request);
42+
return this.loadAuthorizationRequest(request);
4843
}
4944

50-
public static void removeAuthorizationRequest(HttpServletRequest request, HttpServletResponse response) {
51-
CookieUtils.deleteCookie(request, response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME);
52-
CookieUtils.deleteCookie(request, response, REDIRECT_URI_PARAM_COOKIE_NAME);
45+
@Override
46+
public OAuth2AuthorizationRequest removeAuthorizationRequest(HttpServletRequest request, HttpServletResponse response) {
47+
return this.loadAuthorizationRequest(request);
5348
}
5449
}

‎spring-social/src/main/java/com/example/springsocial/security/oauth2/OAuth2AuthenticationFailureHandler.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.springsocial.security.oauth2;
22

33
import com.example.springsocial.util.CookieUtils;
4+
import org.springframework.beans.factory.annotation.Autowired;
45
import org.springframework.security.core.AuthenticationException;
56
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
67
import org.springframework.stereotype.Component;
@@ -17,6 +18,9 @@
1718
@Component
1819
public class OAuth2AuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
1920

21+
@Autowired
22+
HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository;
23+
2024
@Override
2125
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
2226
String targetUrl = CookieUtils.getCookie(request, REDIRECT_URI_PARAM_COOKIE_NAME)
@@ -27,7 +31,7 @@ public void onAuthenticationFailure(HttpServletRequest request, HttpServletRespo
2731
.queryParam("error", exception.getLocalizedMessage())
2832
.build().toUriString();
2933

30-
HttpCookieOAuth2AuthorizationRequestRepository.removeAuthorizationRequest(request, response);
34+
httpCookieOAuth2AuthorizationRequestRepository.removeAuthorizationRequest(request, response);
3135

3236
getRedirectStrategy().sendRedirect(request, response, targetUrl);
3337
}

‎spring-social/src/main/java/com/example/springsocial/security/oauth2/OAuth2AuthenticationSuccessHandler.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS
2626

2727
private AppProperties appProperties;
2828

29+
private HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository;
30+
31+
2932
@Autowired
30-
OAuth2AuthenticationSuccessHandler(TokenProvider tokenProvider, AppProperties appProperties) {
33+
OAuth2AuthenticationSuccessHandler(TokenProvider tokenProvider, AppProperties appProperties,
34+
HttpCookieOAuth2AuthorizationRequestRepository httpCookieOAuth2AuthorizationRequestRepository) {
3135
this.tokenProvider = tokenProvider;
3236
this.appProperties = appProperties;
37+
this.httpCookieOAuth2AuthorizationRequestRepository = httpCookieOAuth2AuthorizationRequestRepository;
3338
}
3439

3540
@Override
@@ -64,7 +69,7 @@ protected String determineTargetUrl(HttpServletRequest request, HttpServletRespo
6469

6570
protected void clearAuthenticationAttributes(HttpServletRequest request, HttpServletResponse response) {
6671
super.clearAuthenticationAttributes(request);
67-
HttpCookieOAuth2AuthorizationRequestRepository.removeAuthorizationRequest(request, response);
72+
httpCookieOAuth2AuthorizationRequestRepository.removeAuthorizationRequest(request, response);
6873
}
6974

7075
private boolean isAuthorizedRedirectUri(String uri) {

0 commit comments

Comments
 (0)