springboot+vue-限制接口调用(自定义注解)

/**
 * 想要权限拦截的接口就加上这个注解
 */
/**
 *
 */
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableAuth {
}

编写实现 ApplicationContextAware 的类:

Spring容器已启动就执行 setApplicationContext 方法

package com.bscz.agm.service.utils.permissions;



import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 项目一启动,就调用这个方法获取所有需要权限拦截的接口,放到checkApis中
 */

@Component
@Configuration
public class ApiAuthDataInit implements ApplicationContextAware {

    /** 存放需要权限拦截的接口uri */
    public static List<String> checkApis = new ArrayList<>();

    /**
     * 获取所有带有@RestController注解的类,
     * 并获取该类下所有带有@EnableAuth注解的方法,
     * 获取该方法@RequestMapping的uri路径,
     * 将uri存入checkApis中
     */

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        Map<String, Object> beanMap = ctx.getBeansWithAnnotation(RestController.class);
        if (beanMap != null) {
            for (Object bean : beanMap.values()) {
                Class<?> clz = bean.getClass();
                Method[] methods = clz.getMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(EnableAuth.class)) {
                        String uri = getApiUri(clz, method);
                        System.err.println(uri);
                        checkApis.add(uri);
                    }
                }
            }
        }
    }

    private String getApiUri(Class<?> clz, Method method) {
        StringBuilder uri = new StringBuilder();
        uri.append(clz.getAnnotation(RequestMapping.class).value()[0]);
        if (method.isAnnotationPresent(GetMapping.class)) {
            uri.append(method.getAnnotation(GetMapping.class).value()[0]);
        } else if (method.isAnnotationPresent(PostMapping.class)) {
            uri.append(method.getAnnotation(PostMapping.class).value()[0]);
        } else if (method.isAnnotationPresent(PutMapping.class)) {
            uri.append(method.getAnnotation(PutMapping.class).value()[0]);
        }else if (method.isAnnotationPresent(DeleteMapping.class)) {
            uri.append(method.getAnnotation(DeleteMapping.class).value()[0]);
        }else if (method.isAnnotationPresent(RequestMapping.class)) {
            uri.append(method.getAnnotation(RequestMapping.class).value()[0]);
        }
        return uri.toString();
    }

}

编写api拦截器:

package com.bscz.agm.service.utils.permissions;
/**
 * 编写api拦截器
 */

import com.bscz.agm.dto.CarsDto;
import com.bscz.agm.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@WebFilter(urlPatterns = "/*", filterName = "apiFilter")
public class ApiFilter implements Filter {
    @Autowired
    private  RedisService redisService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        String authorization = req.getHeader("Authorization");
        // 判断checkApis中是否包含当前请求的uri
        if (ApiAuthDataInit.checkApis.contains(req.getRequestURI())) {
           //里面是要编写的拦截需求业务
           //如果想拦截直接返回return;
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }

}

参考博客:https://www.cnblogs.com/tangzhe/p/9229415.html

 

 

 

                            ---------------长按二维码关注程序媛小姐姐公众号有更多彩蛋哦---------------

                                            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值