React客户端头在服务器Spring Boot 中丢失-尽管它们确实与Postman一起到达

ev7lccsx  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(164)

我有一个React客户端应用程序,它使HTTP到达服务器

  1. export const restApiGet = async (apiPath: string) => {
  2. const headers = {
  3. 'token': 'aaaa',
  4. 'postman-token': 'bbbb',
  5. };
  6. return Axios.get(`${apiUrl}${apiPath}`, {
  7. withCredentials: true, // Include cookies in the request
  8. headers: headers
  9. }
  10. );
  11. };

字符串
请求到达服务器,服务器是Sping Boot Java
但在我的服务器我有一个过滤器,我需要读取令牌头

  1. @Component
  2. @WebFilter
  3. public class UserIdFilter implements Filter {
  4. //TODO add filter for all response if object has userId column it must match the userId of the token
  5. private final AbstractCacheService abstractCacheService;
  6. public UserIdFilter(AbstractCacheService abstractCacheService) {
  7. this.abstractCacheService = abstractCacheService;
  8. }
  9. @Override
  10. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  11. HttpServletRequest httpRequest = (HttpServletRequest) request;
  12. }


但在服务器上,当我试图获得其空头
如果我在Chrome上查看请求-我将头从token转换为Token。但从头中阅读Token也不会带来任何结果
如果我复制Chrome请求到 Postman 的标题到达 Postman
我已经在etc host-windows-www.example.com上设置了localhost,我也调用localhost。127.0.0.1
我怎么能帮助服务器看到令牌?它觉得问题是在React,因为与 Postman 一切都在工作,我也试图发挥与交叉过滤器设置-允许,但仍然是同样的问题

  1. @Configuration
  2. public class CorsConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addCorsMappings(CorsRegistry registry) {
  5. registry.addMapping("/**")
  6. .allowedOriginPatterns("*") // Allow requests from any origin
  7. .allowedMethods("*") // Allow all HTTP methods (GET, POST, PUT, DELETE, etc.)
  8. .allowedHeaders("*") // Allow all headers
  9. .exposedHeaders("*") // Expose all headers
  10. .allowCredentials(true);//.allowedOriginPatterns("*"); // Allow credentials (e.g., cookies)
  11. }

z3yyvxxp

z3yyvxxp1#

通过添加此cors过滤器设置修复

  1. import org.springframework.context.annotation.Configuration;
  2. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  3. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  4. @Configuration
  5. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  6. @Override
  7. protected void configure(HttpSecurity http) throws Exception {
  8. http.headers().cacheControl();
  9. http.csrf().disable(); // Disable CSRF
  10. http.cors();
  11. }
  12. }

字符串

展开查看全部

相关问题