♻️ 重构代码。 登陆后回掉request response

This commit is contained in:
冷冷 committed 2019-09-16 10:23:44 +08:00
1 parent 17b05dac26
commit 984336923a
4 files changed
+41 -6

No files matched your search

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