博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CORS支持
阅读量:7061 次
发布时间:2019-06-28

本文共 1932 字,大约阅读时间需要 6 分钟。

  hot3.png

注解在controller或者方法上,不写任何参数默认允许所有 origins。

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)@RestController@RequestMapping("/account")public class AccountController {	@RequestMapping("/{id}")	public Account retrieve(@PathVariable Long id) {		// ...	}	@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")	public void remove(@PathVariable Long id) {		// ...	}}

或者

@CrossOrigin(maxAge = 3600)@RestController@RequestMapping("/account")public class AccountController {	@CrossOrigin("http://domain2.com")	@RequestMapping("/{id}")	public Account retrieve(@PathVariable Long id) {		// ...	}	@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")	public void remove(@PathVariable Long id) {		// ...	}}

全局配置

@Configurationpublic class WebConfig {    @Bean    public WebMvcConfigurer corsConfigurer() {        return new WebMvcConfigurerAdapter() {            @Override            public void addCorsMappings(CorsRegistry registry) {                registry.addMapping("/success")                        .allowedOrigins("http://com.myhost:8080")//                        .allowedMethods("PUT", "DELETE")//                        .allowedHeaders("header1", "header2", "header3")//                        .exposedHeaders("header1", "header2")                        .allowCredentials(false).maxAge(3600);            }        };    }}

关于 SpringSecurity 支持 cors

除了需要配置全局CORS以外,再添加一个 cors().and()即可。

protected void configure(HttpSecurity http) throws Exception {        http                .headers()                .frameOptions()                .sameOrigin()                .and()                // disable CSRF, http basic, form login//                .csrf().disable()                // 跨域支持                .cors().and()                .authorizeRequests()                .antMatchers("/user/**").authenticated()                .anyRequest().permitAll()                ....

 

转载于:https://my.oschina.net/lemos/blog/869583

你可能感兴趣的文章
分治法排序
查看>>
【WEBAPI】常用参数传递方法总结
查看>>
UVA11825 黑客的攻击 Hackers' Crackdown 状压DP,二进制,子集枚举
查看>>
爬虫间隔抓取服务器网页
查看>>
const define区别
查看>>
Python 面向对象(初级篇)
查看>>
头指针与头结点的异同
查看>>
学习Python的捷径
查看>>
方法中(+),(-)的区分
查看>>
Django学习(五) 定义视图以及页面模板
查看>>
关于pycharm中导入自己写的模块没有智能提示的解决办法
查看>>
【转载】K-means聚类算法
查看>>
debian系统下安装ssh
查看>>
C++内联函数、函数模板之于头文件
查看>>
MAVEN学习笔记之基础(1)
查看>>
input type=file 怎么样调取用户手机照相机
查看>>
iOS LaunchImage快速设置
查看>>
海量数据面试题----分而治之/hash映射 + hash统计 + 堆/快速/归并排序
查看>>
左神算法进阶班4_1画出楼的轮廓
查看>>
力扣算法题—072编辑距离
查看>>