我在Angular上有一个CORS Origin的问题…我把我的项目部署在DigitalOcean上的kluster kubernetes上。
例如,我可以使用以下服务:http://167.172.13.45:8889(它可以工作)与Postman。
My Service在Angular 8中的应用
@Injectable()
export class CategoriesService {
public apiUrl: string;
private categories:string = environment.categories;
constructor(private http: HttpClient) { }
getAllCategories(page:number){
return this.http.get(this.categories+"/public/v1");
}
}
我的环境export const environment = {生产:假的,类别:"/类别">
};和我的代理…
{
"/categories/*": {
"target": "http://167.172.13.45:8889",
"secure": true,
"logLevel": "debug",
"changeOrigin": true
}
}
有谁能来帮帮我吗?为什么不是我自己的项目?Kubernetes from digitalocean.
我在我的SpringBoot项目中添加以下类来解决这个问题。
@Component
public class XClacksOverhead implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("Access-Control-Allow-Methods", "GET,POST,PATCH,DELETE,PUT,OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "*");
response.setHeader("Access-Control-Max-Age", "86400");
chain.doFilter(req, res);
}
@Override
public void destroy() {}
@Override
public void init(FilterConfig arg0) throws ServletException {}
}