src/main/java/com/sums/aspect/ShiroAuthenticationAspect.java

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

package com.sums.aspect;

import com.sums.annotations.PermissionVerify;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
/**
 * @Author TongHui ShiroAuthenticationAspect.java
 * @Description 权限验证
 * @Date 2022/3/24 9:54
 * @Version 1.0
 */
@Aspect
@Component
public class ShiroAuthenticationAspect {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(public  * com.sums.controller.*.*(..))")
    public void controllerPointcut() {
    }
    @Before("controllerPointcut()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpServletResponse response = attributes.getResponse();
        Subject subject = SecurityUtils.getSubject();
        String targetName = joinPoint.getSignature().getName();
        Object target = joinPoint.getTarget();
        Class<?> aClass = target.getClass();
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            PermissionVerify annotation = method.getAnnotation(PermissionVerify.class);
            if (method.getName().equals(targetName) && annotation != null){
                if (!subject.isPermitted(annotation.value())) {
                    logger.info("权限验证:{}","无此权限");
                    try {
                        response.sendRedirect("/sums/noauth");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return;
                }
            }
        }
        logger.info("权限验证:{}","验证通过");
    }
后面还有 1 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。