处理捕获异常出现异常的状态
This commit is contained in:
parent
3f243f5c9c
commit
f2b8ffdbf5
|
@ -4,8 +4,8 @@ import com.google.gson.Gson;
|
|||
import com.jsl.oa.utils.ErrorCode;
|
||||
import com.jsl.oa.utils.JwtUtil;
|
||||
import com.jsl.oa.utils.ResultUtil;
|
||||
import org.apache.shiro.authc.ExpiredCredentialsException;
|
||||
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
@ -16,9 +16,9 @@ import javax.servlet.http.HttpServletRequest;
|
|||
* <hr/>
|
||||
* 用于JWT的过滤器
|
||||
*
|
||||
* @since v1.1.0
|
||||
* @version v1.1.0
|
||||
* @author 筱锋xiao_lfeng
|
||||
* @version v1.1.0
|
||||
* @since v1.1.0
|
||||
*/
|
||||
public class JwtFilter extends BasicHttpAuthenticationFilter {
|
||||
|
||||
|
@ -27,8 +27,8 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
|
|||
* <hr/>
|
||||
* 判断用户Token是否存在,如果存在则进行验证
|
||||
*
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
* @param mappedValue 映射值
|
||||
* @return {@link Boolean}
|
||||
*/
|
||||
|
@ -51,20 +51,45 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
|
|||
* <hr/>
|
||||
* 当访问被拒绝时,会调用此方法
|
||||
*
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
* @param mappedValue 映射值
|
||||
* @return {@link Boolean}
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
@Override
|
||||
protected boolean onAccessDenied(ServletRequest request, @NotNull ServletResponse response, Object mappedValue) throws Exception {
|
||||
Gson gson = new Gson();
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
response.getWriter().println(gson.toJson(ResultUtil.error(ErrorCode.UNAUTHORIZED)));
|
||||
return false;
|
||||
protected boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
|
||||
try {
|
||||
// 尝试获取Authorization Header
|
||||
String token = getAuthzHeader(request);
|
||||
if (token == null || token.isEmpty()) {
|
||||
// 未提供Token,拒绝访问
|
||||
Gson gson = new Gson();
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
response.getWriter().println(gson.toJson(ResultUtil.error(ErrorCode.UNAUTHORIZED)));
|
||||
return false;
|
||||
} else {
|
||||
// 解析Bearer后面的令牌
|
||||
token = token.replace("Bearer ", "");
|
||||
System.out.println(token);
|
||||
if (JwtUtil.verify(token)) {
|
||||
// Token验证通过
|
||||
return true;
|
||||
} else {
|
||||
// Token验证失败,抛出异常
|
||||
throw new ExpiredCredentialsException("Token已过期");
|
||||
}
|
||||
}
|
||||
} catch (ExpiredCredentialsException e) {
|
||||
// 处理Token过期异常,返回自定义的JSON信息
|
||||
Gson gson = new Gson();
|
||||
response.setContentType("application/json;charset=UTF-8");
|
||||
response.getWriter().println(gson.toJson(ResultUtil.error(ErrorCode.TOKEN_EXPIRED)));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <h2>获取Authorization Header</h2>
|
||||
* <hr/>
|
||||
|
|
|
@ -33,7 +33,7 @@ public class ShiroConfiguration {
|
|||
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
|
||||
|
||||
// 设置未登陆响应接口
|
||||
shiroFilterFactoryBean.setLoginUrl("/unauthorized");
|
||||
shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
|
||||
|
||||
// 添加JWT过滤器
|
||||
Map<String, Filter> filters = new LinkedHashMap<>();
|
||||
|
|
|
@ -2,16 +2,31 @@ package com.jsl.oa.exception;
|
|||
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
import com.jsl.oa.utils.ResultUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
import java.sql.SQLIntegrityConstraintViolationException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ControllerAdvice
|
||||
public class ProcessException {
|
||||
|
||||
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
|
||||
public ResponseEntity<BaseResponse> methodNotAllowedException() {
|
||||
public ResponseEntity<BaseResponse> businessMethodNotAllowedException() {
|
||||
return ResultUtil.error("MethodNotAllowed", 405, "请求方法错误");
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = SQLIntegrityConstraintViolationException.class)
|
||||
public ResponseEntity<BaseResponse> businessSQLIntegrityConstraintViolationException(@NotNull SQLIntegrityConstraintViolationException e) {
|
||||
if (Pattern.matches(".*Duplicate entry.*", e.getMessage())) {
|
||||
return ResultUtil.error("DuplicateEntry", 400, "数据重复");
|
||||
} else if (Pattern.matches(".*Cannot delete or update a parent row: a foreign key constraint fails.*", e.getMessage())) {
|
||||
return ResultUtil.error("DataAssociation", 400, "数据存在关联,无法删除");
|
||||
} else {
|
||||
return ResultUtil.error("DatabaseError", 400, "数据库异常");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,17 +30,23 @@ public interface UserMapper {
|
|||
@Select("SELECT * FROM organize_oa.oa_user WHERE job_id = #{jobId}")
|
||||
UserDO login(UserLoginVO userLoginVO);
|
||||
|
||||
@Update("update organize_oa.oa_user set enabled = 0 where id = #{id} ")
|
||||
@Update("UPDATE organize_oa.oa_user SET enabled = 0 WHERE id = #{id} ")
|
||||
void userDelete(UserDeleteVO userDeleteVO);
|
||||
|
||||
@Update("update organize_oa.oa_user set account_no_locked = 1 where id = #{id} ")
|
||||
@Update("UPDATE organize_oa.oa_user SET account_no_locked = 1 WHERE id = #{id} ")
|
||||
void userLock(UserLockVO userLockVO);
|
||||
|
||||
@Select("select * from organize_oa.oa_user where id = #{id}")
|
||||
@Select("SELECT * FROM organize_oa.oa_user WHERE id = #{id}")
|
||||
UserDO getUserById(Long id);
|
||||
|
||||
@Select("select * from organize_oa.oa_user where email = #{email}")
|
||||
@Select("SELECT * FROM organize_oa.oa_user WHERE email = #{email}")
|
||||
UserDO getUserInfoByEmail(String email);
|
||||
|
||||
@Select("SELECT * FROM organize_oa.oa_user WHERE phone = #{phone}")
|
||||
UserDO getUserInfoByPhone(String user);
|
||||
|
||||
@Select("SELECT * FROM organize_oa.oa_user WHERE job_id = #{jobId}")
|
||||
UserDO getUserByJobId(String user);
|
||||
|
||||
void userEditProfile(UserEditProfile userEditProfile);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.jsl.oa.utils.BaseResponse;
|
|||
import com.jsl.oa.utils.ErrorCode;
|
||||
import com.jsl.oa.utils.ResultUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.mindrot.jbcrypt.BCrypt;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -34,7 +35,7 @@ public class UserServiceImpl implements UserService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public BaseResponse userLock(UserLockVO userLockVO) {
|
||||
public BaseResponse userLock(@NotNull UserLockVO userLockVO) {
|
||||
//判断用户是否存在
|
||||
if(userDAO.isExistUser(userLockVO.getId())) {
|
||||
userDAO.userLock(userLockVO);
|
||||
|
@ -43,7 +44,7 @@ public class UserServiceImpl implements UserService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public BaseResponse userEditProfile(UserEditProfile userEditProfile) {
|
||||
public BaseResponse userEditProfile(@NotNull UserEditProfile userEditProfile) {
|
||||
if(userDAO.isExistUser(userEditProfile.getId())) {
|
||||
if(userEditProfile.getPassword()!=null){
|
||||
userEditProfile.setPassword(BCrypt.hashpw(userEditProfile.getPassword(), BCrypt.gensalt()));
|
||||
|
|
|
@ -11,6 +11,7 @@ public enum ErrorCode {
|
|||
TIMESTAMP_ERROR("TimestampError", 40014, "时间戳错误"),
|
||||
USER_NOT_EXIST("UserNotExist", 40015, "用户不存在"),
|
||||
UNAUTHORIZED("Unauthorized", 40100, "未授权"),
|
||||
TOKEN_EXPIRED("TokenExpired", 40101, "Token已过期"),
|
||||
DATABASE_INSERT_ERROR("DatabaseInsertError", 50010, "数据库插入错误"),
|
||||
DATABASE_UPDATE_ERROR("DatabaseUpdateError", 50011, "数据库更新错误"),
|
||||
DATABASE_DELETE_ERROR("DatabaseDeleteError", 50012, "数据库删除错误");
|
||||
|
|
Loading…
Reference in New Issue
Block a user