1. 程式人生 > >介面開發-攔截器

介面開發-攔截器

因為時間問題,直接說重點了。關於什麼是攔截器?SpringBoot應該怎麼整合?攔截器能做什麼用,請自行百度。

通過攔截器,我們要解決兩大問題,第一、跨域訪問;第二、使用者鑑權;

 

一、檔案目錄

 

二、ApiInterceptor

package com.univalsoft.common.interceptor;

import com.univalsoft.common.model.APIResponse;
import com.univalsoft.tools.HttpTools;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;


public class ApiInterceptor implements HandlerInterceptor {

    //
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }


    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
            throws Exception {
    }

    // 攔截器
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 設定跨域訪問
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");

        return true;

//        // 其他業務判斷
//        String[] noAuthApis = {
//                "/api/account/login", // 登入
//        };
//
//        String requestURI = request.getRequestURI();
//        if (!Arrays.asList(noAuthApis).contains(requestURI)) {
//            System.out.println("需要驗證");
//
//            boolean canRequest = true;
//            if (canRequest) {
//                return true;
//            } else {
//                APIResponse error = new APIResponse();
//                error.fail(APIResponse.ERROR_AUTH_FAIL, null);
//                HttpTools.sendJsonMessage(response, error);
//                return false;
//            }
//        }
//
//        System.out.println("攔截器執行完畢");
//        return true;
    }
}

  

三、ApiInterceptorConf

package com.univalsoft.common.interceptor;

import com.univalsoft.tools.PropertiesTools;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class ApiInterceptorConf extends WebMvcConfigurationSupport {

    @Bean
    public HandlerInterceptor apiInterceptor() {
        return new ApiInterceptor();

    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // 註冊自己的攔截器
        // 攔截所有請求
        registry.addInterceptor(apiInterceptor()).addPathPatterns("/api/**");

        super.addInterceptors(registry);
    }


    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        // 介面文件
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        // 圖片服務
        String _imagePath = PropertiesTools.applicationProperty("app.image.path");
        registry.addResourceHandler("/image/**")
                .addResourceLocations("file:" + _imagePath);

        // APP H5
        registry.addResourceHandler("/app/**")
                .addResourceLocations("classpath:/public/app/");

        super.addResourceHandlers(registry);
    }
}

 

經過上面的設定,介面的跨域訪問就解決了,關於使用者鑑權部分,註釋掉了,有興趣的可以研究一下。