如何在Spring Boot 2.0 Web MVC中拦截所有请求?

时间:2019-01-21 13:41:52

标签: spring spring-boot spring-mvc

我有下面的代码是从Spring Webflux应用程序改编而成的:

  import org.springframework.web.server.ServerWebExchange;
        import org.springframework.web.server.WebFilter;
        import org.springframework.web.server.WebFilterChain;

       import org.springframework.web.servlet.config.annotation.EnableWebMvc;
       import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
       import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



     @Configuration
    @EnableWebMvc

    @Slf4j

@ComponentScan(basePackages = "mypackage")
        public class WebConfig implements WebMvcConfigurer {

        @Autowired
        @Lazy
        private UserRepository userRepository;

        @Autowired
        @Lazy
        private UserService userService;




        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {


            registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
            registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");


        }


        @Bean
        public WebFilter filter() {
            return (exchange, chain) -> {
                ServerHttpRequest req = exchange.getRequest();
                String uri = req.getURI().toString();


                // detect cookie, if no cookie create new one, add pageview to user, save user
                // code removed

                return chain.filter(exchange);

            };
        }

但是由于Webfilter接口过滤器方法返回Mono,所以它不能编译。

如何在Spring Boot 2.0 Spring Web MVC应用程序(不是webflux)中拦截所有请求(跨领域问题)?

0 个答案:

没有答案