Skip to content

Commit 83edd89

Browse files
committed
Try implementing custom referrer request cache
1 parent 0ed3e61 commit 83edd89

5 files changed

Lines changed: 164 additions & 203 deletions

File tree

pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@
4141
<groupId>org.springframework.boot</groupId>
4242
<artifactId>spring-boot-starter-security</artifactId>
4343
</dependency>
44-
<dependency>
45-
<groupId>org.springframework.security.oauth.boot</groupId>
46-
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
47-
<version>2.0.1.RELEASE</version>
48-
</dependency>
4944
<dependency>
5045
<groupId>org.springframework.security</groupId>
5146
<artifactId>spring-security-config</artifactId>

src/main/java/com/okta/developer/jugtours/config/OAuth2AuthenticationSuccessHandler.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/main/java/com/okta/developer/jugtours/config/OAuth2Configuration.java

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/main/java/com/okta/developer/jugtours/config/SecurityConfiguration.java

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
package com.okta.developer.jugtours.config;
22

3-
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.context.annotation.Bean;
46
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Profile;
58
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
69
import org.springframework.security.config.annotation.web.builders.WebSecurity;
10+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
711
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12+
import org.springframework.security.web.PortResolver;
13+
import org.springframework.security.web.PortResolverImpl;
814
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
15+
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
16+
import org.springframework.security.web.savedrequest.RequestCache;
17+
import org.springframework.security.web.savedrequest.SavedRequest;
18+
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
19+
import org.springframework.security.web.util.matcher.RequestMatcher;
20+
import org.springframework.util.StringUtils;
21+
22+
import javax.servlet.http.*;
23+
import java.text.SimpleDateFormat;
24+
import java.util.*;
925

1026
@Configuration
11-
@EnableOAuth2Sso
27+
@EnableWebSecurity
1228
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
29+
private static final String SAVED_LOGIN_ORIGIN_URI = SecurityConfiguration.class.getName() + "_SAVED_ORIGIN";
30+
private final Logger log = LoggerFactory.getLogger(SecurityConfiguration.class);
1331

1432
@Override
1533
public void configure(WebSecurity web) throws Exception {
@@ -20,6 +38,8 @@ public void configure(WebSecurity web) throws Exception {
2038
protected void configure(HttpSecurity http) throws Exception {
2139
http
2240
.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
41+
.and()
42+
.requestCache().requestCache(refererRequestCache())
2343
.and()
2444
.authorizeRequests()
2545
.antMatchers("/", "/api/user").permitAll()
@@ -29,4 +49,136 @@ protected void configure(HttpSecurity http) throws Exception {
2949
.requestMatchers(r -> r.getHeader("x-forwarded-proto") != null)
3050
.requiresSecure();*/
3151
}
52+
53+
@Bean
54+
@Profile("dev")
55+
public RequestCache refererRequestCache() {
56+
return new RequestCache() {
57+
private RequestMatcher requestMatcher = AnyRequestMatcher.INSTANCE;
58+
private PortResolver portResolver = new PortResolverImpl();
59+
60+
@Override
61+
public void saveRequest(HttpServletRequest request, HttpServletResponse response) {
62+
if (request.getRemoteUser() == null && this.requestMatcher.matches(request)) {
63+
String referrer = request.getHeader("referer");
64+
if (!StringUtils.isEmpty(referrer) &&
65+
request.getSession().getAttribute(SAVED_LOGIN_ORIGIN_URI) == null) {
66+
log.info("Saving login origin URI: {}", referrer);
67+
SavedRequest savedRequest = referrerRequest(referrer);
68+
request.getSession().setAttribute(SAVED_LOGIN_ORIGIN_URI, savedRequest);
69+
}
70+
} else {
71+
log.debug("Request not saved as configured RequestMatcher did not match");
72+
}
73+
}
74+
75+
@Override
76+
public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse response) {
77+
HttpSession session = request.getSession(false);
78+
79+
if (session != null) {
80+
return (SavedRequest) session.getAttribute(SAVED_LOGIN_ORIGIN_URI);
81+
}
82+
83+
return null;
84+
}
85+
86+
@Override
87+
public HttpServletRequest getMatchingRequest(HttpServletRequest request, HttpServletResponse response) {
88+
DefaultSavedRequest saved = (DefaultSavedRequest) getRequest(request, response);
89+
90+
if (saved == null) {
91+
return null;
92+
}
93+
94+
if (!saved.doesRequestMatch(request, portResolver)) {
95+
log.debug("saved request doesn't match");
96+
return null;
97+
}
98+
99+
removeRequest(request, response);
100+
101+
return new SavedRequestAwareWrapper(saved, request);
102+
}
103+
104+
@Override
105+
public void removeRequest(HttpServletRequest request, HttpServletResponse response) {
106+
HttpSession session = request.getSession(false);
107+
108+
if (session != null) {
109+
log.debug("Removing SavedRequest from session if present");
110+
session.removeAttribute(SAVED_LOGIN_ORIGIN_URI);
111+
}
112+
}
113+
};
114+
}
115+
116+
private static final TimeZone GMT_ZONE = TimeZone.getTimeZone("GMT");
117+
private SavedRequest savedRequest = null;
118+
/**
119+
* The set of SimpleDateFormat formats to use in getDateHeader(). Notice that because
120+
* SimpleDateFormat is not thread-safe, we can't declare formats[] as a static
121+
* variable.
122+
*/
123+
protected final SimpleDateFormat[] formats = new SimpleDateFormat[3];
124+
125+
class SavedRequestAwareWrapper extends HttpServletRequestWrapper {
126+
127+
SavedRequestAwareWrapper(SavedRequest saved, HttpServletRequest request){
128+
super(request);
129+
savedRequest = saved;
130+
131+
formats[0] = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
132+
formats[1] = new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US);
133+
formats[2] = new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US);
134+
135+
formats[0].setTimeZone(GMT_ZONE);
136+
formats[1].setTimeZone(GMT_ZONE);
137+
formats[2].setTimeZone(GMT_ZONE);
138+
}
139+
}
140+
141+
private SavedRequest referrerRequest(final String referrer) {
142+
return new SavedRequest() {
143+
@Override
144+
public String getRedirectUrl() {
145+
return referrer;
146+
}
147+
148+
@Override
149+
public List<Cookie> getCookies() {
150+
return null;
151+
}
152+
153+
@Override
154+
public String getMethod() {
155+
return null;
156+
}
157+
158+
@Override
159+
public List<String> getHeaderValues(String name) {
160+
return null;
161+
}
162+
163+
@Override
164+
public Collection<String> getHeaderNames() {
165+
return null;
166+
}
167+
168+
@Override
169+
public List<Locale> getLocales() {
170+
return null;
171+
}
172+
173+
@Override
174+
public String[] getParameterValues(String name) {
175+
return new String[0];
176+
}
177+
178+
@Override
179+
public Map<String, String[]> getParameterMap() {
180+
return null;
181+
}
182+
};
183+
}
32184
}

0 commit comments

Comments
 (0)