springboot跨域设置

springboot提供了两种跨域方式

  • 实现WebMvcConfigurer接口的addCorsMappings方法
  • FilterRegistrationBean添加CorsFilter

方式一

1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.maxAge(3600);
}

}

方式二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
public class WebConfig {

@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOriginPattern("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(new CorsFilter(source));
bean.setName("corsFilter");
return bean;
}

}

区别

分使用场景

假如你用了servletfilter,并filter前提返回了请求结果,用FilterRegistrationBean方式才行,因为此时请求还没到WebMvcConfigurer处理就已经返回了。

没这种情况两者行为一样。

方式三

跨域无非就是在header里写入控制跨域相关的字段信息,自己实现Filter或者Interceptor就行了。

maven项目添加第三方jar库

第三方的sdk没在maven公共仓库里,需要下载jar集成到maven项目里

把jar文件拷贝到项目里

在根目录或resources创建lib目录,把jar文件拷贝进去

修改pom.xml文件,添加依赖

1
2
3
4
5
6
7
8

<dependency>
<groupId>com.example</groupId>
<artifactId>opensdk</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${pom.basedir}/src/main/resources/lib/opensdk.jar</systemPath>
</dependency>

修改spring-boot-maven-plugin

1
2
3
4
5
6
7
8
9
10
11
12

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>

刷新maven或者重新打开项目

windows下hadoop配置

hadoop不支持windows,做为开发环境通过配置是可以使用部分功能的

下载hadoop

下载指定版本https://archive.apache.org/dist/hadoop/common/,下载linux使用的xxx.tar.gz,解压到指定目录

解压过程中会提示错误,看提示信息是文件链接的问题,不用管它。

配置环境变量

环境变量里新增一条记录:

变量名:HADDOOP_HOME
变量值:D:\hadoop-2.8.1(解压目录)

把bin目录添加到PATH

也是就%HADDOOP_HOME%\bin

java安装

如果没有java去安装java

安装winutils

windows下需要安装这个东西,下载地是https://github.com/cdarlint/winutils ,找到对应的版本下载之后把所有文件放hadoop的bin目录下就行了

其它

命令行运行下hadoop,没提示错误就说明成功了