拦截在Spring Boot中调用RestController

时间:2018-05-19 10:57:31

标签: spring spring-boot

我看过类似的问题:

how to intercept all requests in spring REST controllers?

spring boot adding http request interceptors

仍然无法找到解决方案。 我有一个Spring Boot应用程序,它运行一堆Rest控制器,并希望在请求传递给控制器​​之前拦截调用并执行一些业务逻辑。我有这个:

我的Sprint Boot应用程序类:

package com.amazonaws.lambda.keefinty.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication(scanBasePackages="com.amazonaws.lambda.keefinty.controllers")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我的配置类在同一个包中一个应用程序:

package com.amazonaws.lambda.keefinty.application;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.amazonaws.lambda.keefinty.interceptors.MyInterceptor;

@Configuration
public class MyConfiguration extends WebMvcConfigurerAdapter  {

    @Bean
    public MyInterceptor getInterceptor() {
        return new MyInterceptor();
    }

    @Override
    public void addInterceptors (InterceptorRegistry registry) {
        System.out.println("\n\nAdding interceptors\n\n");
        registry.addInterceptor(getInterceptor()).addPathPatterns("/**");
    }
}

最后是拦截器类:

package com.amazonaws.lambda.keefinty.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class MyInterceptor extends HandlerInterceptorAdapter {

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

        System.out.print("\\n\\nIn MyInterceptor.preHandle\\n\\n");
        String token = request.getParameter("token");

        if (StringUtils.isBlank(token)) {
            throw new Exception("Invalid User Id or Password. Please try again.");
        }

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception exception) throws Exception {
        // TODO Auto-generated method stub
        System.out.print("\n\nIn MyInterceptor.afterCompletion\\n\\n");
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
        System.out.print("\\n\\nIn MyInterceptor.postHandle\\n\\n");
    }
}

配置类似乎永远不会被调用来注册拦截器,也不会调用MyInterceptor。

1 个答案:

答案 0 :(得分:2)

通过从@SpringBootApplication(scanBasePackages="com.amazonaws.lambda.keefinty.controllers")包中声明com.amazonaws.lambda.keefinty.controllers仅注释的组件,将发现它的子包。

MyConfiguration类位于com.amazonaws.lambda.keefinty.application包中,该包不属于声明的组件扫描包。

解决此问题的一种方法是从scanBasePackages声明中删除@SpringBootApplication参数。这将允许MyConfiguration被组件扫描,因为默认情况下,声明@SpringBootApplication的包是组件扫描的。