修改返回值

master
bynt 2 years ago
parent 59a1e16240
commit ed396d592d

@ -0,0 +1,130 @@
package com.baiye.http;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author
* "" NULL json
* @param <T>
*/
@JsonSerialize
@Getter
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@NoArgsConstructor
public class LoginResponse<T> implements Serializable {
private static final long serialVersionUID = -7237947628886320124L;
/**
* 0, 1,
*/
private int status;
/**
*
*/
private String message;
/**
*
*/
private T data;
/*
*/
private LoginResponse(int status) {
this.status = status;
}
private LoginResponse(int status, T data) { // ps: 当调用T为String类型时候,会默认调用下面的ServerResponse(int status, String message)类型的构造器
this.status = status;
this.data = data;
}
private LoginResponse(int status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
private LoginResponse(int status, String message) {
this.status = status;
this.message = message;
}
/*
,,
*/
/**
*
*/
public static <T> LoginResponse<T> createBySuccess() {
return new LoginResponse<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getDesc());
}
/**
*
*/
public static <T> LoginResponse<T> createBySuccessMessage(String message) {
return new LoginResponse<>(ResponseCode.SUCCESS.getCode(), message);
}
/**
*
*/
public static <T> LoginResponse<T> createBySuccess(T data) {
return new LoginResponse<>(ResponseCode.SUCCESS.getCode(), data);
}
/**
*
*/
public static <T> LoginResponse<T> createBySuccess(String message, T data) {
return new LoginResponse<>(ResponseCode.SUCCESS.getCode(), message, data);
}
/*
,,
*/
/**
*
*/
public static <T> LoginResponse<T> createByError(){
return new LoginResponse<>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
}
/**
* ()
*/
public static <T> LoginResponse<T> createByErrorMessage(String errorMessage){
return new LoginResponse<>(ResponseCode.ERROR.getCode(),errorMessage);
}
/**
* ()()
*/
public static <T> LoginResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage){
return new LoginResponse<>(errorCode,errorMessage);
}
/**
* ()()
*/
public static <T> LoginResponse<T> createBySuccess(int successCode){
return new LoginResponse<>(successCode, ResponseCode.SUCCESS.getDesc());
}
}

@ -33,7 +33,7 @@ public class ResourceAuthExceptionEntryPoint implements AuthenticationEntryPoint
Map<String,Object> result = new HashMap<>(6);
result.put("code", HttpStatus.UNAUTHORIZED.value());
if (e!=null){
result.put("msg","error");
result.put("message","error");
result.put("data", "user does not exist ");
}
response.setStatus(HttpStatus.UNAUTHORIZED.value());

@ -1,6 +1,6 @@
package com.baiye.handle;
import com.baiye.http.CommonResponse;
import com.baiye.http.LoginResponse;
import com.baiye.http.ResponseCode;
import com.baiye.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
@ -24,38 +24,38 @@ import java.io.IOException;
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
CommonResponse<?> result;
LoginResponse<?> result;
String username = request.getParameter("username");
if (exception instanceof AccountExpiredException) {
// 账号过期
log.info("[登录失败] - 用户[{}]账号过期", username);
result = CommonResponse.createByErrorMessage(ResponseCode.USER_ACCOUNT_EXPIRED.getDesc());
result = LoginResponse.createByErrorMessage(ResponseCode.USER_ACCOUNT_EXPIRED.getDesc());
} else if (exception instanceof BadCredentialsException) {
// 密码错误
log.info("[登录失败] - 用户[{}]密码错误", username);
result = CommonResponse.createByErrorMessage(ResponseCode.USER_PASSWORD_ERROR.getDesc());
result = LoginResponse.createByErrorMessage(ResponseCode.USER_PASSWORD_ERROR.getDesc());
} else if (exception instanceof CredentialsExpiredException) {
// 密码过期
log.info("[登录失败] - 用户[{}]密码过期", username);
result = CommonResponse.createByErrorMessage(ResponseCode.USER_PASSWORD_EXPIRED.getDesc());
result = LoginResponse.createByErrorMessage(ResponseCode.USER_PASSWORD_EXPIRED.getDesc());
} else if (exception instanceof DisabledException) {
// 用户被禁用
log.info("[登录失败] - 用户[{}]被禁用", username);
result = CommonResponse.createByErrorMessage(ResponseCode.USER_DISABLED.getDesc());
result = LoginResponse.createByErrorMessage(ResponseCode.USER_DISABLED.getDesc());
} else if (exception instanceof LockedException) {
// 用户被锁定
log.info("[登录失败] - 用户[{}]被锁定", username);
result = CommonResponse.createByErrorMessage(ResponseCode.USER_LOCKED.getDesc());
result = LoginResponse.createByErrorMessage(ResponseCode.USER_LOCKED.getDesc());
} else if (exception instanceof InternalAuthenticationServiceException) {
// 内部错误
log.error(String.format("[登录失败] - [%s]内部错误", username));
result = CommonResponse.createByErrorMessage(ResponseCode.USER_LOGIN_FAIL.getDesc());
result = LoginResponse.createByErrorMessage(ResponseCode.USER_LOGIN_FAIL.getDesc());
} else {
// 其他错误
log.error(String.format("[登录失败] - [%s]其他错误", username), exception);
result = CommonResponse.createByErrorMessage(ResponseCode.USER_LOGIN_FAIL.getDesc());
result = LoginResponse.createByErrorMessage(ResponseCode.USER_LOGIN_FAIL.getDesc());
}
ResponseUtil.responseWriter(response, "UTF-8", HttpStatus.UNAUTHORIZED.value(), result);
}

Loading…
Cancel
Save