From ec6e0cf7890c9658f3994588a16c4261c6172b9f Mon Sep 17 00:00:00 2001 From: Cicio Flaviu Date: Mon, 15 Jun 2020 10:06:37 +0300 Subject: [PATCH] BAEL-4018 Improved login redirect classes. --- .../baeldung/loginredirect/LoginPageFilter.java | 16 ++++++++++------ .../loginredirect/LoginPageInterceptor.java | 4 ++-- .../loginredirect/LoginRedirectApplication.java | 3 ++- .../baeldung/loginredirect/UsersController.java | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageFilter.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageFilter.java index 635f811e7a..3c6d076756 100644 --- a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageFilter.java +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageFilter.java @@ -18,20 +18,24 @@ class LoginPageFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - if (isAuthenticated() && ((HttpServletRequest) request).getRequestURI().equals("/loginUser")) { + HttpServletRequest servletRequest = (HttpServletRequest) request; + HttpServletResponse servletResponse = (HttpServletResponse) response; + + if (isAuthenticated() && "/loginUser".equals(servletRequest.getRequestURI())) { String encodedRedirectURL = ((HttpServletResponse) response).encodeRedirectURL( - ((HttpServletRequest) request).getContextPath() + "/userMainPage"); + servletRequest.getContextPath() + "/userMainPage"); - ((HttpServletResponse) response).setStatus(HttpStatus.SC_TEMPORARY_REDIRECT); - ((HttpServletResponse) response).setHeader("Location", encodedRedirectURL); + servletResponse.setStatus(HttpStatus.SC_TEMPORARY_REDIRECT); + servletResponse.setHeader("Location", encodedRedirectURL); } - chain.doFilter(request, response); + + chain.doFilter(servletRequest, servletResponse); } private boolean isAuthenticated() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication == null || authentication instanceof AnonymousAuthenticationToken) { + if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { return false; } return authentication.isAuthenticated(); diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageInterceptor.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageInterceptor.java index f8fbe76b61..aa93201f37 100644 --- a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageInterceptor.java +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginPageInterceptor.java @@ -16,7 +16,7 @@ class LoginPageInterceptor extends HandlerInterceptorAdapter { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { UrlPathHelper urlPathHelper = new UrlPathHelper(); - if (urlPathHelper.getLookupPathForRequest(request).equals("/loginUser") && isAuthenticated()) { + if ("/loginUser".equals(urlPathHelper.getLookupPathForRequest(request)) && isAuthenticated()) { String encodedRedirectURL = response.encodeRedirectURL( request.getContextPath() + "/userMainPage"); @@ -31,7 +31,7 @@ class LoginPageInterceptor extends HandlerInterceptorAdapter { private boolean isAuthenticated() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication == null || authentication instanceof AnonymousAuthenticationToken) { + if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { return false; } return authentication.isAuthenticated(); diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectApplication.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectApplication.java index b7f9863775..1e44240449 100644 --- a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectApplication.java +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/LoginRedirectApplication.java @@ -2,9 +2,10 @@ package com.baeldung.loginredirect; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ImportResource; @SpringBootApplication -//@ImportResource({"classpath*:spring-security-login-redirect.xml"}) +@ImportResource({"classpath*:spring-security-login-redirect.xml"}) class LoginRedirectApplication { public static void main(String[] args) { SpringApplication.run(LoginRedirectApplication.class, args); diff --git a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/UsersController.java b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/UsersController.java index cfe87d9c21..308495d2ab 100644 --- a/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/UsersController.java +++ b/spring-security-modules/spring-security-mvc-boot-2/src/main/java/com/baeldung/loginredirect/UsersController.java @@ -24,7 +24,7 @@ class UsersController { private boolean isAuthenticated() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication == null || authentication instanceof AnonymousAuthenticationToken) { + if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { return false; } return authentication.isAuthenticated();