package me.zbl.fullstack.config;

import me.zbl.fullstack.interceptor.UserAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

/**
 * WEB MVC 配置
 */
@Configuration
public class WebConfAdapter extends WebMvcConfigurerAdapter {

  private UserAuthenticationInterceptor securityInterceptor;

  @Autowired
  public WebConfAdapter(UserAuthenticationInterceptor securityInterceptor) {
    super();
    this.securityInterceptor = securityInterceptor;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    //后台登录拦截器拦截路径
    registry.addInterceptor(securityInterceptor)
            .addPathPatterns("/admin/**")
            .excludePathPatterns("/adminlogin/**");
  }

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    super.addViewControllers(registry);
    //主页
    registry.addViewController("/").setViewName("index");
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //配置静态资源路径
    registry.addResourceHandler("/**").addResourceLocations("classpath:static/");
  }

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    converters.add(responseBodyConverter());
  }

  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    super.configureContentNegotiation(configurer);
    configurer.favorPathExtension(false);
  }

  @Override
  public Validator getValidator() {
    return super.getValidator();
  }

  @Bean
  public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowCredentials(true);
    configuration.addAllowedOrigin("*");
    configuration.addAllowedHeader("*");
    configuration.setAllowedMethods(Arrays.asList("GET", "PUT", "POST", "DELETE"));
    source.registerCorsConfiguration("/**", configuration);
    return new CorsFilter(source);
  }

  @Bean
  public HttpMessageConverter<String> responseBodyConverter() {
    //编码
    StringHttpMessageConverter converter = new StringHttpMessageConverter(
            Charset.forName("UTF-8"));
    return converter;
  }
}

package me.zbl.fullstack.config;

import me.zbl.fullstack.interceptor.UserAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

/**
 * WEB MVC 配置
 */
@Configuration
public class WebConfAdapter extends WebMvcConfigurerAdapter {

  private UserAuthenticationInterceptor securityInterceptor;

  @Autowired
  public WebConfAdapter(UserAuthenticationInterceptor securityInterceptor) {
    super();
    this.securityInterceptor = securityInterceptor;
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    //后台登录拦截器拦截路径
    registry.addInterceptor(securityInterceptor)
            .addPathPatterns("/admin/**")
            .excludePathPatterns("/adminlogin/**");
  }

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    super.addViewControllers(registry);
    //主页
    registry.addViewController("/").setViewName("forward:/index");
  }

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //配置静态资源路径
    registry.addResourceHandler("/**").addResourceLocations("classpath:static/");
  }

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    converters.add(responseBodyConverter());
  }

  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    super.configureContentNegotiation(configurer);
    configurer.favorPathExtension(false);
  }

  @Override
  public Validator getValidator() {
    return super.getValidator();
  }

  @Bean
  public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowCredentials(true);
    configuration.addAllowedOrigin("*");
    configuration.addAllowedHeader("*");
    configuration.setAllowedMethods(Arrays.asList("GET", "PUT", "POST", "DELETE"));
    source.registerCorsConfiguration("/**", configuration);
    return new CorsFilter(source);
  }

  @Bean
  public HttpMessageConverter<String> responseBodyConverter() {
    //编码
    StringHttpMessageConverter converter = new StringHttpMessageConverter(
            Charset.forName("UTF-8"));
    return converter;
  }
}

修改主页 Mapping 转发到 /index