47 lines
3.6 KiB
HTML
47 lines
3.6 KiB
HTML
|
<p>一:使用@CrossOrigin注解</p>
|
||
|
<div class="cnblogs_code">
|
||
|
<pre><span style="color: #000000;"> @CrossOrigin
|
||
|
@RequestMapping(value </span>= "/get"<span style="color: #000000;">)
|
||
|
</span><span style="color: #0000ff;">public</span> HashMap<String, Object><span style="color: #000000;"> get(@RequestParam String name) {
|
||
|
HashMap</span><String, Object> map = <span style="color: #0000ff;">new</span> HashMap<String, Object><span style="color: #000000;">();
|
||
|
map.put(</span>"title", "hello world"<span style="color: #000000;">);
|
||
|
map.put(</span>"name"<span style="color: #000000;">, name);
|
||
|
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> map;
|
||
|
}</span></pre>
|
||
|
</div>
|
||
|
<p>二:配置跨域类</p>
|
||
|
<div class="cnblogs_code">
|
||
|
<pre><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||
|
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.context.annotation.Bean;
|
||
|
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.context.annotation.Configuration;
|
||
|
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.core.Ordered;
|
||
|
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.web.cors.CorsConfiguration;
|
||
|
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||
|
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.springframework.web.filter.CorsFilter;
|
||
|
|
||
|
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.Collections;
|
||
|
|
||
|
</span><span style="color: #008000;">/**</span><span style="color: #008000;">
|
||
|
* 跨域请求过滤器
|
||
|
*
|
||
|
</span><span style="color: #008000;">*/</span><span style="color: #000000;">
|
||
|
@Configuration
|
||
|
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> OriginFilter {
|
||
|
@SuppressWarnings(</span>"unchecked"<span style="color: #000000;">)
|
||
|
@Bean
|
||
|
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> FilterRegistrationBean corsFilter() {
|
||
|
UrlBasedCorsConfigurationSource source </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> UrlBasedCorsConfigurationSource();
|
||
|
CorsConfiguration corsConfiguration </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> CorsConfiguration();
|
||
|
corsConfiguration.setAllowCredentials(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);
|
||
|
corsConfiguration.setAllowedOrigins(Collections.singletonList(CorsConfiguration.ALL));
|
||
|
corsConfiguration.setAllowedHeaders(Collections.singletonList(CorsConfiguration.ALL));
|
||
|
corsConfiguration.setAllowedMethods(Collections.singletonList(CorsConfiguration.ALL));
|
||
|
corsConfiguration.addExposedHeader(</span>"Authorization"<span style="color: #000000;">);
|
||
|
source.registerCorsConfiguration(</span>"/**"<span style="color: #000000;">, corsConfiguration);
|
||
|
FilterRegistrationBean bean </span>= <span style="color: #0000ff;">new</span> FilterRegistrationBean(<span style="color: #0000ff;">new</span><span style="color: #000000;"> CorsFilter(source));
|
||
|
bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||
|
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> bean;
|
||
|
}
|
||
|
}</span></pre>
|
||
|
</div>
|
||
|
<p> </p>
|