AppConfiguration.java 4.0 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Q
qiaozhanwei 已提交
17
package org.apache.dolphinscheduler.api.configuration;
L
ligang 已提交
18

Q
qiaozhanwei 已提交
19
import org.apache.dolphinscheduler.api.interceptor.LoginHandlerInterceptor;
L
ligang 已提交
20 21
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
L
lidongdai 已提交
22
import org.springframework.web.servlet.LocaleResolver;
L
lidongdai 已提交
23
import org.springframework.web.servlet.config.annotation.*;
L
lidongdai 已提交
24 25 26 27 28
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.util.Locale;

L
ligang 已提交
29 30 31 32 33

/**
 * application configuration
 */
@Configuration
34
public class AppConfiguration implements WebMvcConfigurer {
L
ligang 已提交
35 36 37

  public static final String LOGIN_INTERCEPTOR_PATH_PATTERN = "/**/*";
  public static final String LOGIN_PATH_PATTERN = "/login";
S
sky 已提交
38
  public static final String REGISTER_PATH_PATTERN = "/users/register";
L
ligang 已提交
39
  public static final String PATH_PATTERN = "/**";
L
lidongdai 已提交
40 41
  public static final String LOCALE_LANGUAGE_COOKIE = "language";
  public static final int COOKIE_MAX_AGE = 3600;
L
ligang 已提交
42 43 44 45 46 47 48


  @Bean
  public LoginHandlerInterceptor loginInterceptor() {
    return new LoginHandlerInterceptor();
  }

L
lidongdai 已提交
49

L
lidongdai 已提交
50 51
  /**
   * Cookie
B
bao liang 已提交
52
   * @return local resolver
L
lidongdai 已提交
53
   */
L
lidongdai 已提交
54
  @Bean(name = "localeResolver")
L
lidongdai 已提交
55 56
  public LocaleResolver localeResolver() {
    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
L
lidongdai 已提交
57 58
    localeResolver.setCookieName(LOCALE_LANGUAGE_COOKIE);
    /** set default locale **/
L
lidongdai 已提交
59
    localeResolver.setDefaultLocale(Locale.US);
L
lidongdai 已提交
60 61
    /** set cookie max age **/
    localeResolver.setCookieMaxAge(COOKIE_MAX_AGE);
L
lidongdai 已提交
62 63 64 65 66 67
    return localeResolver;
  }

  @Bean
  public LocaleChangeInterceptor localeChangeInterceptor() {
    LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
L
lidongdai 已提交
68
    /**  **/
L
lidongdai 已提交
69
    lci.setParamName("language");
L
lidongdai 已提交
70 71 72 73 74 75 76 77 78

    return lci;
  }


  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    //i18n
    registry.addInterceptor(localeChangeInterceptor());
L
lidongdai 已提交
79

S
sky 已提交
80
    registry.addInterceptor(loginInterceptor()).addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN).excludePathPatterns(LOGIN_PATH_PATTERN, REGISTER_PATH_PATTERN, "/swagger-resources/**", "/webjars/**", "/v2/**", "/doc.html", "*.html", "/ui/**");
L
lidongdai 已提交
81 82 83 84 85
  }


  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
L
lidongdai 已提交
86
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
L
lidongdai 已提交
87 88
    registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
89
    registry.addResourceHandler("/ui/**").addResourceLocations("file:ui/");
L
lidongdai 已提交
90 91
  }

92 93 94 95 96 97
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/ui/").setViewName("forward:/ui/index.html");
    registry.addViewController("/").setViewName("forward:/ui/index.html");
  }

L
ligang 已提交
98 99 100 101 102 103 104 105 106
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping(PATH_PATTERN).allowedOrigins("*").allowedMethods("*");
  }


  /**
   * Turn off suffix-based content negotiation
   *
B
bao liang 已提交
107
   * @param configurer configurer
L
ligang 已提交
108 109 110 111 112
   */
  @Override
  public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false);
  }
L
lidongdai 已提交
113 114 115 116




L
ligang 已提交
117
}