Skip to content

Commit 903e50b

Browse files
committed
Fixes how the session interceptor is created and adds redirect url
configuration.
1 parent 42025e4 commit 903e50b

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

src/main/java/formflow/library/config/SessionContinuityInterceptorConfiguration.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package formflow.library.config;
22

33
import formflow.library.interceptors.SessionContinuityInterceptor;
4+
45
import java.util.List;
6+
57
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Value;
69
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
710
import org.springframework.context.annotation.Configuration;
811
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@@ -17,15 +20,18 @@ public class SessionContinuityInterceptorConfiguration implements WebMvcConfigur
1720

1821
@Autowired
1922
List<FlowConfiguration> flowConfigurations;
20-
21-
23+
24+
@Value("${form-flow.session-continuity-interceptor.redirect-url:/}")
25+
private String redirectUrl;
26+
2227
/**
2328
* Adds the SessionContinuityInterceptor to the Interceptor registry.
29+
*
2430
* @param registry the Interceptor registry.
2531
*/
2632
@Override
2733
public void addInterceptors(InterceptorRegistry registry) {
28-
registry.addInterceptor(new SessionContinuityInterceptor(flowConfigurations))
34+
registry.addInterceptor(new SessionContinuityInterceptor(flowConfigurations, redirectUrl))
2935
.addPathPatterns(List.of(SessionContinuityInterceptor.FLOW_PATH_FORMAT,
3036
SessionContinuityInterceptor.NAVIGATION_FLOW_PATH_FORMAT));
3137
}

src/main/java/formflow/library/interceptors/SessionContinuityInterceptor.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,20 @@
2222
import org.springframework.web.servlet.HandlerInterceptor;
2323

2424
/**
25-
* This interceptor prevents users from jumping to random pages in a flow.
25+
* This interceptor prevents users with an invalid session from jumping to random pages in a flow.
2626
*/
27-
@Component
2827
@Slf4j
2928
@ConditionalOnProperty(name = "form-flow.session-continuity-interceptor.enabled", havingValue = "true")
3029
public class SessionContinuityInterceptor implements HandlerInterceptor, Ordered {
3130

3231
public static final String FLOW_PATH_FORMAT = ScreenController.FLOW + "/" + ScreenController.FLOW_SCREEN_PATH;
3332
public static final String NAVIGATION_FLOW_PATH_FORMAT = FLOW_PATH_FORMAT + "/navigation";
34-
35-
private final String REDIRECT_URL = "/?sessionBad=true";
36-
33+
private final String redirectUrl;
3734
public List<FlowConfiguration> flowConfigurations;
3835

39-
public SessionContinuityInterceptor(List<FlowConfiguration> flowConfigurations) {
36+
public SessionContinuityInterceptor(List<FlowConfiguration> flowConfigurations, String redirectUrl) {
4037
this.flowConfigurations = flowConfigurations;
38+
this.redirectUrl = redirectUrl;
4139
}
4240

4341
/**
@@ -73,7 +71,7 @@ public boolean preHandle(HttpServletRequest request, @NotNull HttpServletRespons
7371
return true;
7472
}
7573
log.error("No active session found for request to {}. Redirecting to landing page.", request.getRequestURI());
76-
response.sendRedirect(REDIRECT_URL);
74+
response.sendRedirect(redirectUrl);
7775
return false;
7876
}
7977

@@ -87,7 +85,7 @@ public boolean preHandle(HttpServletRequest request, @NotNull HttpServletRespons
8785
if (submissionId == null && !parsedUrl.get("screen").equals(firstScreen)) {
8886
log.error("A submission ID was not found in the session for request to {}. Redirecting to landing page.",
8987
request.getRequestURI());
90-
response.sendRedirect(REDIRECT_URL);
88+
response.sendRedirect(redirectUrl);
9189
return false;
9290
}
9391

src/test/java/formflow/library/interceptors/SpyInterceptorConfig.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import formflow.library.config.FormFlowConfigurationProperties;
44
import formflow.library.config.FlowConfiguration;
5+
56
import java.util.List;
7+
68
import org.mockito.Mockito;
79
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.beans.factory.annotation.Value;
811
import org.springframework.boot.test.context.TestConfiguration;
912
import org.springframework.context.annotation.Bean;
1013
import org.springframework.context.annotation.Primary;
@@ -15,34 +18,37 @@
1518
@TestConfiguration
1619
public class SpyInterceptorConfig implements WebMvcConfigurer {
1720

18-
@Autowired
19-
private List<FlowConfiguration> flowConfigurations;
20-
21-
@Autowired
22-
private FormFlowConfigurationProperties formFlowConfigurationProperties;
23-
24-
@Bean
25-
@Primary
26-
public LocaleChangeInterceptor localeChangeInterceptor() {
27-
return Mockito.spy(new LocaleChangeInterceptor());
28-
}
29-
30-
@Bean
31-
@Primary // Ensure this bean takes precedence over the real one
32-
public SessionContinuityInterceptor dataRequiredInterceptor() {
33-
return Mockito.spy(new SessionContinuityInterceptor(flowConfigurations));
34-
}
35-
36-
@Bean
37-
@Primary
38-
public DisabledFlowInterceptor disabledFlowInterceptor() {
39-
return Mockito.spy(new DisabledFlowInterceptor(formFlowConfigurationProperties));
40-
}
41-
42-
@Override
43-
public void addInterceptors(InterceptorRegistry registry) {
44-
registry.addInterceptor(localeChangeInterceptor());
45-
registry.addInterceptor(dataRequiredInterceptor());
46-
registry.addInterceptor(disabledFlowInterceptor());
47-
}
21+
@Autowired
22+
private List<FlowConfiguration> flowConfigurations;
23+
24+
@Autowired
25+
private FormFlowConfigurationProperties formFlowConfigurationProperties;
26+
27+
@Value("${form-flow.session-continuity-interceptor.redirect-url:/}")
28+
private String redirectUrl;
29+
30+
@Bean
31+
@Primary
32+
public LocaleChangeInterceptor localeChangeInterceptor() {
33+
return Mockito.spy(new LocaleChangeInterceptor());
34+
}
35+
36+
@Bean
37+
@Primary // Ensure this bean takes precedence over the real one
38+
public SessionContinuityInterceptor dataRequiredInterceptor() {
39+
return Mockito.spy(new SessionContinuityInterceptor(flowConfigurations, redirectUrl));
40+
}
41+
42+
@Bean
43+
@Primary
44+
public DisabledFlowInterceptor disabledFlowInterceptor() {
45+
return Mockito.spy(new DisabledFlowInterceptor(formFlowConfigurationProperties));
46+
}
47+
48+
@Override
49+
public void addInterceptors(InterceptorRegistry registry) {
50+
registry.addInterceptor(localeChangeInterceptor());
51+
registry.addInterceptor(dataRequiredInterceptor());
52+
registry.addInterceptor(disabledFlowInterceptor());
53+
}
4854
}

0 commit comments

Comments
 (0)