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

ev7lccsx  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(60)

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

export const restApiGet = async (apiPath: string) => {
    const headers  = {
      'token': 'aaaa',
      'postman-token': 'bbbb',
    };
    return Axios.get(`${apiUrl}${apiPath}`, {
      withCredentials: true, // Include cookies in the request
      headers: headers
    }
    );
  };

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

@Component
@WebFilter
public class UserIdFilter implements Filter {

    //TODO add filter for all response if object has userId column it must match the userId of the token
    private final AbstractCacheService abstractCacheService;

    public UserIdFilter(AbstractCacheService abstractCacheService) {
        this.abstractCacheService = abstractCacheService;
    }
    

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
       
}


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

@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
            .allowedOriginPatterns("*") // Allow requests from any origin
            .allowedMethods("*") // Allow all HTTP methods (GET, POST, PUT, DELETE, etc.)
            .allowedHeaders("*") // Allow all headers
            .exposedHeaders("*") // Expose all headers
            .allowCredentials(true);//.allowedOriginPatterns("*"); // Allow credentials (e.g., cookies)

}

z3yyvxxp

z3yyvxxp1#

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

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().cacheControl();
        http.csrf().disable(); // Disable CSRF
        http.cors();
    }
}

字符串

相关问题