diff --git a/pom.xml b/pom.xml index 3858e62..5bcfbbe 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ 1.8 1.1.14 - 3.3.1 + 3.3.2 2.5 3.2.2 1.3.3 diff --git a/src/main/java/com/wayn/common/enums/UserStatus.java b/src/main/java/com/wayn/common/enums/UserStatus.java new file mode 100644 index 0000000..dc6e049 --- /dev/null +++ b/src/main/java/com/wayn/common/enums/UserStatus.java @@ -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; + } +} diff --git a/src/main/java/com/wayn/common/exception/GlobalExceptionHandler.java b/src/main/java/com/wayn/common/exception/GlobalExceptionHandler.java index ccf4b17..fb8f9a5 100644 --- a/src/main/java/com/wayn/common/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/wayn/common/exception/GlobalExceptionHandler.java @@ -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(), "认证失败,请联系管理员授权"); + } + + /** * 全局异常 */ diff --git a/src/main/java/com/wayn/framework/security/service/UserDetailsServiceImpl.java b/src/main/java/com/wayn/framework/security/service/UserDetailsServiceImpl.java index db70651..5803ba3 100644 --- a/src/main/java/com/wayn/framework/security/service/UserDetailsServiceImpl.java +++ b/src/main/java/com/wayn/framework/security/service/UserDetailsServiceImpl.java @@ -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().eq("user_name", username)); if (user == null) { -// List 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)); }