Skip to content

Commit 080c31d

Browse files
committed
Fix #25724: show error message when session expired during login
1 parent cb55cc8 commit 080c31d

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/security/WebSecurityConfig.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
3030
import org.springframework.core.env.Environment;
31+
import org.springframework.http.HttpStatus;
32+
import org.springframework.security.access.AccessDeniedException;
3133
import org.springframework.security.authentication.AuthenticationEventPublisher;
3234
import org.springframework.security.authentication.AuthenticationManager;
3335
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
@@ -37,11 +39,20 @@
3739
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3840
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
3941
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
42+
import org.springframework.security.web.access.AccessDeniedHandler;
43+
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
4044
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
45+
import org.springframework.security.web.csrf.InvalidCsrfTokenException;
46+
import org.springframework.security.web.csrf.MissingCsrfTokenException;
4147
import org.springframework.security.web.header.writers.StaticHeadersWriter;
4248
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
49+
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
4350

4451
import javax.inject.Inject;
52+
import javax.servlet.ServletException;
53+
import javax.servlet.http.HttpServletRequest;
54+
import javax.servlet.http.HttpServletResponse;
55+
import java.io.IOException;
4556
import java.util.List;
4657

4758
@Configuration
@@ -96,7 +107,20 @@ protected void configure(HttpSecurity http) throws Exception {
96107

97108
// Perform CSRF check on the login form
98109
http.csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("/login", "POST"));
99-
110+
111+
http.exceptionHandling().accessDeniedHandler(new AccessDeniedHandler() {
112+
final AntPathRequestMatcher matcher = new AntPathRequestMatcher("/login", "POST");
113+
final AccessDeniedHandler defaultAccessDeniedHandler = new AccessDeniedHandlerImpl();
114+
@Override
115+
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
116+
if (matcher.matcher(request).isMatch() && accessDeniedException instanceof MissingCsrfTokenException) {
117+
response.sendRedirect(ServletUriComponentsBuilder.fromCurrentContextPath().path("/login").queryParam("error", "expired").build().toUriString());
118+
} else {
119+
defaultAccessDeniedHandler.handle(request, response, accessDeniedException);
120+
}
121+
}
122+
});
123+
100124
// Always set header: X-Content-Type-Options=nosniff
101125
http.headers().contentTypeOptions();
102126

src/main/java/eu/openanalytics/containerproxy/ui/AuthController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ public class AuthController extends BaseController {
4949
@RequestMapping(value = "/login", method = RequestMethod.GET)
5050
public Object getLoginPage(@RequestParam Optional<String> error, ModelMap map) {
5151
prepareMap(map);
52-
if (error.isPresent()) map.put("error", "Invalid user name or password");
52+
if (error.isPresent()) {
53+
if (error.get().equals("expired")) {
54+
map.put("error", "You took too long to login, please try again");
55+
} else {
56+
map.put("error", "Invalid user name or password");
57+
}
58+
}
5359

5460
if (auth instanceof OpenIDAuthenticationBackend) {
5561
return new RedirectView(((OpenIDAuthenticationBackend) auth).getLoginRedirectURI());

0 commit comments

Comments
 (0)