Browse Source

[Fix-4439][*] Fix cors problem (#4490)

* [Fix][UI] Fix warning in npm install

* [Fix-4439][*] Fix CORS problem

* [Improvement][API] Improve locale change interceptor

* [Improvement][API] Add test for locale change interceptor

* [Improvement][API] Fix code style

* [Improvement][Build] Update .gitignore
Shiwen Cheng 4 years ago
parent
commit
0b8fcac37b

+ 8 - 114
.gitignore

@@ -4,15 +4,9 @@
 .zip
 .gz
 .DS_Store
-.idea
-.idea/
-.idea/*
 .target
-.target/
-**/**/target/**
-target/*
-*/target
-*/target/*
+.idea/
+target/
 .settings
 .nbproject
 .classpath
@@ -28,123 +22,23 @@ node_modules
 npm-debug.log
 .vscode
 logs/*
+.mvn/
 .www
 t.*
+.factorypath
+Chart.lock
 yarn.lock
 package-lock.json
 config.gypi
 test/coverage
 /docs/zh_CN/介绍
 /docs/zh_CN/贡献代码.md
-/dolphinscheduler-common/src/main/resources/zookeeper.properties
+dolphinscheduler-common/src/main/resources/zookeeper.properties
+dolphinscheduler-dao/src/main/resources/dao/data_source.properties
 dolphinscheduler-alert/logs/
 dolphinscheduler-alert/src/main/resources/alert.properties_bak
 dolphinscheduler-alert/src/main/resources/logback.xml
 dolphinscheduler-server/src/main/resources/logback.xml
 dolphinscheduler-ui/dist
 dolphinscheduler-ui/node
-dolphinscheduler-ui/dist/css/common.16ac5d9.css
-dolphinscheduler-ui/dist/css/home/index.b444b91.css
-dolphinscheduler-ui/dist/css/login/index.5866c64.css
-dolphinscheduler-ui/dist/js/0.ac94e5d.js
-dolphinscheduler-ui/dist/js/0.ac94e5d.js.map
-dolphinscheduler-ui/dist/js/1.0b043a3.js
-dolphinscheduler-ui/dist/js/1.0b043a3.js.map
-dolphinscheduler-ui/dist/js/10.1bce3dc.js
-dolphinscheduler-ui/dist/js/10.1bce3dc.js.map
-dolphinscheduler-ui/dist/js/11.79f04d8.js
-dolphinscheduler-ui/dist/js/11.79f04d8.js.map
-dolphinscheduler-ui/dist/js/12.420daa5.js
-dolphinscheduler-ui/dist/js/12.420daa5.js.map
-dolphinscheduler-ui/dist/js/13.e5bae1c.js
-dolphinscheduler-ui/dist/js/13.e5bae1c.js.map
-dolphinscheduler-ui/dist/js/14.f2a0dca.js
-dolphinscheduler-ui/dist/js/14.f2a0dca.js.map
-dolphinscheduler-ui/dist/js/15.45373e8.js
-dolphinscheduler-ui/dist/js/15.45373e8.js.map
-dolphinscheduler-ui/dist/js/16.fecb0fc.js
-dolphinscheduler-ui/dist/js/16.fecb0fc.js.map
-dolphinscheduler-ui/dist/js/17.84be279.js
-dolphinscheduler-ui/dist/js/17.84be279.js.map
-dolphinscheduler-ui/dist/js/18.307ea70.js
-dolphinscheduler-ui/dist/js/18.307ea70.js.map
-dolphinscheduler-ui/dist/js/19.144db9c.js
-dolphinscheduler-ui/dist/js/19.144db9c.js.map
-dolphinscheduler-ui/dist/js/2.8b4ef29.js
-dolphinscheduler-ui/dist/js/2.8b4ef29.js.map
-dolphinscheduler-ui/dist/js/20.4c527e9.js
-dolphinscheduler-ui/dist/js/20.4c527e9.js.map
-dolphinscheduler-ui/dist/js/21.831b2a2.js
-dolphinscheduler-ui/dist/js/21.831b2a2.js.map
-dolphinscheduler-ui/dist/js/22.2b4bb2a.js
-dolphinscheduler-ui/dist/js/22.2b4bb2a.js.map
-dolphinscheduler-ui/dist/js/23.81467ef.js
-dolphinscheduler-ui/dist/js/23.81467ef.js.map
-dolphinscheduler-ui/dist/js/24.54a00e4.js
-dolphinscheduler-ui/dist/js/24.54a00e4.js.map
-dolphinscheduler-ui/dist/js/25.8d7bd36.js
-dolphinscheduler-ui/dist/js/25.8d7bd36.js.map
-dolphinscheduler-ui/dist/js/26.2ec5e78.js
-dolphinscheduler-ui/dist/js/26.2ec5e78.js.map
-dolphinscheduler-ui/dist/js/27.3ab48c2.js
-dolphinscheduler-ui/dist/js/27.3ab48c2.js.map
-dolphinscheduler-ui/dist/js/28.363088a.js
-dolphinscheduler-ui/dist/js/28.363088a.js.map
-dolphinscheduler-ui/dist/js/29.6c5853a.js
-dolphinscheduler-ui/dist/js/29.6c5853a.js.map
-dolphinscheduler-ui/dist/js/3.a0edb5b.js
-dolphinscheduler-ui/dist/js/3.a0edb5b.js.map
-dolphinscheduler-ui/dist/js/30.940fdd3.js
-dolphinscheduler-ui/dist/js/30.940fdd3.js.map
-dolphinscheduler-ui/dist/js/31.168a460.js
-dolphinscheduler-ui/dist/js/31.168a460.js.map
-dolphinscheduler-ui/dist/js/32.8df6594.js
-dolphinscheduler-ui/dist/js/32.8df6594.js.map
-dolphinscheduler-ui/dist/js/33.4480bbe.js
-dolphinscheduler-ui/dist/js/33.4480bbe.js.map
-dolphinscheduler-ui/dist/js/34.b407fe1.js
-dolphinscheduler-ui/dist/js/34.b407fe1.js.map
-dolphinscheduler-ui/dist/js/35.f340b0a.js
-dolphinscheduler-ui/dist/js/35.f340b0a.js.map
-dolphinscheduler-ui/dist/js/36.8880c2d.js
-dolphinscheduler-ui/dist/js/36.8880c2d.js.map
-dolphinscheduler-ui/dist/js/37.ea2a25d.js
-dolphinscheduler-ui/dist/js/37.ea2a25d.js.map
-dolphinscheduler-ui/dist/js/38.98a59ee.js
-dolphinscheduler-ui/dist/js/38.98a59ee.js.map
-dolphinscheduler-ui/dist/js/39.a5e958a.js
-dolphinscheduler-ui/dist/js/39.a5e958a.js.map
-dolphinscheduler-ui/dist/js/4.4ca44db.js
-dolphinscheduler-ui/dist/js/4.4ca44db.js.map
-dolphinscheduler-ui/dist/js/40.e187b1e.js
-dolphinscheduler-ui/dist/js/40.e187b1e.js.map
-dolphinscheduler-ui/dist/js/41.0e89182.js
-dolphinscheduler-ui/dist/js/41.0e89182.js.map
-dolphinscheduler-ui/dist/js/42.341047c.js
-dolphinscheduler-ui/dist/js/42.341047c.js.map
-dolphinscheduler-ui/dist/js/43.27b8228.js
-dolphinscheduler-ui/dist/js/43.27b8228.js.map
-dolphinscheduler-ui/dist/js/44.e8869bc.js
-dolphinscheduler-ui/dist/js/44.e8869bc.js.map
-dolphinscheduler-ui/dist/js/45.8d54901.js
-dolphinscheduler-ui/dist/js/45.8d54901.js.map
-dolphinscheduler-ui/dist/js/5.e1ed7f3.js
-dolphinscheduler-ui/dist/js/5.e1ed7f3.js.map
-dolphinscheduler-ui/dist/js/6.241ba07.js
-dolphinscheduler-ui/dist/js/6.241ba07.js.map
-dolphinscheduler-ui/dist/js/7.ab2e297.js
-dolphinscheduler-ui/dist/js/7.ab2e297.js.map
-dolphinscheduler-ui/dist/js/8.83ff814.js
-dolphinscheduler-ui/dist/js/8.83ff814.js.map
-dolphinscheduler-ui/dist/js/9.39cb29f.js
-dolphinscheduler-ui/dist/js/9.39cb29f.js.map
-dolphinscheduler-ui/dist/js/common.733e342.js
-dolphinscheduler-ui/dist/js/common.733e342.js.map
-dolphinscheduler-ui/dist/js/home/index.78a5d12.js
-dolphinscheduler-ui/dist/js/home/index.78a5d12.js.map
-dolphinscheduler-ui/dist/js/login/index.291b8e3.js
-dolphinscheduler-ui/dist/js/login/index.291b8e3.js.map
-dolphinscheduler-ui/dist/lib/external/
-dolphinscheduler-ui/src/js/conf/home/pages/projects/pages/taskInstance/index.vue
-/dolphinscheduler-dao/src/main/resources/dao/data_source.properties
-
+docker/build/apache-dolphinscheduler*

+ 19 - 13
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/AppConfiguration.java

@@ -17,17 +17,20 @@
 
 package org.apache.dolphinscheduler.api.configuration;
 
+import org.apache.dolphinscheduler.api.interceptor.LocaleChangeInterceptor;
 import org.apache.dolphinscheduler.api.interceptor.LoginHandlerInterceptor;
+
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
 import org.springframework.web.servlet.LocaleResolver;
 import org.springframework.web.servlet.config.annotation.*;
 import org.springframework.web.servlet.i18n.CookieLocaleResolver;
-import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
 
 import java.util.Locale;
 
-
 /**
  * application configuration
  */
@@ -39,7 +42,17 @@ public class AppConfiguration implements WebMvcConfigurer {
     public static final String REGISTER_PATH_PATTERN = "/users/register";
     public static final String PATH_PATTERN = "/**";
     public static final String LOCALE_LANGUAGE_COOKIE = "language";
-    public static final int COOKIE_MAX_AGE = 3600;
+
+    @Bean
+    public CorsFilter corsFilter() {
+        CorsConfiguration config = new CorsConfiguration();
+        config.addAllowedOrigin("*");
+        config.addAllowedMethod("*");
+        config.addAllowedHeader("*");
+        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
+        configSource.registerCorsConfiguration(PATH_PATTERN, config);
+        return new CorsFilter(configSource);
+    }
 
     @Bean
     public LoginHandlerInterceptor loginInterceptor() {
@@ -56,16 +69,14 @@ public class AppConfiguration implements WebMvcConfigurer {
         localeResolver.setCookieName(LOCALE_LANGUAGE_COOKIE);
         // set default locale
         localeResolver.setDefaultLocale(Locale.US);
-        // set cookie max age
-        localeResolver.setCookieMaxAge(COOKIE_MAX_AGE);
+        // set language tag compliant
+        localeResolver.setLanguageTagCompliant(false);
         return localeResolver;
     }
 
     @Bean
     public LocaleChangeInterceptor localeChangeInterceptor() {
-        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
-        lci.setParamName("language");
-        return lci;
+        return new LocaleChangeInterceptor();
     }
 
     @Override
@@ -94,11 +105,6 @@ public class AppConfiguration implements WebMvcConfigurer {
         registry.addViewController("/").setViewName("forward:/ui/index.html");
     }
 
-    @Override
-    public void addCorsMappings(CorsRegistry registry) {
-        registry.addMapping(PATH_PATTERN).allowedOrigins("*").allowedMethods("*");
-    }
-
     /**
      * Turn off suffix-based content negotiation
      *

+ 56 - 0
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LocaleChangeInterceptor.java

@@ -0,0 +1,56 @@
+/*
+ * 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.
+ */
+
+package org.apache.dolphinscheduler.api.interceptor;
+
+import org.apache.dolphinscheduler.api.service.BaseService;
+import org.apache.dolphinscheduler.common.Constants;
+
+import java.util.Locale;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.context.i18n.LocaleContextHolder;
+import org.springframework.lang.Nullable;
+import org.springframework.util.StringUtils;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+public class LocaleChangeInterceptor extends HandlerInterceptorAdapter {
+
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
+        Cookie cookie = BaseService.getCookie(request, Constants.LOCALE_LANGUAGE);
+        if (cookie != null) {
+            // Proceed in cookie
+            return true;
+        }
+        // Proceed in header
+        String newLocale = request.getHeader(Constants.LOCALE_LANGUAGE);
+        if (newLocale != null) {
+            LocaleContextHolder.setLocale(parseLocaleValue(newLocale));
+        }
+        return true;
+    }
+
+    @Nullable
+    protected Locale parseLocaleValue(String localeValue) {
+        return StringUtils.parseLocale(localeValue);
+    }
+
+}

+ 48 - 0
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/interceptor/LocaleChangeInterceptorTest.java

@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+package org.apache.dolphinscheduler.api.interceptor;
+
+import org.apache.dolphinscheduler.api.ApiApplicationServer;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = ApiApplicationServer.class)
+public class LocaleChangeInterceptorTest {
+
+    @Autowired
+    LocaleChangeInterceptor interceptor;
+
+    @Test
+    public void testPreHandle() {
+        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
+        // test no language
+        Assert.assertTrue(interceptor.preHandle(request, response, null));
+    }
+
+}

+ 5 - 0
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java

@@ -906,6 +906,11 @@ public final class Constants {
 
     public static final String PASSWORD_DEFAULT = "******";
 
+    /**
+     * locale
+     */
+    public static final String LOCALE_LANGUAGE = "language";
+
     /**
      * driver
      */

+ 3 - 0
dolphinscheduler-ui/package.json

@@ -3,6 +3,8 @@
   "version": "1.0.0",
   "description": "A vue.js project",
   "author": "DolphinScheduler",
+  "repository": "https://github.com/apache/incubator-dolphinscheduler",
+  "license": "Apache-2.0",
   "scripts": {
     "build": "npm run clean && cross-env NODE_ENV=production webpack --config ./build/webpack.config.prod.js",
     "dev": "cross-env NODE_ENV=development webpack-dev-server --config ./build/webpack.config.dev.js",
@@ -39,6 +41,7 @@
     "vuex-router-sync": "^5.0.0"
   },
   "devDependencies": {
+    "acorn": "^7.4.1",
     "autoprefixer": "^9.1.0",
     "babel-core": "^6.25.0",
     "babel-helper-vue-jsx-merge-props": "^2.0.2",

+ 4 - 0
dolphinscheduler-ui/src/js/module/io/index.js

@@ -84,6 +84,10 @@ io.interceptors.request.use(
           _t: Math.random()
         })
       }
+      config.headers = config.headers || {}
+      const language = cookies.get('language')
+      if (language) config.headers.language = language
+      if (sIdCookie) config.headers.sessionId = sIdCookie
       return config
     }
   }, error => {

+ 1 - 0
pom.xml

@@ -776,6 +776,7 @@
                         <include>**/api/enums/StatusTest.java</include>
                         <include>**/api/exceptions/ApiExceptionHandlerTest.java</include>
                         <include>**/api/exceptions/ServiceExceptionTest.java</include>
+                        <include>**/api/interceptor/LocaleChangeInterceptorTest.java</include>
                         <include>**/api/interceptor/LoginHandlerInterceptorTest.java</include>
                         <include>**/api/security/impl/pwd/PasswordAuthenticatorTest.java</include>
                         <include>**/api/security/impl/ldap/LdapAuthenticatorTest.java</include>