BAEL-4018 Improved login redirect classes.
This commit is contained in:
parent
84d820a594
commit
ec6e0cf789
@ -18,20 +18,24 @@ class LoginPageFilter extends GenericFilterBean {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
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(
|
String encodedRedirectURL = ((HttpServletResponse) response).encodeRedirectURL(
|
||||||
((HttpServletRequest) request).getContextPath() + "/userMainPage");
|
servletRequest.getContextPath() + "/userMainPage");
|
||||||
|
|
||||||
((HttpServletResponse) response).setStatus(HttpStatus.SC_TEMPORARY_REDIRECT);
|
servletResponse.setStatus(HttpStatus.SC_TEMPORARY_REDIRECT);
|
||||||
((HttpServletResponse) response).setHeader("Location", encodedRedirectURL);
|
servletResponse.setHeader("Location", encodedRedirectURL);
|
||||||
}
|
}
|
||||||
chain.doFilter(request, response);
|
|
||||||
|
chain.doFilter(servletRequest, servletResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAuthenticated() {
|
private boolean isAuthenticated() {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
|
if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return authentication.isAuthenticated();
|
return authentication.isAuthenticated();
|
||||||
|
@ -16,7 +16,7 @@ class LoginPageInterceptor extends HandlerInterceptorAdapter {
|
|||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||||
|
|
||||||
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
UrlPathHelper urlPathHelper = new UrlPathHelper();
|
||||||
if (urlPathHelper.getLookupPathForRequest(request).equals("/loginUser") && isAuthenticated()) {
|
if ("/loginUser".equals(urlPathHelper.getLookupPathForRequest(request)) && isAuthenticated()) {
|
||||||
|
|
||||||
String encodedRedirectURL = response.encodeRedirectURL(
|
String encodedRedirectURL = response.encodeRedirectURL(
|
||||||
request.getContextPath() + "/userMainPage");
|
request.getContextPath() + "/userMainPage");
|
||||||
@ -31,7 +31,7 @@ class LoginPageInterceptor extends HandlerInterceptorAdapter {
|
|||||||
|
|
||||||
private boolean isAuthenticated() {
|
private boolean isAuthenticated() {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
|
if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return authentication.isAuthenticated();
|
return authentication.isAuthenticated();
|
||||||
|
@ -2,9 +2,10 @@ package com.baeldung.loginredirect;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.ImportResource;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
//@ImportResource({"classpath*:spring-security-login-redirect.xml"})
|
@ImportResource({"classpath*:spring-security-login-redirect.xml"})
|
||||||
class LoginRedirectApplication {
|
class LoginRedirectApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(LoginRedirectApplication.class, args);
|
SpringApplication.run(LoginRedirectApplication.class, args);
|
||||||
|
@ -24,7 +24,7 @@ class UsersController {
|
|||||||
|
|
||||||
private boolean isAuthenticated() {
|
private boolean isAuthenticated() {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
|
if (authentication == null || AnonymousAuthenticationToken.class.isAssignableFrom(authentication.getClass())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return authentication.isAuthenticated();
|
return authentication.isAuthenticated();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user