修改异常返回错误
parent
cbb5408850
commit
0d3c221d0b
@ -0,0 +1,23 @@
|
||||
package com.baiye.core.base.api;
|
||||
|
||||
/**
|
||||
* 返回码接口
|
||||
*
|
||||
* @author pangu
|
||||
*/
|
||||
public interface IResultCode {
|
||||
|
||||
/**
|
||||
* 返回码
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int getCode();
|
||||
|
||||
/**
|
||||
* 返回消息
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
String getMsg();
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.baiye.core.util;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baiye.core.base.api.Result;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 响应工具
|
||||
*
|
||||
* @author pangu
|
||||
*/
|
||||
public class ResponseUtil {
|
||||
|
||||
private ResponseUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置响应
|
||||
*
|
||||
* @param response HttpServletResponse
|
||||
* @param contentType content-type
|
||||
* @param status http状态码
|
||||
* @param value 响应内容
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static void responseWriter(HttpServletResponse response, String contentType,
|
||||
int status, Object value) throws IOException {
|
||||
response.setContentType(contentType);
|
||||
response.setStatus(status);
|
||||
response.getOutputStream().write(JSON.toJSONString(value).getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置webflux模型响应
|
||||
*
|
||||
* @param response ServerHttpResponse
|
||||
* @param contentType content-type
|
||||
* @param status http状态码
|
||||
* @param value 响应内容
|
||||
* @return Mono<Void>
|
||||
*/
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType,
|
||||
HttpStatus status, Object value) {
|
||||
response.setStatusCode(status);
|
||||
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
||||
Result<?> result = Result.fail(status.value(), value.toString());
|
||||
DataBuffer dataBuffer = response.bufferFactory().wrap(JSON.toJSONString(result).getBytes());
|
||||
return response.writeWith(Mono.just(dataBuffer));
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.baiye.exception.global.handle.GlobalExceptionHandler
|
@ -1,4 +1,4 @@
|
||||
package com.baiye;
|
||||
package com.baiye.annotation;
|
||||
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
|
@ -1,4 +1,4 @@
|
||||
package com.baiye;
|
||||
package com.baiye.annotation;
|
||||
|
||||
import com.baiye.config.ResourceServerAutoConfiguration;
|
||||
import com.baiye.component.PermissionComponent;
|
@ -0,0 +1,54 @@
|
||||
package com.baiye.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 忽略URL属性配置
|
||||
*
|
||||
* @author pangu
|
||||
*/
|
||||
@Data
|
||||
@RefreshScope
|
||||
@Component
|
||||
public class IgnoreUrlPropsConfiguration {
|
||||
|
||||
/**
|
||||
* 认证中心默认忽略验证地址
|
||||
*/
|
||||
private static final String[] SECURITY_ENDPOINTS = {
|
||||
"/auth/**",
|
||||
"/actuator/**",
|
||||
"/oauth/**",
|
||||
"/auth/**",
|
||||
"/token/**",
|
||||
"/login/*",
|
||||
"/actuator/**",
|
||||
"/v2/api-docs",
|
||||
"/doc.html",
|
||||
"/webjars/**",
|
||||
"**/favicon.ico",
|
||||
"/swagger-resources/**"
|
||||
};
|
||||
|
||||
private List<String> urls = new ArrayList<>();
|
||||
|
||||
private List<String> client = new ArrayList<>();
|
||||
|
||||
private List<String> ignoreSecurity = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 首次加载合并ENDPOINTS
|
||||
*/
|
||||
@PostConstruct
|
||||
public void initIgnoreSecurity() {
|
||||
Collections.addAll(ignoreSecurity, SECURITY_ENDPOINTS);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.baiye.handle;
|
||||
|
||||
import com.baiye.core.base.api.Result;
|
||||
import com.baiye.core.base.api.ResultCode;
|
||||
import com.baiye.core.util.ResponseUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.authentication.*;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 登录失败的回调
|
||||
* @author pangu
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CustomAuthenticationFailureHandler implements AuthenticationFailureHandler {
|
||||
@Override
|
||||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
|
||||
Result<?> result;
|
||||
String username = request.getParameter("username");
|
||||
if (exception instanceof AccountExpiredException) {
|
||||
// 账号过期
|
||||
log.info("[登录失败] - 用户[{}]账号过期", username);
|
||||
result = Result.fail(ResultCode.USER_ACCOUNT_EXPIRED);
|
||||
} else if (exception instanceof BadCredentialsException) {
|
||||
// 密码错误
|
||||
log.info("[登录失败] - 用户[{}]密码错误", username);
|
||||
result = Result.fail(ResultCode.USER_PASSWORD_ERROR);
|
||||
} else if (exception instanceof CredentialsExpiredException) {
|
||||
// 密码过期
|
||||
log.info("[登录失败] - 用户[{}]密码过期", username);
|
||||
result = Result.fail(ResultCode.USER_PASSWORD_EXPIRED);
|
||||
} else if (exception instanceof DisabledException) {
|
||||
// 用户被禁用
|
||||
log.info("[登录失败] - 用户[{}]被禁用", username);
|
||||
result = Result.fail(ResultCode.USER_DISABLED);
|
||||
|
||||
} else if (exception instanceof LockedException) {
|
||||
// 用户被锁定
|
||||
log.info("[登录失败] - 用户[{}]被锁定", username);
|
||||
result = Result.fail(ResultCode.USER_LOCKED);
|
||||
|
||||
} else if (exception instanceof InternalAuthenticationServiceException) {
|
||||
// 内部错误
|
||||
log.error(String.format("[登录失败] - [%s]内部错误", username));
|
||||
result = Result.fail(ResultCode.USER_LOGIN_FAIL);
|
||||
} else {
|
||||
// 其他错误
|
||||
log.error(String.format("[登录失败] - [%s]其他错误", username), exception);
|
||||
result = Result.fail(ResultCode.USER_LOGIN_FAIL);
|
||||
}
|
||||
ResponseUtil.responseWriter(response, "UTF-8", HttpStatus.UNAUTHORIZED.value(), result);
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.baiye.component.InnerAspect,\
|
||||
com.baiye.service.UserDetailsServiceImpl
|
||||
com.baiye.service.UserDetailsServiceImpl,\
|
||||
com.baiye.config.IgnoreUrlPropsConfiguration
|
||||
|
@ -0,0 +1,71 @@
|
||||
package com.baiye.auth.translator;
|
||||
|
||||
import com.baiye.exception.global.handle.ApiError;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidScopeException;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
|
||||
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
|
||||
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* WEB响应异常处理类
|
||||
*
|
||||
* @author pangu
|
||||
*/
|
||||
@Slf4j
|
||||
@Component("customWebRespExceptionTranslator")
|
||||
public class CustomWebRespExceptionTranslator implements WebResponseExceptionTranslator {
|
||||
|
||||
@Override
|
||||
public ResponseEntity<?> translate(Exception e) {
|
||||
ResponseEntity.BodyBuilder status = ResponseEntity.status(HttpStatus.UNAUTHORIZED);
|
||||
String message = "认证失败";
|
||||
log.error(message, e);
|
||||
if (e instanceof UnsupportedGrantTypeException) {
|
||||
message = "不支持该认证类型";
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
if (e instanceof InvalidTokenException
|
||||
&& StringUtils.containsIgnoreCase(e.getMessage(), "Invalid refresh token (expired)")) {
|
||||
message = "刷新令牌已过期,请重新登录";
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
if (e instanceof InvalidScopeException) {
|
||||
message = "不是有效的scope值";
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
if (e instanceof InvalidGrantException) {
|
||||
if (StringUtils.containsIgnoreCase(e.getMessage(), "Invalid refresh token")) {
|
||||
message = "refresh token无效";
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
if (StringUtils.containsIgnoreCase(e.getMessage(), "Invalid authorization code")) {
|
||||
message = "authorization code无效";
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
|
||||
}
|
||||
if (StringUtils.containsIgnoreCase(e.getMessage(), "locked")) {
|
||||
message = "用户已被锁定,请联系管理员";
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
message = "用户名或密码错误";
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 统一返回
|
||||
*/
|
||||
private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
|
||||
return new ResponseEntity<>(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
, \ / ,
|
||||
/ \ )\__/( / \
|
||||
/ \ (_\ /_) / \
|
||||
__________________/_____\__\@ @/___/_____\_________________
|
||||
| ______ |\../| |
|
||||
| ''|| \\ _____ \VV/ _____ ____ |
|
||||
| || || //|| || \\ //|| || \\ // \\ \\ // |
|
||||
| ||=='' // || || || // || || || || || \\_// |
|
||||
| || //==|| ||=='' //==|| || || || || // \\ |
|
||||
| || // || || \\ // || ||__// \\__// // \\ |
|
||||
|__________________________________________________________|
|
||||
| /\ / \\ \ /\ |
|
||||
| / V )) V \ |
|
||||
|/ ` // ' \|
|
||||
` V '
|
||||
|
||||
xx xx xx xx xx xx
|
||||
xx xx xx xx xx xx xx xx
|
||||
=========================================================================
|
@ -0,0 +1,19 @@
|
||||
, \ / ,
|
||||
/ \ )\__/( / \
|
||||
/ \ (_\ /_) / \
|
||||
__________________/_____\__\@ @/___/_____\_________________
|
||||
| ______ |\../| |
|
||||
| ''|| \\ _____ \VV/ _____ ____ |
|
||||
| || || //|| || \\ //|| || \\ // \\ \\ // |
|
||||
| ||=='' // || || || // || || || || || \\_// |
|
||||
| || //==|| ||=='' //==|| || || || || // \\ |
|
||||
| || // || || \\ // || ||__// \\__// // \\ |
|
||||
|__________________________________________________________|
|
||||
| /\ / \\ \ /\ |
|
||||
| / V )) V \ |
|
||||
|/ ` // ' \|
|
||||
` V '
|
||||
|
||||
xx xx xx xx xx xx
|
||||
xx xx xx xx xx xx xx xx
|
||||
=========================================================================
|
@ -0,0 +1,47 @@
|
||||
package com.baiye.controller;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import com.baiye.exception.global.BadRequestException;
|
||||
import com.baiye.properties.FileProperties;
|
||||
import com.baiye.util.FileUtil;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author Enzo
|
||||
* @date : 2022/1/25
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/pictures")
|
||||
@RequiredArgsConstructor
|
||||
public class PictureController {
|
||||
private final FileProperties properties;
|
||||
|
||||
@ApiOperation("修改头像")
|
||||
@PostMapping(value = "/upload")
|
||||
public ResponseEntity<Object> updateAvatar(@RequestParam("file") MultipartFile uploadFile) {
|
||||
String image = "gif jpg png jpeg";
|
||||
String fileType = FileUtil.getExtensionName
|
||||
(uploadFile.getOriginalFilename());
|
||||
if (fileType != null && !image.contains(fileType)) {
|
||||
throw new BadRequestException("文件格式错误!, 仅支持 " + image + " 格式");
|
||||
}
|
||||
// 应改为File路径
|
||||
File file = FileUtil.upload
|
||||
(uploadFile, properties.getPath().getAvatar());
|
||||
ImmutableMap<String, String> picture = ImmutableMap.of
|
||||
("picture", file != null
|
||||
? file.getName() : CharSequenceUtil.SPACE);
|
||||
return new ResponseEntity<>(picture, HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -1,8 +1,19 @@
|
||||
_ _ _
|
||||
| | | | (_)
|
||||
___| |______ __ _ __| |_ __ ___ _ _ __
|
||||
/ _ | |______/ _` |/ _` | '_ ` _ \| | '_ \
|
||||
| __| | | (_| | (_| | | | | | | | | | |
|
||||
\___|_| \__,_|\__,_|_| |_| |_|_|_| |_|
|
||||
|
||||
:: Spring Boot :: (v2.1.0.RELEASE)
|
||||
, \ / ,
|
||||
/ \ )\__/( / \
|
||||
/ \ (_\ /_) / \
|
||||
__________________/_____\__\@ @/___/_____\_________________
|
||||
| ______ |\../| |
|
||||
| ''|| \\ _____ \VV/ _____ ____ |
|
||||
| || || //|| || \\ //|| || \\ // \\ \\ // |
|
||||
| ||=='' // || || || // || || || || || \\_// |
|
||||
| || //==|| ||=='' //==|| || || || || // \\ |
|
||||
| || // || || \\ // || ||__// \\__// // \\ |
|
||||
|__________________________________________________________|
|
||||
| /\ / \\ \ /\ |
|
||||
| / V )) V \ |
|
||||
|/ ` // ' \|
|
||||
` V '
|
||||
|
||||
xx xx xx xx xx xx
|
||||
xx xx xx xx xx xx xx xx
|
||||
=========================================================================
|
Loading…
Reference in New Issue