Commit 27d26c9ded4b452e7779e39da367a1d4c40eea63
1 parent
65c24b45
1.安全相关,登录异常提示、统一异常处理
2.维修保养任务分别在9点、12点整点时进行一次提示
Showing
3 changed files
with
131 additions
and
18 deletions
src/main/java/com/bsth/controller/sys/UserController.java
| @@ -85,6 +85,12 @@ public class UserController extends BaseController<SysUser, Integer> { | @@ -85,6 +85,12 @@ public class UserController extends BaseController<SysUser, Integer> { | ||
| 85 | 85 | ||
| 86 | SysUser user = sysUserService.findByUserName(userName); | 86 | SysUser user = sysUserService.findByUserName(userName); |
| 87 | 87 | ||
| 88 | + if (null == user) { | ||
| 89 | + userOrPasswordInvalid(rs, userName); | ||
| 90 | + | ||
| 91 | + return rs; | ||
| 92 | + } | ||
| 93 | + | ||
| 88 | // 校验用户状态 | 94 | // 校验用户状态 |
| 89 | if (!user.isEnabled()) { | 95 | if (!user.isEnabled()) { |
| 90 | return put(rs, "msg", "该用户已被锁定,请联系管理员"); | 96 | return put(rs, "msg", "该用户已被锁定,请联系管理员"); |
| @@ -108,19 +114,8 @@ public class UserController extends BaseController<SysUser, Integer> { | @@ -108,19 +114,8 @@ public class UserController extends BaseController<SysUser, Integer> { | ||
| 108 | 114 | ||
| 109 | // 校验密码 | 115 | // 校验密码 |
| 110 | boolean matchStatus = new BCryptPasswordEncoder(4).matches(password, user.getPassword()); | 116 | boolean matchStatus = new BCryptPasswordEncoder(4).matches(password, user.getPassword()); |
| 111 | - if (null == user || !matchStatus) { | ||
| 112 | - rs.put("msg", "密码有误"); | ||
| 113 | - | ||
| 114 | - Integer errTimes = USER_ERRTIMES.get(userName); | ||
| 115 | - if (null == errTimes) { | ||
| 116 | - errTimes = 0; | ||
| 117 | - } | ||
| 118 | - USER_ERRTIMES.put(userName, ++errTimes); | ||
| 119 | - if (errTimes > 3) { | ||
| 120 | - USER_LOCKTIME.put(userName, System.currentTimeMillis() + 600000); | ||
| 121 | - USER_ERRTIMES.put(userName, 0); | ||
| 122 | - put(rs, "msg", "密码错误4次,账户将被锁定10分钟"); | ||
| 123 | - } | 117 | + if (!matchStatus) { |
| 118 | + userOrPasswordInvalid(rs, userName); | ||
| 124 | 119 | ||
| 125 | return rs; | 120 | return rs; |
| 126 | } | 121 | } |
| @@ -151,6 +146,21 @@ public class UserController extends BaseController<SysUser, Integer> { | @@ -151,6 +146,21 @@ public class UserController extends BaseController<SysUser, Integer> { | ||
| 151 | return rs; | 146 | return rs; |
| 152 | } | 147 | } |
| 153 | 148 | ||
| 149 | + private void userOrPasswordInvalid(Map<String, Object> rs, String userName) { | ||
| 150 | + rs.put("msg", "密码有误"); | ||
| 151 | + | ||
| 152 | + Integer errTimes = USER_ERRTIMES.get(userName); | ||
| 153 | + if (null == errTimes) { | ||
| 154 | + errTimes = 0; | ||
| 155 | + } | ||
| 156 | + USER_ERRTIMES.put(userName, ++errTimes); | ||
| 157 | + if (errTimes > 3) { | ||
| 158 | + USER_LOCKTIME.put(userName, System.currentTimeMillis() + 600000); | ||
| 159 | + USER_ERRTIMES.put(userName, 0); | ||
| 160 | + put(rs, "msg", "密码错误4次,账户将被锁定10分钟"); | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + | ||
| 154 | @RequestMapping(value = "/change_user", method = RequestMethod.POST) | 164 | @RequestMapping(value = "/change_user", method = RequestMethod.POST) |
| 155 | public Map<String, Object> changeUser(HttpServletRequest request, @RequestParam String userName, | 165 | public Map<String, Object> changeUser(HttpServletRequest request, @RequestParam String userName, |
| 156 | @RequestParam String password) { | 166 | @RequestParam String password) { |
src/main/java/com/bsth/data/maintenance_plan/MtPlanCenter.java
| @@ -11,6 +11,8 @@ import org.springframework.beans.BeansException; | @@ -11,6 +11,8 @@ import org.springframework.beans.BeansException; | ||
| 11 | import org.springframework.beans.factory.annotation.Autowired; | 11 | import org.springframework.beans.factory.annotation.Autowired; |
| 12 | import org.springframework.context.ApplicationContext; | 12 | import org.springframework.context.ApplicationContext; |
| 13 | import org.springframework.context.ApplicationContextAware; | 13 | import org.springframework.context.ApplicationContextAware; |
| 14 | +import org.springframework.scheduling.annotation.EnableScheduling; | ||
| 15 | +import org.springframework.scheduling.annotation.Scheduled; | ||
| 14 | import org.springframework.stereotype.Component; | 16 | import org.springframework.stereotype.Component; |
| 15 | 17 | ||
| 16 | import java.util.HashMap; | 18 | import java.util.HashMap; |
| @@ -23,6 +25,7 @@ import java.util.Set; | @@ -23,6 +25,7 @@ import java.util.Set; | ||
| 23 | * @author Hill | 25 | * @author Hill |
| 24 | */ | 26 | */ |
| 25 | @Component | 27 | @Component |
| 28 | +@EnableScheduling | ||
| 26 | public class MtPlanCenter implements ApplicationContextAware { | 29 | public class MtPlanCenter implements ApplicationContextAware { |
| 27 | 30 | ||
| 28 | private static Set<MaintenancePlan> data; | 31 | private static Set<MaintenancePlan> data; |
| @@ -35,13 +38,13 @@ public class MtPlanCenter implements ApplicationContextAware { | @@ -35,13 +38,13 @@ public class MtPlanCenter implements ApplicationContextAware { | ||
| 35 | /** | 38 | /** |
| 36 | * 车辆自编号 和 最新一条数据对照 | 39 | * 车辆自编号 和 最新一条数据对照 |
| 37 | */ | 40 | */ |
| 38 | - private static Map<String, MaintenancePlan> safeMap; | 41 | + private static Map<String, MaintenancePlan> maintainMap; |
| 39 | 42 | ||
| 40 | private static Logger logger = LoggerFactory.getLogger(MtPlanCenter.class); | 43 | private static Logger logger = LoggerFactory.getLogger(MtPlanCenter.class); |
| 41 | 44 | ||
| 42 | static { | 45 | static { |
| 43 | data = new HashSet<>(); | 46 | data = new HashSet<>(); |
| 44 | - safeMap = new HashMap<>(); | 47 | + maintainMap = new HashMap<>(); |
| 45 | } | 48 | } |
| 46 | 49 | ||
| 47 | private static DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); | 50 | private static DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); |
| @@ -55,7 +58,7 @@ public class MtPlanCenter implements ApplicationContextAware { | @@ -55,7 +58,7 @@ public class MtPlanCenter implements ApplicationContextAware { | ||
| 55 | //通知客户端 | 58 | //通知客户端 |
| 56 | sendUtils.sendMaintenancePlan(maintenancePlan); | 59 | sendUtils.sendMaintenancePlan(maintenancePlan); |
| 57 | data.add(maintenancePlan); | 60 | data.add(maintenancePlan); |
| 58 | - safeMap.put(maintenancePlan.getZbh(), maintenancePlan); | 61 | + maintainMap.put(maintenancePlan.getZbh(), maintenancePlan); |
| 59 | } | 62 | } |
| 60 | } | 63 | } |
| 61 | 64 | ||
| @@ -65,8 +68,16 @@ public class MtPlanCenter implements ApplicationContextAware { | @@ -65,8 +68,16 @@ public class MtPlanCenter implements ApplicationContextAware { | ||
| 65 | 68 | ||
| 66 | public static void clear(){ | 69 | public static void clear(){ |
| 67 | data = new HashSet<>(); | 70 | data = new HashSet<>(); |
| 68 | - safeMap = new HashMap<>(); | ||
| 69 | - logger.info("清除安全驾驶数据,,,"); | 71 | + maintainMap = new HashMap<>(); |
| 72 | + logger.info("清除维修保养数据,,,"); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + @Scheduled(cron = "0 0 9,12 * * *") | ||
| 76 | + public void pushMtPlan() { | ||
| 77 | + for (MaintenancePlan maintenancePlan : data) { | ||
| 78 | + //通知客户端 | ||
| 79 | + sendUtils.sendMaintenancePlan(maintenancePlan); | ||
| 80 | + } | ||
| 70 | } | 81 | } |
| 71 | 82 | ||
| 72 | @Override | 83 | @Override |
src/main/java/com/bsth/exception/GlobalExceptionHandler.java
0 → 100644
| 1 | +package com.bsth.exception; | ||
| 2 | + | ||
| 3 | +import com.bsth.common.ResponseCode; | ||
| 4 | +import org.slf4j.Logger; | ||
| 5 | +import org.slf4j.LoggerFactory; | ||
| 6 | +import org.springframework.security.access.AccessDeniedException; | ||
| 7 | +import org.springframework.security.authentication.AccountExpiredException; | ||
| 8 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
| 9 | +import org.springframework.validation.BindException; | ||
| 10 | +import org.springframework.web.bind.MethodArgumentNotValidException; | ||
| 11 | +import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| 12 | +import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| 13 | +import org.springframework.web.servlet.NoHandlerFoundException; | ||
| 14 | + | ||
| 15 | +import java.util.HashMap; | ||
| 16 | +import java.util.Map; | ||
| 17 | + | ||
| 18 | +/** | ||
| 19 | + * 全局异常处理器 | ||
| 20 | + * | ||
| 21 | + * @author luwu | ||
| 22 | + */ | ||
| 23 | +@RestControllerAdvice | ||
| 24 | +public class GlobalExceptionHandler | ||
| 25 | +{ | ||
| 26 | + private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
| 27 | + | ||
| 28 | + @ExceptionHandler(NoHandlerFoundException.class) | ||
| 29 | + public Map<String, Object> handlerNoFoundException(Exception e) | ||
| 30 | + { | ||
| 31 | + log.error(e.getMessage(), e); | ||
| 32 | + return error("路径不存在,请检查路径是否正确"); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + @ExceptionHandler(AccessDeniedException.class) | ||
| 36 | + public Map<String, Object> handleAuthorizationException(AccessDeniedException e) | ||
| 37 | + { | ||
| 38 | + log.error(e.getMessage()); | ||
| 39 | + return error("没有权限,请联系管理员授权"); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + @ExceptionHandler(AccountExpiredException.class) | ||
| 43 | + public Map<String, Object> handleAccountExpiredException(AccountExpiredException e) | ||
| 44 | + { | ||
| 45 | + log.error(e.getMessage(), e); | ||
| 46 | + return error(e.getMessage()); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + @ExceptionHandler(UsernameNotFoundException.class) | ||
| 50 | + public Map<String, Object> handleUsernameNotFoundException(UsernameNotFoundException e) | ||
| 51 | + { | ||
| 52 | + log.error(e.getMessage(), e); | ||
| 53 | + return error(e.getMessage()); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + @ExceptionHandler(Exception.class) | ||
| 57 | + public Map<String, Object> handleException(Exception e) | ||
| 58 | + { | ||
| 59 | + log.error(e.getMessage(), e); | ||
| 60 | + return error(e.getMessage()); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * 自定义验证异常 | ||
| 65 | + */ | ||
| 66 | + @ExceptionHandler(BindException.class) | ||
| 67 | + public Map<String, Object> validatedBindException(BindException e) | ||
| 68 | + { | ||
| 69 | + log.error(e.getMessage(), e); | ||
| 70 | + String message = e.getAllErrors().get(0).getDefaultMessage(); | ||
| 71 | + return error(message); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + /** | ||
| 75 | + * 自定义验证异常 | ||
| 76 | + */ | ||
| 77 | + @ExceptionHandler(MethodArgumentNotValidException.class) | ||
| 78 | + public Object validExceptionHandler(MethodArgumentNotValidException e) | ||
| 79 | + { | ||
| 80 | + log.error(e.getMessage(), e); | ||
| 81 | + String message = e.getBindingResult().getFieldError().getDefaultMessage(); | ||
| 82 | + return error(message); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + private Map<String, Object> error(String message) { | ||
| 86 | + Map<String, Object> result = new HashMap<>(); | ||
| 87 | + result.put("status", ResponseCode.ERROR); | ||
| 88 | + result.put("msg", ""); | ||
| 89 | + | ||
| 90 | + return result; | ||
| 91 | + } | ||
| 92 | +} |