perf(异常处理): 优化全局异常处理

master
wayn 5 years ago
parent fcfe42d3e6
commit 53f0e33563

@ -17,7 +17,7 @@
<properties>
<java.version>1.8</java.version>
<druid.version>1.1.14</druid.version>
<mybatis-plus.version>3.3.1</mybatis-plus.version>
<mybatis-plus.version>3.3.2</mybatis-plus.version>
<commons.io.version>2.5</commons.io.version>
<commons.collections.version>3.2.2</commons.collections.version>
<commons.fileupload.version>1.3.3</commons.fileupload.version>

@ -0,0 +1,24 @@
package com.wayn.common.enums;
/**
*
*/
public enum UserStatus {
OK(0, "正常"), DISABLE(1, "停用"), DELETED(2, "删除");
private final Integer code;
private final String info;
UserStatus(Integer code, String info) {
this.code = code;
this.info = info;
}
public Integer getCode() {
return code;
}
public String getInfo() {
return info;
}
}

@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ -72,7 +73,7 @@ public class GlobalExceptionHandler {
}
/**
*
* 访
*/
@ExceptionHandler(AccessDeniedException.class)
public R handleAuthorizationException(AccessDeniedException e) {
@ -80,6 +81,16 @@ public class GlobalExceptionHandler {
return R.error(HttpStatus.FORBIDDEN.value(), "没有权限,请联系管理员授权");
}
/**
*
*/
@ExceptionHandler(AuthenticationException.class)
public R handleAuthenticationException(AuthenticationException e) {
log.error(e.getMessage());
return R.error(HttpStatus.FORBIDDEN.value(), "认证失败,请联系管理员授权");
}
/**
*
*/

@ -1,12 +1,14 @@
package com.wayn.framework.security.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wayn.common.enums.UserStatus;
import com.wayn.framework.security.LoginUserDetail;
import com.wayn.project.system.domain.SysUser;
import com.wayn.project.system.service.IDeptService;
import com.wayn.project.system.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@ -39,10 +41,13 @@ public class UserDetailsServiceImpl implements UserDetailsService {
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
SysUser user = iUserService.getOne(new QueryWrapper<SysUser>().eq("user_name", username));
if (user == null) {
// List<GrantedAuthority> authorityLists = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
log.info("登录用户:{} 不存在.", username);
throw new UsernameNotFoundException("登录用户:" + username + " 不存在");
}
if (UserStatus.DISABLE.getCode() == user.getUserStatus()) {
log.info("登录用户:{} 已经被停用.", username);
throw new DisabledException("登录用户:" + username + " 不存在");
}
user.setSysDept(iDeptService.getById(user.getDeptId()));
return new LoginUserDetail(user, permissionService.getMenuPermission(user));
}

Loading…
Cancel
Save