我有一个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)
}
型
1条答案
按热度按时间z3yyvxxp1#
通过添加此cors过滤器设置修复
字符串