This commit is contained in:
chen-xin-zhi 2025-04-01 13:21:18 +08:00
parent fcebd8474e
commit 8bff09fcbc
4 changed files with 119 additions and 77 deletions

View File

@ -1,75 +1,77 @@
//package com.greenorange.promotion.aop; package com.greenorange.promotion.aop;
//
//
//import com.greenorange.promotion.annotation.AuthCheck; import com.greenorange.promotion.annotation.AuthCheck;
//import com.greenorange.promotion.common.ErrorCode; import com.greenorange.promotion.common.ErrorCode;
//import com.greenorange.promotion.constant.UserConstant; import com.greenorange.promotion.constant.UserConstant;
//import com.greenorange.promotion.exception.BusinessException; import com.greenorange.promotion.exception.BusinessException;
//import com.greenorange.promotion.model.enums.UserRoleEnum; import com.greenorange.promotion.model.entity.User;
//import jakarta.annotation.Resource; import com.greenorange.promotion.model.enums.UserRoleEnum;
//import jakarta.servlet.http.HttpServletRequest; import com.greenorange.promotion.service.user.UserService;
//import org.apache.commons.lang3.StringUtils; import jakarta.annotation.Resource;
//import org.aspectj.lang.ProceedingJoinPoint; import jakarta.servlet.http.HttpServletRequest;
//import org.aspectj.lang.annotation.Around; import org.apache.commons.lang3.StringUtils;
//import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.ProceedingJoinPoint;
//import org.springframework.stereotype.Component; import org.aspectj.lang.annotation.Around;
//import org.springframework.web.context.request.RequestAttributes; import org.aspectj.lang.annotation.Aspect;
//import org.springframework.web.context.request.RequestContextHolder; import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.RequestAttributes;
// import org.springframework.web.context.request.RequestContextHolder;
///** import org.springframework.web.context.request.ServletRequestAttributes;
// * 权限校验AOP
// */ /**
//@Aspect * 权限校验AOP
//@Component */
//public class AuthInterceptor { @Aspect
// @Component
// @Resource public class AuthInterceptor {
// private UserService userService;
// @Resource
// /** private UserService userService;
// * 执行拦截
// */ /**
// @Around("@annotation(authCheck)") * 执行拦截
// public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable { */
// // 接口的权限 @Around("@annotation(authCheck)")
// String mustRole = authCheck.mustRole(); public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
// RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); // 接口的权限
// HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); String mustRole = authCheck.mustRole();
// //当前登录用户 RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
// User loginUser = userService.getLoginUser(request); HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
// //必须有该权限才通过 //当前登录用户
// if (StringUtils.isNotBlank(mustRole)) { User loginUser = userService.getLoginUser(request);
// //mustUserRoleEnum是接口权限 //必须有该权限才通过
// UserRoleEnum mustUserRoleEnum = UserRoleEnum.getEnumByValues(mustRole); if (StringUtils.isNotBlank(mustRole)) {
// if(mustUserRoleEnum == null) { //mustUserRoleEnum是接口权限
// throw new BusinessException(ErrorCode.NO_AUTH_ERROR); UserRoleEnum mustUserRoleEnum = UserRoleEnum.getEnumByValues(mustRole);
// } if(mustUserRoleEnum == null) {
// //用户权限 throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
// String userRole = loginUser.getUserRole(); }
// //根据用户角色获取封装后的枚举类对象 //用户权限
// UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValues(userRole); String userRole = loginUser.getUserRole();
// //根据用户角色获取封装后的枚举类对象
// //如果被封号直接拒绝 UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValues(userRole);
// if (UserRoleEnum.BAN.equals(userRoleEnum)) {
// throw new BusinessException(ErrorCode.NO_AUTH_ERROR); //如果被封号直接拒绝
// } if (UserRoleEnum.BAN.equals(userRoleEnum)) {
// throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
// //如果接口需要Boss权限则需要判断用户是否是boss管理员 }
// if (UserRoleEnum.BOSS.equals(mustUserRoleEnum)) {
// if (!mustRole.equals(userRole)) { //如果接口需要Boss权限则需要判断用户是否是boss管理员
// throw new BusinessException(ErrorCode.NO_AUTH_ERROR); if (UserRoleEnum.BOSS.equals(mustUserRoleEnum)) {
// } if (!mustRole.equals(userRole)) {
// } throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
// //如果接口需要管理员权限则需要判断用户是否是boss或者admin管理员 }
// if (UserRoleEnum.ADMIN.equals(mustUserRoleEnum)) { }
// if (!mustRole.equals(userRole) && !userRole.equals(UserConstant.BOSS_ROLE)) { //如果接口需要管理员权限则需要判断用户是否是boss或者admin管理员
// throw new BusinessException(ErrorCode.NO_AUTH_ERROR); if (UserRoleEnum.ADMIN.equals(mustUserRoleEnum)) {
// } if (!mustRole.equals(userRole) && !userRole.equals(UserConstant.BOSS_ROLE)) {
// } throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
// } }
// //通过权限校验放行 }
// return joinPoint.proceed(); }
// } //通过权限校验放行
// return joinPoint.proceed();
//} }
}

View File

@ -17,6 +17,12 @@ public interface UserConstant {
String USER_DEFAULT_AVATAR = ""; String USER_DEFAULT_AVATAR = "";
/**
* 用户登录键
*/
String USER_LOGIN_STATE = "qingcheng";
/** /**
* 默认角色 * 默认角色
*/ */

View File

@ -10,6 +10,7 @@ import com.greenorange.promotion.model.dto.user.UserUpdateRequest;
import com.greenorange.promotion.model.entity.User; import com.greenorange.promotion.model.entity.User;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.greenorange.promotion.model.vo.user.UserVO; import com.greenorange.promotion.model.vo.user.UserVO;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
@ -63,6 +64,8 @@ public interface UserService extends IService<User> {
boolean delBatchUser(CommonBatchRequest commonBatchRequest); boolean delBatchUser(CommonBatchRequest commonBatchRequest);
/**
* 校验用户是否登录
*/
User getLoginUser(HttpServletRequest request);
} }

View File

@ -15,17 +15,22 @@ import com.greenorange.promotion.model.dto.user.UserAddRequest;
import com.greenorange.promotion.model.dto.user.UserQueryRequest; import com.greenorange.promotion.model.dto.user.UserQueryRequest;
import com.greenorange.promotion.model.dto.user.UserUpdateRequest; import com.greenorange.promotion.model.dto.user.UserUpdateRequest;
import com.greenorange.promotion.model.entity.User; import com.greenorange.promotion.model.entity.User;
import com.greenorange.promotion.model.enums.UserRoleEnum;
import com.greenorange.promotion.model.vo.user.UserVO; import com.greenorange.promotion.model.vo.user.UserVO;
import com.greenorange.promotion.service.common.CommonService; import com.greenorange.promotion.service.common.CommonService;
import com.greenorange.promotion.service.user.UserService; import com.greenorange.promotion.service.user.UserService;
import com.greenorange.promotion.mapper.UserMapper; import com.greenorange.promotion.mapper.UserMapper;
import com.greenorange.promotion.utils.SqlUtils; import com.greenorange.promotion.utils.SqlUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import static com.greenorange.promotion.constant.UserConstant.USER_LOGIN_STATE;
/** /**
* @author 35880 * @author 35880
* @description 针对表user(用户表)的数据库操作Service实现 * @description 针对表user(用户表)的数据库操作Service实现
@ -149,6 +154,32 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "用户批量删除失败"); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "用户批量删除失败");
return true; return true;
} }
/**
* 获取当前登录用户
*/
@Override
public User getLoginUser(HttpServletRequest request) {
HttpSession session = request.getSession();
Object userObj = session.getAttribute(USER_LOGIN_STATE);
User currentUser = (User) userObj;
if (currentUser == null || currentUser.getId() == null) {
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
}
//根据id进行查询
Long userId = currentUser.getId();
currentUser = this.getById(userId);
if (currentUser == null) {
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
}
//被封号
if (UserRoleEnum.BAN.getValue().equals(currentUser.getUserRole())) {
throw new BusinessException(ErrorCode.FORBIDDEN_ERROR);
}
return currentUser;
}
} }