package com.inkasso.auth.config;

import com.inkasso.auth.interceptor.AuthenticationInterceptor;
import com.inkasso.auth.interceptor.InterceptorInit;
import com.inkasso.auth.interceptor.RateLimiterInterceptor;
import com.inkasso.auth.service.impl.BackEndUserCheckAuthentication;
import com.inkasso.auth.service.impl.TokenCheckAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张超
 * @date 2021/11/3
 */
@Configuration
public class AuthenticationConfig {


  @Bean
  public TokenCheckAuthentication tokenCheckAuthentication() {

    return new TokenCheckAuthentication();
  }


  @Bean
  public BackEndUserCheckAuthentication backEndUserCheckAuthentication() {

    return new BackEndUserCheckAuthentication();
  }

  @Bean
  public RateLimiterInterceptor rateLimiterInterceptor(){

    return new RateLimiterInterceptor();
  }


  @Bean
  @ConditionalOnProperty(prefix = "kafubao",name="authentication",havingValue = "true",matchIfMissing = false)
  public AuthenticationInterceptor authenticationInterceptor() {

    return new AuthenticationInterceptor();
  }

  @Bean
  @ConditionalOnProperty(prefix = "kafubao",name="authentication",havingValue = "true",matchIfMissing = false)
  public InterceptorInit interceptorInit(@Autowired AuthenticationInterceptor authenticationInterceptor,
                                         @Autowired RateLimiterInterceptor rateLimiterInterceptor) {
    List<HandlerInterceptor> list = new ArrayList<>();
    list.add(authenticationInterceptor);
    list.add(rateLimiterInterceptor);
    return new InterceptorInit(list);
  }



}
