【问题标题】:Spring CORS Support Custom Token Auth SolutionSpring CORS 支持自定义 Token Auth 解决方案
【发布时间】:2016-03-17 14:08:34
【问题描述】:

我正在尝试实施自定义令牌身份验证解决方案。 这里的问题是我无法运行跨域请求。 尝试了很多不同的配置,但似乎都没有用。

**Configure Http Security** 

  .csrf()
    .disable() //SCRF is disables
  .exceptionHandling()
    .authenticationEntryPoint(this.unauthorizedHandler)
    .and()
  .sessionManagement()
    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and()
  .authorizeRequests()
    .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
    .anyRequest().authenticated();

// Custom JWT based authentication


httpSecurity
  .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

使用自定义 Token 配置 Cors 过滤器的实现。

**CorsFilter DoFilter **


HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;

response.setHeader("Access-Control-Allow-Origin", "*"); //Allow origin activated
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, " + tokenHeader);
if (request.getMethod().equals("OPTIONS")) {
    response.setStatus(HttpServletResponse.SC_OK);
} else {
    chain.doFilter(request, response);
}

我开始的代码可以在这里找到: https://github.com/brahalla/Cerberus

有什么线索吗?

【问题讨论】:

    标签: spring cors token


    【解决方案1】:

    假设您的过滤器没问题,您需要配置 CORS - https://spring.io/guides/gs/rest-service-cors/#_enabling_cors。您的配置中似乎没有任何这些

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多