/*
 * *************************************************
 * Copyright (c) 2019 QMTH. All Rights Reserved.
 * Created by Deason on 2019-05-07 11:02:18.
 * *************************************************
 */

package cn.com.qmth.examcloud.core.questions.starter.config;

import cn.com.qmth.examcloud.web.interceptor.ApiFlowLimitedInterceptor;
import cn.com.qmth.examcloud.web.interceptor.ApiStatisticInterceptor;
import cn.com.qmth.examcloud.web.interceptor.FirstInterceptor;
import cn.com.qmth.examcloud.web.redis.RedisClient;
import cn.com.qmth.examcloud.web.security.DataRuleInterceptor;
import cn.com.qmth.examcloud.web.security.RequestPermissionInterceptor;
import cn.com.qmth.examcloud.web.security.ResourceManager;
import cn.com.qmth.examcloud.web.security.RpcInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class ExamCloudWebMvcConfigurer implements WebMvcConfigurer {

    @Autowired
    private ResourceManager resourceManager;

    @Autowired
    private RedisClient redisClient;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new FirstInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new ApiFlowLimitedInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(new RpcInterceptor(resourceManager)).addPathPatterns("/**");

        RequestPermissionInterceptor permissionInterceptor = new RequestPermissionInterceptor(resourceManager, redisClient);
        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**");
        registry.addInterceptor(new ApiStatisticInterceptor()).addPathPatterns("/**");

        registry.addInterceptor(new DataRuleInterceptor(resourceManager)).addPathPatterns("/api/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowCredentials(false).allowedMethods("*")
                .maxAge(3600);
    }

}