springboot接口越过拦截器的三种方式-创新互联

1:

南浔网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联公司2013年开创至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

第一种,添加排除路径,这里可以添加多个,也可以使用正则表达式

第二种,标记路径 所有包含.api的接口直接放行

第三种比较高级,自定义注解放行拦截器

@Target(value = {ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CrossAuth {
}

首先自定义权限注解

package com.example.pidog.interceptor;

import com.example.pidog.annotation.authority.Admin;
import com.example.pidog.annotation.authority.AuthUtil;
import com.example.pidog.annotation.authority.CrossAuth;
import com.example.pidog.exception.RoleException;
import com.example.pidog.threadLocal.UserThreadLocal;
import io.jsonwebtoken.SignatureException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Resource
    private AuthUtil authUtil;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws SignatureException {

        if (handler instanceof HandlerMethod) {
            Method method = ((HandlerMethod) handler).getMethod();
            if (method.isAnnotationPresent(CrossAuth.class)) {
                return true;
            }
        }

        //这里写入token验证逻辑
       
    }

}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页名称:springboot接口越过拦截器的三种方式-创新互联
文章源于:http://ybzwz.com/article/desegs.html