我正在用React和Redux构建一个前端应用程序,我正在使用axios来执行我的请求。我想访问响应头中的所有字段。在我的浏览器中,我可以检查标题,我可以看到我需要的所有字段都是存在的(如令牌,uid等…),但当我调用

const request = axios.post(`${ROOT_URL}/auth/sign_in`, props);
request.then((response)=>{
  console.log(response.headers);
});

我只是

Object {content-type: "application/json; charset=utf-8", cache-control: "max-age=0, private, must-revalidate"}

这里我的浏览器网络选项卡,正如你可以看到的所有其他领域都存在。

最好成绩。


当前回答

我也面临着同样的问题。我在我的WebSecurity.java中做了这个,它是关于CORS配置中的setExposedHeaders方法。

@Bean
CorsConfigurationSource corsConfigurationSource() {

    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowCredentials(true);
    configuration.setAllowedOrigins(Arrays.asList(FRONT_END_SERVER));
    configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));
    configuration.setAllowedHeaders(Arrays.asList("X-Requested-With","Origin","Content-Type","Accept","Authorization"));
    
    // This allow us to expose the headers
    configuration.setExposedHeaders(Arrays.asList("Access-Control-Allow-Headers", "Authorization, x-xsrf-token, Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, " +
            "Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"));
    
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

我希望它能奏效。

其他回答

我也面临着同样的问题。我在我的WebSecurity.java中做了这个,它是关于CORS配置中的setExposedHeaders方法。

@Bean
CorsConfigurationSource corsConfigurationSource() {

    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowCredentials(true);
    configuration.setAllowedOrigins(Arrays.asList(FRONT_END_SERVER));
    configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));
    configuration.setAllowedHeaders(Arrays.asList("X-Requested-With","Origin","Content-Type","Accept","Authorization"));
    
    // This allow us to expose the headers
    configuration.setExposedHeaders(Arrays.asList("Access-Control-Allow-Headers", "Authorization, x-xsrf-token, Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, " +
            "Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"));
    
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}

我希望它能奏效。

对于Spring Boot 2,如果你不想使用全局CORS配置,你可以通过方法或类/控制器级别使用@CrossOrigin声明和exposedHeaders属性来实现。

例如,为YourController方法添加头授权:

@CrossOrigin(exposedHeaders = "authorization")
@RestController
public class YourController {
    ...
}

对于django help

CORS_EXPOSE_HEADERS = [
        'your header'
    ]

这真的帮助了我,谢谢Nick Uraltsev的回答。

对于那些使用cors的nodejs的人:

...
const cors = require('cors');

const corsOptions = {
  exposedHeaders: 'Authorization',
};

app.use(cors(corsOptions));
...

在这种情况下,您以res.header('Authorization', ' Bearer ${token} ').send()的方式发送响应;

[扩展@vladimir所说的话]

如果你使用的是Django 和django-cors-headers允许/控制CORS, 您应该在settings.py中设置以下内容

CORS_EXPOSE_HEADERS = ['yourCustomHeader']