zoukankan      html  css  js  c++  java
  • Spring Boot 跨域

    一:使用@CrossOrigin注解

        @CrossOrigin
        @RequestMapping(value = "/get")
        public HashMap<String, Object> get(@RequestParam String name) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("title", "hello world");
            map.put("name", name);
            return map;
        }

    二:配置跨域类

    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    import org.springframework.web.filter.CorsFilter;
    
    import java.util.Collections;
    
    /**
     * 跨域请求过滤器
     *
     */
    @Configuration
    public class OriginFilter {
        @SuppressWarnings("unchecked")
        @Bean
        public FilterRegistrationBean corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.setAllowCredentials(true);
            corsConfiguration.setAllowedOrigins(Collections.singletonList(CorsConfiguration.ALL));
            corsConfiguration.setAllowedHeaders(Collections.singletonList(CorsConfiguration.ALL));
            corsConfiguration.setAllowedMethods(Collections.singletonList(CorsConfiguration.ALL));
            corsConfiguration.addExposedHeader("Authorization");
            source.registerCorsConfiguration("/**", corsConfiguration);
            FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
            bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
            return bean;
        }
    }
  • 相关阅读:
    周练2
    周练1
    周赛6(28)
    django中的orm:
    crm项目包含django创建虚拟环境:
    crm项目建表(django自带认证、分页、插件功能):
    自动化测试js代码打印类名:
    pages框架之豆瓣:
    mybatis反向生成实体类、dao层以及映射文件
    mybatis反向生成实体类、dao层以及映射文件
  • 原文地址:https://www.cnblogs.com/dcrenl/p/14103739.html
Copyright © 2011-2022 走看看