refactor(auth): 优化认证服务异常处理逻辑
- 合并导入Spring Security认证相关类 - 增加对内部认证服务异常的捕获处理 - 保留业务异常的直接抛出 - 完善未知异常的日志记录,包含完整堆栈信息 - 调整设备响应DTO结构,增加字段分隔空行 fix(device): 调整设备响应对象结构 - 在设备响应DTO中添加ID字段上方空行以改善代码可读性
This commit is contained in:
@@ -7,6 +7,7 @@ import java.time.LocalDate;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class DeviceResponse {
|
public class DeviceResponse {
|
||||||
|
|
||||||
private Long id;
|
private Long id;
|
||||||
private String name;
|
private String name;
|
||||||
private String model;
|
private String model;
|
||||||
|
|||||||
@@ -13,10 +13,7 @@ import cn.nopj.chaos_api.util.JwtTokenUtil;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.*;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
|
||||||
import org.springframework.security.authentication.DisabledException;
|
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
@@ -87,12 +84,16 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
res.setTokenHead(tokenHead);
|
res.setTokenHead(tokenHead);
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
}catch (BadCredentialsException e) {
|
}catch (BadCredentialsException | InternalAuthenticationServiceException e) {
|
||||||
throw new BizException(ErrorCode.USER_NOT_EXISTS_OR_PASSWORD_WRONG);
|
throw new BizException(ErrorCode.USER_NOT_EXISTS_OR_PASSWORD_WRONG);
|
||||||
} catch (DisabledException e) {
|
} catch (DisabledException e) {
|
||||||
throw new BizException(ErrorCode.USER_NOT_ENABLED);
|
throw new BizException(ErrorCode.USER_NOT_ENABLED);
|
||||||
} catch (Exception e) {
|
} catch (BizException e){
|
||||||
log.error("用户登录异常: {}", e.getMessage());
|
throw e;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
|
||||||
|
log.error("用户登录异常",e);
|
||||||
throw new BizException(ErrorCode.SYSTEM_ERROR);
|
throw new BizException(ErrorCode.SYSTEM_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user