|
@@ -1,7 +1,9 @@
|
|
|
package com.muchinfo.mtp3century.filter;
|
|
package com.muchinfo.mtp3century.filter;
|
|
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.muchinfo.mtp3century.utils.CenturyDecryptUtil;
|
|
import com.muchinfo.mtp3century.utils.CenturyDecryptUtil;
|
|
|
import com.muchinfo.mtp3century.utils.RedisServiceUtil;
|
|
import com.muchinfo.mtp3century.utils.RedisServiceUtil;
|
|
|
|
|
+import com.muchinfo.mtp3century.utils.SpringBeanUtils;
|
|
|
import com.muchinfo.mtp3common.enumtype.Constants;
|
|
import com.muchinfo.mtp3common.enumtype.Constants;
|
|
|
import com.muchinfo.mtp3common.enumtype.MessageType;
|
|
import com.muchinfo.mtp3common.enumtype.MessageType;
|
|
|
import com.muchinfo.mtp3common.enumtype.SysConstants;
|
|
import com.muchinfo.mtp3common.enumtype.SysConstants;
|
|
@@ -20,7 +22,6 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
|
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
|
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.FilterChain;
|
|
import javax.servlet.FilterChain;
|
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -35,12 +36,6 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
|
|
|
|
|
|
|
|
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
|
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
|
|
|
|
|
|
|
- @Resource
|
|
|
|
|
- private RedisServiceUtil redisServiceUtil;
|
|
|
|
|
-
|
|
|
|
|
- @Resource
|
|
|
|
|
- private ISystemmanagerMapper iSystemmanagerMapper;
|
|
|
|
|
-
|
|
|
|
|
public JwtLoginFilter(String defaultFilterProcessesUrl, AuthenticationManager authenticationManager) {
|
|
public JwtLoginFilter(String defaultFilterProcessesUrl, AuthenticationManager authenticationManager) {
|
|
|
super(new AntPathRequestMatcher(defaultFilterProcessesUrl));
|
|
super(new AntPathRequestMatcher(defaultFilterProcessesUrl));
|
|
|
setAuthenticationManager(authenticationManager);
|
|
setAuthenticationManager(authenticationManager);
|
|
@@ -112,45 +107,51 @@ public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {
|
|
|
* @throws ServletException
|
|
* @throws ServletException
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
- protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
|
|
|
|
|
- //清理上下文
|
|
|
|
|
- SecurityContextHolder.clearContext();
|
|
|
|
|
- String loginPWDFail_msg = null;
|
|
|
|
|
|
|
+ protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) {
|
|
|
|
|
+ Object cacheObject = SpringBeanUtils.getBean(RedisServiceUtil.class).getCacheObject("user-login-fail");
|
|
|
log.error("AuthenticationException", failed);
|
|
log.error("AuthenticationException", failed);
|
|
|
|
|
+ String[] param = new String [4];
|
|
|
//失败超过默认次数锁定用户
|
|
//失败超过默认次数锁定用户
|
|
|
- Systemmanager sysUser = RequestUtils.read(request, Systemmanager.class);
|
|
|
|
|
|
|
+ Systemmanager sysUser = SpringBeanUtils.getBean(ISystemmanagerMapper.class).selectOne(new QueryWrapper<Systemmanager>().eq("LOGINCODE", cacheObject));
|
|
|
if (Objects.nonNull(sysUser)) {
|
|
if (Objects.nonNull(sysUser)) {
|
|
|
sysUser.setLoginfailnum(sysUser.getLoginfailnum() + 1);
|
|
sysUser.setLoginfailnum(sysUser.getLoginfailnum() + 1);
|
|
|
- Object redisParam = redisServiceUtil.getCacheMapValue(Constants.REDIS_HEADER_SYSPARAM,
|
|
|
|
|
|
|
+ Object redisParam = SpringBeanUtils.getBean(RedisServiceUtil.class).getCacheMapValue(Constants.REDIS_HEADER_SYSPARAM,
|
|
|
SysConstants.SYSPARAM_CODE_PWERR_TIMES);
|
|
SysConstants.SYSPARAM_CODE_PWERR_TIMES);
|
|
|
- Object errorCountTime = redisServiceUtil.getCacheMapValue(Constants.REDIS_HEADER_SYSPARAM,
|
|
|
|
|
|
|
+ Object errorCountTime = SpringBeanUtils.getBean(RedisServiceUtil.class).getCacheMapValue(Constants.REDIS_HEADER_SYSPARAM,
|
|
|
SysConstants.SYSPARAM_CODE_LOCKTIME);
|
|
SysConstants.SYSPARAM_CODE_LOCKTIME);
|
|
|
double defaultMin = errorCountTime == null ? 180d : Double.parseDouble(String.valueOf(errorCountTime));
|
|
double defaultMin = errorCountTime == null ? 180d : Double.parseDouble(String.valueOf(errorCountTime));
|
|
|
int errorNum = redisParam == null ? 3 : Integer.parseInt(String.valueOf(redisParam));
|
|
int errorNum = redisParam == null ? 3 : Integer.parseInt(String.valueOf(redisParam));
|
|
|
//设置了= 0不限制 错误次数
|
|
//设置了= 0不限制 错误次数
|
|
|
if (errorNum > 0) {
|
|
if (errorNum > 0) {
|
|
|
- loginPWDFail_msg = Constants.loginPWDFail_msg.
|
|
|
|
|
- replace("{#1}", String.valueOf(errorNum - sysUser.getLoginfailnum())).
|
|
|
|
|
- replace("{#2}", String.valueOf(errorNum)).
|
|
|
|
|
- replace("{#3}", String.valueOf(defaultMin));
|
|
|
|
|
|
|
+ param[0] = String.valueOf(errorNum - sysUser.getLoginfailnum());
|
|
|
|
|
+ param[1] = String.valueOf(errorNum);
|
|
|
|
|
+ param[2] = String.valueOf(defaultMin);
|
|
|
|
|
+ param[3] = ".";
|
|
|
// 失败3次锁定
|
|
// 失败3次锁定
|
|
|
if (sysUser.getLoginfailnum() >= errorNum) {
|
|
if (sysUser.getLoginfailnum() >= errorNum) {
|
|
|
sysUser.setLogintime(new Date());
|
|
sysUser.setLogintime(new Date());
|
|
|
sysUser.setManagerstatus(SysEnums.SysStatus.lock.getCode());
|
|
sysUser.setManagerstatus(SysEnums.SysStatus.lock.getCode());
|
|
|
- iSystemmanagerMapper.updateById(sysUser);
|
|
|
|
|
- loginPWDFail_msg = loginPWDFail_msg + "," + Constants.userlock_msg;
|
|
|
|
|
|
|
+ param[3] = "," + Constants.userlock_msg;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ SpringBeanUtils.getBean(ISystemmanagerMapper.class).updateById(sysUser);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ SpringBeanUtils.getBean(RedisServiceUtil.class).deleteByPrex("user-login-fail");
|
|
|
|
|
+ //清理上下文
|
|
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
//判断异常类
|
|
//判断异常类
|
|
|
if (failed instanceof InternalAuthenticationServiceException) {
|
|
if (failed instanceof InternalAuthenticationServiceException) {
|
|
|
ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM009.getCode(), request.getHeader("Accept-Language"));
|
|
ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM009.getCode(), request.getHeader("Accept-Language"));
|
|
|
} else if (failed instanceof UsernameNotFoundException) {
|
|
} else if (failed instanceof UsernameNotFoundException) {
|
|
|
ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM010.getCode(), request.getHeader("Accept-Language"));
|
|
ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM010.getCode(), request.getHeader("Accept-Language"));
|
|
|
} else if (failed instanceof BadCredentialsException) {
|
|
} else if (failed instanceof BadCredentialsException) {
|
|
|
- ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, loginPWDFail_msg == null ? MessageType.MESSAGE_ERROR_CODE_SYSTEM011.getCode() : loginPWDFail_msg, request.getHeader("Accept-Language"));
|
|
|
|
|
- } else if (failed instanceof LockedException) {
|
|
|
|
|
|
|
+ if(param[0] == null){
|
|
|
|
|
+ ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM011.getCode(), request.getHeader("Accept-Language"));
|
|
|
|
|
+ }else{
|
|
|
|
|
+ ResponseUtils.writeFiledForParameters(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, param, request.getHeader("Accept-Language"));
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (failed instanceof LockedException) {
|
|
|
ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM012.getCode(), request.getHeader("Accept-Language"));
|
|
ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM012.getCode(), request.getHeader("Accept-Language"));
|
|
|
} else if (failed instanceof CredentialsExpiredException) {
|
|
} else if (failed instanceof CredentialsExpiredException) {
|
|
|
ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM013.getCode(), request.getHeader("Accept-Language"));
|
|
ResponseUtils.writeFiled(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageType.MESSAGE_ERROR_CODE_SYSTEM013.getCode(), request.getHeader("Accept-Language"));
|