SpringBoot 配置攔截器方式

 一、基於URL實現的攔截器:

 

/**
 * @description:常量類
 * @author: Lxq
 * @date: 2020/5/25 8:58
 */
public class Const {

    /**
     * 不驗證URL anon:不驗證/authc:受控制的
     */
    public static final String NO_INTERCEPTOR_PATH = ".*/((.css)|(.js)|(images)|(login)|(anon)).*";
}
/**
 * @description: 基於URL實現攔截器
 * @author: Lxq
 * @date: 2020/5/25 8:41
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    /**
     * 在請求之前調用
     * 基於URL實現的攔截器
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 請求頭中是否帶token
        String token = request.getHeader("token");
        String path = request.getServletPath();
        if (path.matches(Const.NO_INTERCEPTOR_PATH)) {
            // 不需要過濾的請求
            return true;
        }else {
            // 攔截器要做的是,判斷令牌,權限等信息
            System.out.println("====================================");
            return true;
        }
    }
}

 二、基於註解的攔截器

1.創建註解

/**
 * @description:在需要登錄驗證的Controller的方法上使用此註解
 * @author: Lxq
 * @date: 2020/5/25 9:22
 */
@Target({ElementType.METHOD})// 可用在方法名上
@Retention(RetentionPolicy.RUNTIME)// 運行時有效
public @interface LoginRequired {
}

2.創建攔截器

/**
 * @description: 基於註解的攔截器
 * @author: Lxq
 * @date: 2020/5/25 9:20
 */
public class AuthorityInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 如果不是映射到方法直接通過
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        // 方法註解級攔截器
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        // 判斷接口是否需要登錄
        LoginRequired methodAnnotation = method.getAnnotation(LoginRequired.class);
        // 有 @LoginRequired 註解,需要認證
        if (methodAnnotation != null) {
            // 這寫你攔截需要做的事情,比如取緩存,SESSION,權限判斷等
            System.out.println("====================================");
            return true;
        }
        return true;
    }
}

三、把攔截器添加到配置中,相當於SpringMVC時的配置文件


/**
 * @description: 將攔截器添加到配置中
 * @author: Lxq
 * @date: 2020/5/25 9:05
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 攔截所有請求,通過判斷是否有 @LoginRequired 註解 決定是否需要登錄
        registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**");
    }

    @Bean
    public LoginInterceptor LoginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public AuthorityInterceptor AuthorityInterceptor() {
        return new AuthorityInterceptor();
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章