美文网首页
spring boot + security 跨域

spring boot + security 跨域

作者: yexue | 来源:发表于2018-10-23 10:53 被阅读119次

原文链接:yexuejc-blog
直接上代码,留作记录

网上普通的跨域设置可以让普通请求支持跨域,但是遇到security 的登录/login 就不行了

贴出核心代码

@EnableWebSecurity(debug = false)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//其他业务代码
....

@Bean
    public CorsConfigurationSource corsConfigurationSource() {
        final CorsConfiguration configuration = new CorsConfiguration();
        //指定允许跨域的请求(*所有):http://wap.ivt.guansichou.com
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));
        // setAllowCredentials(true) is important, otherwise:
        // The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
        configuration.setAllowCredentials(true);
        // setAllowedHeaders is important! Without it, OPTIONS preflight request
        // will fail with 403 Invalid CORS request
        configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "X-User-Agent", "Content-Type"));
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}


    /**
     * 关键.cors()
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .cors()
                .and().servletApi().disable()
                .requestCache().disable();
               
        //其他业务代码
         ....
    }

//其他业务代码
....
}

参考:spring-boot-realworld-example-app

相关文章

  • 2018-12-11

    spring security 的跨域问题 spring security跨域设置 在spring-sercuri...

  • 跨域配置

    SpringBoot跨域配置 我们的后端使用Spring Boot。Spring Boot跨域非常简单,只需书写以...

  • spring boot + security 跨域

    原文链接:yexuejc-blog直接上代码,留作记录 网上普通的跨域设置可以让普通请求支持跨域,但是遇到secu...

  • Spring boot + Spring security 跨域

    问题场景: 在spring securit配置类中配置了authenticationEntryPoint()当用户...

  • 2019-04-01

    Spring Boot轻松跨域:Spring Boot中采用注解轻松实现跨域的一个基础例子 1.项目结构,conf...

  • Shiro、Spring Security整合

    Shiro、Spring Security Spring Boot 整合 Spring Security 快速上⼿...

  • SpringBoot+Vue数据交互

    后端框架Spring Boot,前端框架vue 1. 跨域+携带cookie 跨域-携带cookie 2. 拦截器...

  • Spring Boot 跨域访问

    如何在 Spring Boot 中配置跨域访问呢?Spring Boot 提供了对 CORS 的支持,您可以实现W...

  • Spring Boot整合Spring Security

    Spring Boot对Spring Security的支持 Spring Boot针对Spring Securi...

  • Spring Security应用详解

    一、Spring Security集成SpringBoot Spring Boot提供spring-boot-st...

网友评论

      本文标题:spring boot + security 跨域

      本文链接:https://www.haomeiwen.com/subject/azmozftx.html