修复了获取我的工作项为空时抛出500的错误,修复了同IP登录请求过多时抛出500的错误,关闭了所有调试用的输出

master
ArgonarioD 2022-07-15 00:35:33 +08:00
parent 7e15ea8ed6
commit 2b964c907d
7 changed files with 38 additions and 31 deletions

View File

@ -26,28 +26,28 @@ public class ExceptionHandlerAdvice {
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ResponseMap handleUnauthorizedException(Exception e) {
// log.error(ExceptionUtils.getStackTrace(e));
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return ResponseMap.of(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
}
@ExceptionHandler(BadRequestException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseMap handleBadRequestException(BadRequestException e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return ResponseMap.of(HttpStatus.BAD_REQUEST.value(), e.getMessage());
}
@ExceptionHandler(ForbiddenException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseMap handleForbiddenException(ForbiddenException e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return ResponseMap.of(HttpStatus.FORBIDDEN.value(), e.getMessage());
}
@ExceptionHandler(BindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseMap handleBindException(BindException e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return ResponseMap.of(HttpStatus.BAD_REQUEST.value(),
e.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
@ -58,7 +58,7 @@ public class ExceptionHandlerAdvice {
@ExceptionHandler(TooManyRequestException.class)
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public ResponseMap handleTooManyRequestException(TooManyRequestException e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return ResponseMap.of(HttpStatus.TOO_MANY_REQUESTS.value(), e.getMessage());
}
}

View File

@ -26,7 +26,7 @@ public class TokenInterceptor implements HandlerInterceptor {
//FIXME: 最终上线时要把这里的输出删掉
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws TokenException {
System.out.println(httpServletRequest.getRequestURL() + " " + httpServletRequest.getMethod());
// System.out.println(httpServletRequest.getRequestURL() + " " + httpServletRequest.getMethod());
String token = httpServletRequest.getHeader(TokenUtils.HEADER_TOKEN);
if (null == token || "".equals(token.trim())) {
throw new TokenException("缺少Token");
@ -37,8 +37,8 @@ public class TokenInterceptor implements HandlerInterceptor {
if (TokenUtils.checkTimeOut(token)) {
throw new TokenException("Token已过期");
}
System.out.println(Objects.requireNonNull(TokenUtils.getStaffId(token)));
System.out.println(token);
// System.out.println(Objects.requireNonNull(TokenUtils.getStaffId(token)));
// System.out.println(token);
if (!token.equals(redisTemplate.opsForValue().get(Objects.<Integer>requireNonNull(TokenUtils.getStaffId(token))))) {
throw new TokenException("请重新登录");
}

View File

@ -4,6 +4,7 @@ import cn.edu.hfut.rmdjzz.projectmanagement.entity.Staff;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.TokenException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.TooManyRequestException;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.multipart.MultipartFile;
@ -18,7 +19,7 @@ public interface IStaffService extends IService<Staff> {
String STAFF_DOES_NOT_EXIST = "用户不存在";
String LEVEL_1 = "超级管理员";
Map<String, Object> login(String requestIpAddress, String username, String password) throws BadRequestException, TokenException, ForbiddenException;
Map<String, Object> login(String requestIpAddress, String username, String password) throws BadRequestException, TokenException, ForbiddenException, TooManyRequestException;
Boolean logout(String token) throws TokenException;

View File

@ -104,7 +104,7 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> impl
return projectGroupService.addCreator(project.getProjectId(), TokenUtils.getStaffId(token));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
throw new BadRequestException(BadRequestException.OPERATE_FAILED);
}
return false;
@ -134,7 +134,7 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> impl
return true;
throw new BadRequestException(BadRequestException.OPERATE_FAILED);
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
throw new BadRequestException(BadRequestException.OPERATE_FAILED);
}
}

View File

@ -3,6 +3,7 @@ package cn.edu.hfut.rmdjzz.projectmanagement.service.impl;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Staff;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.TooManyRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.mapper.StaffMapper;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IStaffService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.BeanUtils;
@ -48,12 +49,17 @@ public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements
@SuppressWarnings("ConstantConditions")
@Override
public Map<String, Object> login(String requestIpAddress, String staffUsername, String password) throws BadRequestException, ForbiddenException {
public Map<String, Object> login(String requestIpAddress, String staffUsername, String password) throws BadRequestException, ForbiddenException, TooManyRequestException {
String requestIpAddressHex = IPAddress.of(requestIpAddress).toString();
if (Boolean.FALSE.equals(redisTemplate.hasKey(requestIpAddressHex))) {
redisTemplate.opsForValue().set(requestIpAddressHex, 0, LOGIN_TRY_COUNT_TIMEOUT, TimeUnit.MINUTES);
}
int loginCount = (int) redisTemplate.opsForValue().get(requestIpAddressHex);
int loginCount;
try {
loginCount = (int) redisTemplate.opsForValue().get(requestIpAddressHex);
} catch (Exception e) {
throw new TooManyRequestException("系统繁忙,请稍后再试");
}
if (loginCount >= LOGIN_MAX_TRY_COUNT) {
throw new ForbiddenException(String.format("还需要等待%s才能继续尝试登录"
@ -140,28 +146,28 @@ public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements
if (totalCount < 1) {
throw new BadRequestException("读取不到条目");
}
System.out.println(totalCount);
System.out.println(staffUsernameC);
// System.out.println(totalCount);
// System.out.println(staffUsernameC);
if (!xlsxColumnGetter("staffPassword", 1, staffPasswordC, sheet) ||
staffPasswordC.size() != totalCount) {
throw new BadRequestException("读取列staffPassword失败");
}
System.out.println(staffPasswordC);
// System.out.println(staffPasswordC);
if (!xlsxColumnGetter("staffFullname", 2, staffFullnameC, sheet) ||
staffFullnameC.size() != totalCount) {
throw new BadRequestException("读取列staffFullname失败");
}
System.out.println(staffFullnameC);
// System.out.println(staffFullnameC);
if (!xlsxColumnGetter("staffGender", 3, staffGenderC, sheet) ||
staffGenderC.size() != totalCount) {
throw new BadRequestException("读取列staffGender失败");
}
System.out.println(staffGenderC);
// System.out.println(staffGenderC);
if (!xlsxColumnGetter("staffGlobalLevel", 4, staffGlobalLevelC, sheet) ||
staffGlobalLevelC.size() != totalCount) {
throw new BadRequestException("读取列staffGlobalLevel失败");
}
System.out.println(staffGlobalLevelC);
// System.out.println(staffGlobalLevelC);
if (staffGlobalLevelC.stream().anyMatch(level -> Integer.parseInt(level) < 2)) {
throw new BadRequestException("列staffGlobalLevel无效");
}
@ -178,7 +184,7 @@ public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements
staff.setStaffFullname(staffFullnameC.get(i));
staff.setStaffGender(staffGenderC.get(i));
staff.setStaffGlobalLevel(Integer.parseInt(staffGlobalLevelC.get(i)));
System.out.println(staff);
// System.out.println(staff);
if (baseMapper.insert(staff) != 1) {
throw new BadRequestException("第" + (i + 1) + "行数据错误");
}
@ -187,7 +193,7 @@ public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements
} catch (BadRequestException e) {
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
throw new BadRequestException("导入失败");
}
}

View File

@ -91,7 +91,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return false;
}
}
@ -128,7 +128,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
}
return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return false;
}
}
@ -189,7 +189,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
}
return count;
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return 0;
}
}
@ -259,7 +259,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
}
return baseMapper.selectTaskList(Wrappers.<Task>lambdaQuery()
.eq(Task::getDeleted, false)
.in(Task::getTaskId, results));
.in(!results.isEmpty(), Task::getTaskId, results));
}
@Override
@ -277,7 +277,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
}
return true;
} catch (Exception e) { //TODO: 需要调整
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
return false;
}
}
@ -310,7 +310,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
throw new BadRequestException(BadRequestException.OPERATE_FAILED);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
throw new BadRequestException(BadRequestException.OPERATE_FAILED);
}
return task;
@ -330,7 +330,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
if (task.getTaskStatus().equals(Task.STATUS_CLOSED))
typeChangeValue = 2;
}
System.out.println(!task.checkModification(rawTask));
// System.out.println(!task.checkModification(rawTask));
if (!task.checkModification(rawTask) || !task.checkInsert()) {
throw new BadRequestException(BadRequestException.WRONG_PARAMETERS);
}
@ -352,7 +352,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
throw new BadRequestException(BadRequestException.OPERATE_FAILED);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
throw new BadRequestException(BadRequestException.OPERATE_FAILED);
}
return task;

View File

@ -18,8 +18,8 @@ spring:
password: weakPassword
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
port: 8081
tomcat: