BAEL-4018 Improved login redirect classes.

This commit is contained in:
Cicio Flaviu 2020-06-15 10:06:37 +03:00
parent 84d820a594
commit ec6e0cf789
4 changed files with 15 additions and 10 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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);

View File

@ -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();