From 984336923afe5925a12cffd6233fcaf802ad2468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Mon, 16 Sep 2019 10:23:44 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E3=80=82=20=E7=99=BB=E9=99=86=E5=90=8E=E5=9B=9E?= =?UTF-8?q?=E6=8E=89request=20response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PigxAuthenticationFailureEvenHandler.java | 7 ++++++- .../PigxAuthenticationSuccessEventHandler.java | 7 ++++++- ...bstractAuthenticationFailureEvenHandler.java | 17 +++++++++++++++-- ...stractAuthenticationSuccessEventHandler.java | 16 ++++++++++++++-- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEvenHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEvenHandler.java index 73fd877c4..286e2ae3a 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEvenHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEvenHandler.java @@ -23,6 +23,9 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.stereotype.Component; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + /** * @author lengleng * @date 2018/10/8 @@ -37,9 +40,11 @@ public class PigxAuthenticationFailureEvenHandler extends AbstractAuthentication * * @param authenticationException 登录的authentication 对象 * @param authentication 登录的authenticationException 对象 + * @param request 请求 + * @param response 响应 */ @Override - public void handle(AuthenticationException authenticationException, Authentication authentication) { + public void handle(AuthenticationException authenticationException, Authentication authentication, HttpServletRequest request, HttpServletResponse response) { log.info("用户:{} 登录失败,异常:{}", authentication.getPrincipal(), authenticationException.getLocalizedMessage()); } } diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java index b0d5e49c2..0dd045565 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java @@ -22,6 +22,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Component; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + /** * @author lengleng * @date 2018/10/8 @@ -36,9 +39,11 @@ public class PigxAuthenticationSuccessEventHandler extends AbstractAuthenticatio * 获取到登录的authentication 对象 * * @param authentication 登录对象 + * @param request 请求 + * @param response 返回 */ @Override - public void handle(Authentication authentication) { + public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response) { log.info("用户:{} 登录成功", authentication.getPrincipal()); } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationFailureEvenHandler.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationFailureEvenHandler.java index b54d5199c..c29cc1c59 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationFailureEvenHandler.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationFailureEvenHandler.java @@ -21,6 +21,11 @@ import org.springframework.context.ApplicationListener; import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * @author lengleng @@ -36,10 +41,15 @@ public abstract class AbstractAuthenticationFailureEvenHandler implements Applic */ @Override public void onApplicationEvent(AbstractAuthenticationFailureEvent event) { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) + RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = requestAttributes.getRequest(); + HttpServletResponse response = requestAttributes.getResponse(); + AuthenticationException authenticationException = event.getException(); Authentication authentication = (Authentication) event.getSource(); - handle(authenticationException, authentication); + handle(authenticationException, authentication, request, response); } /** @@ -48,6 +58,9 @@ public abstract class AbstractAuthenticationFailureEvenHandler implements Applic * * @param authenticationException 登录的authentication 对象 * @param authentication 登录的authenticationException 对象 + * @param request 请求 + * @param response 响应 */ - public abstract void handle(AuthenticationException authenticationException, Authentication authentication); + public abstract void handle(AuthenticationException authenticationException, Authentication authentication + , HttpServletRequest request, HttpServletResponse response); } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationSuccessEventHandler.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationSuccessEventHandler.java index 8a0293f90..fb69571f2 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationSuccessEventHandler.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationSuccessEventHandler.java @@ -21,6 +21,11 @@ import cn.hutool.core.collection.CollUtil; import org.springframework.context.ApplicationListener; import org.springframework.security.authentication.event.AuthenticationSuccessEvent; import org.springframework.security.core.Authentication; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * @author lengleng @@ -35,9 +40,14 @@ public abstract class AbstractAuthenticationSuccessEventHandler implements Appli */ @Override public void onApplicationEvent(AuthenticationSuccessEvent event) { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) + RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = requestAttributes.getRequest(); + HttpServletResponse response = requestAttributes.getResponse(); + Authentication authentication = (Authentication) event.getSource(); if (CollUtil.isNotEmpty(authentication.getAuthorities())) { - handle(authentication); + handle(authentication, request, response); } } @@ -47,6 +57,8 @@ public abstract class AbstractAuthenticationSuccessEventHandler implements Appli * 获取到登录的authentication 对象 * * @param authentication 登录对象 + * @param request 请求 + * @param response 响应 */ - public abstract void handle(Authentication authentication); + public abstract void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response); }