zhenghuang 6 years ago
parent e35a216ce0
commit aa583d9630

3
.gitignore vendored

@ -2,4 +2,5 @@
/ic.iml
target
.idea
*.iml
*.log
*.iml

@ -0,0 +1,153 @@
package com.kiisoo.ic.config;
import com.kiisoo.ic.common.BaseController;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*
* @author z
*/
@RestControllerAdvice
public class ExceptionAdvice extends BaseController {
/**
* Shiro
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(ShiroException.class)
public Map<String, Object> handle401(ShiroException e) {
return fail(HttpStatus.UNAUTHORIZED.value() + "", e.getMessage());
}
/**
* Shiro(UnauthorizedException)
* 访
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
public Map<String, Object> handle401(UnauthorizedException e) {
return fail("40001", e.getMessage());
}
/**
* Shiro(UnauthenticatedException)
* 访
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(UnauthenticatedException.class)
public Map<String, Object> handle401(UnauthenticatedException e) {
return fail("40001", "认证异常: " + e.getMessage());
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public Map<String, Object> validException(BindException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
Map<String, Object> result = this.getValidError(fieldErrors);
return fail("40006", result);
}
/**
* (MethodArgumentNotValidException)
*
* @return
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, Object> validException(MethodArgumentNotValidException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
Map<String, Object> result = this.getValidError(fieldErrors);
return fail("406", result.get("errorList"));
}
/**
* 404
*
* @return
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public Map<String, Object> handle(NoHandlerFoundException e) {
return fail("404", "资源不存在");
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(HttpMessageNotReadableException.class)
public Map<String, Object> httpMessageNotReadableException(HttpServletRequest request, Throwable ex) {
return fail(this.getStatus(request).value() + "", "请求参数错误");
}
/**
*
*
* @param request
* @param ex
* @return
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public Map<String, Object> globalException(HttpServletRequest request, Throwable ex) {
ex.printStackTrace();
return fail(this.getStatus(request).value() + "", ex.toString() + ": " + ex.getMessage());
}
/**
*
*
* @param request
* @return
*/
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
return HttpStatus.valueOf(statusCode);
}
/**
*
*
* @param fieldErrors
* @return
*/
private Map<String, Object> getValidError(List<FieldError> fieldErrors) {
Map<String, Object> result = new HashMap<String, Object>(16);
List<String> errorList = new ArrayList<String>();
StringBuffer errorMsg = new StringBuffer();
for (FieldError error : fieldErrors) {
errorList.add(error.getDefaultMessage());
errorMsg.append(error.getDefaultMessage());
}
result.put("errorList", errorList);
result.put("errorMsg", errorMsg);
return result;
}
}

@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ -8,9 +8,9 @@ mybatis:
spring:
datasource:
url: jdbc:mysql://192.168.0.215:3306/bsd2?characterEncoding=utf8&allowMultiQueries=true&autoReconnect=true
username: p2p
password: p2p
url: jdbc:mysql://106.15.109.152:3306/p2p?characterEncoding=utf8&allowMultiQueries=true&autoReconnect=true
username: root
password: Bsd@2019
driver-class-name: com.mysql.jdbc.Driver
hikari:
maximum-pool-size: 80

Loading…
Cancel
Save