wlcb-smart-city-usercenter/src/main/java/cn/com/tenlion/usercenter/aspect/GuestControllerAspect.java

129 lines
4.1 KiB
Java
Raw Normal View History

2023-09-07 14:31:18 +08:00
package cn.com.tenlion.usercenter.aspect;
import cn.com.tenlion.usercenter.login.guest.GuestProperties;
import ink.wgink.common.component.SecurityComponent;
import ink.wgink.exceptions.base.SystemException;
import ink.wgink.pojo.dtos.role.RoleSimpleDTO;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
@Order(-1)
@Component
@Aspect
public class GuestControllerAspect {
/**
* 默认新增事务
*/
private static String[] DEFAULT_SAVE_ARRAY = {"add*", "save*", "insert*", "create*", "new*"};
/**
* 默认删除事务
*/
private static String[] DEFAULT_REMOVE_ARRAY = {"delete*", "remove*"};
/**
* 默认执行事务
*/
private static String[] DEFAULT_UPDATE_ARRAY = {"update*", "edit*", "reset*"};
/**
* 默认其它事务
*/
private static String[] DEFAULT_OTHER_ARRAY = {"send*", "exec*", "set*", "login*", "register*", "sign*", "rest*", "upload*"};
@Autowired
private GuestProperties guestProperties;
@Autowired
private SecurityComponent securityComponent;
@Pointcut("execution(public * *..controller..*.*(..))")
public void apiLogCutPoint() {
}
/**
* 访客没有曾改权限只有查看权限
*
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around("apiLogCutPoint()")
public Object apiLogAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
if (guestProperties == null) {
return proceedingJoinPoint.proceed();
}
// 非访客,不处理
if (!isGuest()) {
return result(proceedingJoinPoint);
}
Method method = ((MethodSignature) proceedingJoinPoint.getSignature()).getMethod();
// GET不处理
if (isGetMethod(method)) {
return result(proceedingJoinPoint);
}
throw new SystemException("权限不足");
}
private Object result(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result;
try {
result = proceedingJoinPoint.proceed();
} catch (Throwable e) {
throw e;
}
return result;
}
private boolean isGuest() {
if (securityComponent == null) {
return false;
}
if (securityComponent.getCurrentUser() == null) {
return false;
}
if (securityComponent.getCurrentUser().getUserUsername().equalsIgnoreCase("admin")) {
return false;
}
List<RoleSimpleDTO> roles = securityComponent.getCurrentUser().getRoles();
for (RoleSimpleDTO roleSimpleDTO : roles) {
for (String guestRoleId : guestProperties.getGuestRoleIds()) {
if (StringUtils.equals(roleSimpleDTO.getRoleId(), guestRoleId)) {
return true;
}
}
}
return false;
}
private boolean isGetMethod(Method method) {
GetMapping getMapping = method.getAnnotation(GetMapping.class);
if (getMapping != null) {
return true;
}
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
if (requestMapping == null) {
return false;
}
RequestMethod[] requestMethods = requestMapping.method();
for (RequestMethod requestMethod : requestMethods) {
if (StringUtils.equalsIgnoreCase(requestMethod.name(), "GET")) {
return true;
}
}
return false;
}
}