接口开发-拦截器

因为时间问题,直接说重点了。关于什么是拦截器?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);
    }
}

 

经过上面的设置,接口的跨域访问就解决了,关于用户鉴权部分,注释掉了,有兴趣的可以研究一下。  

 

转载于:https://www.cnblogs.com/univalsoft-mobile-team/p/10232460.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值