Skip to content

Commit

Permalink
Fixes how the session interceptor is created and adds redirect url
Browse files Browse the repository at this point in the history
configuration.
  • Loading branch information
bseeger committed May 24, 2024
1 parent c6a1c31 commit 544c196
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package formflow.library.config;

import formflow.library.interceptors.SessionContinuityInterceptor;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
Expand All @@ -17,15 +20,18 @@ public class SessionContinuityInterceptorConfiguration implements WebMvcConfigur

@Autowired
List<FlowConfiguration> flowConfigurations;



@Value("${form-flow.session-continuity-interceptor.redirect-url:/}")
private String redirectUrl;

/**
* Adds the SessionContinuityInterceptor to the Interceptor registry.
*
* @param registry the Interceptor registry.
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SessionContinuityInterceptor(flowConfigurations))
registry.addInterceptor(new SessionContinuityInterceptor(flowConfigurations, redirectUrl))
.addPathPatterns(List.of(SessionContinuityInterceptor.FLOW_PATH_FORMAT,
SessionContinuityInterceptor.NAVIGATION_FLOW_PATH_FORMAT));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,20 @@
import org.springframework.web.servlet.HandlerInterceptor;

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

public static final String FLOW_PATH_FORMAT = ScreenController.FLOW + "/" + ScreenController.FLOW_SCREEN_PATH;
public static final String NAVIGATION_FLOW_PATH_FORMAT = FLOW_PATH_FORMAT + "/navigation";

private final String REDIRECT_URL = "/?sessionBad=true";

private final String redirectUrl;
public List<FlowConfiguration> flowConfigurations;

public SessionContinuityInterceptor(List<FlowConfiguration> flowConfigurations) {
public SessionContinuityInterceptor(List<FlowConfiguration> flowConfigurations, String redirectUrl) {
this.flowConfigurations = flowConfigurations;
this.redirectUrl = redirectUrl;
}

/**
Expand Down Expand Up @@ -73,7 +71,7 @@ public boolean preHandle(HttpServletRequest request, @NotNull HttpServletRespons
return true;
}
log.error("No active session found for request to {}. Redirecting to landing page.", request.getRequestURI());
response.sendRedirect(REDIRECT_URL);
response.sendRedirect(redirectUrl);
return false;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import formflow.library.config.FormFlowConfigurationProperties;
import formflow.library.config.FlowConfiguration;

import java.util.List;

import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
Expand All @@ -15,34 +18,37 @@
@TestConfiguration
public class SpyInterceptorConfig implements WebMvcConfigurer {

@Autowired
private List<FlowConfiguration> flowConfigurations;

@Autowired
private FormFlowConfigurationProperties formFlowConfigurationProperties;

@Bean
@Primary
public LocaleChangeInterceptor localeChangeInterceptor() {
return Mockito.spy(new LocaleChangeInterceptor());
}

@Bean
@Primary // Ensure this bean takes precedence over the real one
public SessionContinuityInterceptor dataRequiredInterceptor() {
return Mockito.spy(new SessionContinuityInterceptor(flowConfigurations));
}

@Bean
@Primary
public DisabledFlowInterceptor disabledFlowInterceptor() {
return Mockito.spy(new DisabledFlowInterceptor(formFlowConfigurationProperties));
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
registry.addInterceptor(dataRequiredInterceptor());
registry.addInterceptor(disabledFlowInterceptor());
}
@Autowired
private List<FlowConfiguration> flowConfigurations;

@Autowired
private FormFlowConfigurationProperties formFlowConfigurationProperties;

@Value("${form-flow.session-continuity-interceptor.redirect-url:/}")
private String redirectUrl;

@Bean
@Primary
public LocaleChangeInterceptor localeChangeInterceptor() {
return Mockito.spy(new LocaleChangeInterceptor());
}

@Bean
@Primary // Ensure this bean takes precedence over the real one
public SessionContinuityInterceptor dataRequiredInterceptor() {
return Mockito.spy(new SessionContinuityInterceptor(flowConfigurations, redirectUrl));
}

@Bean
@Primary
public DisabledFlowInterceptor disabledFlowInterceptor() {
return Mockito.spy(new DisabledFlowInterceptor(formFlowConfigurationProperties));
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
registry.addInterceptor(dataRequiredInterceptor());
registry.addInterceptor(disabledFlowInterceptor());
}
}

0 comments on commit 544c196

Please sign in to comment.