前后端分离 SpringSecurity在拦截器中设置responseHeader前端无法获取

在前后端分离的项目中,由于跨域限制,默认只能获取到五个HTTP响应头。要访问其他自定义头部,需在服务器端设置Access-Control-Expose-Headers。错误示例展示了未设置该头部导致前端无法获取ttt头部,而正确示例中添加了此设置,使得前端可以正常打印出该头部。
摘要由CSDN通过智能技术生成

原因分析

由于是前后端分离项目,请求涉及跨域

在前后端分离跨域的情况下,默认reponse header只能取到以下

  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

5个默认值,要想取得其他的字段需要在服务器端设置Access-Control-Expose-Headers 配置前端想要获取的header

代码示例

错误示例

后端

@Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        httpServletResponse.addHeader("ttt", "Try it out");
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

前端

Axios.interceptors.response.use(res => {
  console.log(res)
   return res.data;
}, error => {
  Message({
    message: error.response.data.message,
    type: 'error'
  })
  return Promise.reject(error.response);
})

浏览器打印结果

 正确示例

后端

@Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        httpServletResponse.addHeader("ttt", "Try it out");
        httpServletResponse.setHeader("Access-Control-Expose-Headers", "ttt");
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

浏览器打印结果

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
除了在FeignClientInterceptor接口拦截器添加Spring Security的认证信息,还可以在Feign配置添加拦截器,让Spring Security拦截其的请求。 具体实现步骤如下: 1. 创建一个Feign拦截器,用于向请求添加Spring Security的认证信息。 ```java public class AuthInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) { OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails(); template.header("Authorization", "Bearer " + details.getTokenValue()); } } } ``` 2. 创建一个Feign配置,将拦截器添加到其。同时,为了确保Feign调用能够正常进行,需要配置Feign的Encoder和Decoder。 ```java @Configuration public class FeignConfig { @Bean public RequestInterceptor requestInterceptor() { return new AuthInterceptor(); } @Bean public Encoder feignEncoder() { return new JacksonEncoder(); } @Bean public Decoder feignDecoder() { return new JacksonDecoder(); } } ``` 3. 在FeignClient注解添加Feign配置类。 ```java @FeignClient(name = "service-name", configuration = FeignConfig.class) public interface MyFeignClient { @GetMapping("/api/resource") ResponseEntity<Resource> getResource(); } ``` 这样,在调用MyFeignClient定义的方法时,Spring Security将会拦截其的请求,并进行认证和鉴权。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小叶沉迷学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值