CORS Origin with Angular

2/9/2021

I have a problem with CORS Origin on Angular ... I have my Projects deploy in kluster kubernetes on DigitalOcean.

For exampl , I could use this service : http://167.172.13.45:8889 (And it works) with Postman.

My Service in 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");
  }
}

My environment

export const environment = { production: false, categories: '/categories' };

And my proxy ...

{
    "/categories/*": {
      "target": "http://167.172.13.45:8889",
      "secure": true,
      "logLevel": "debug",
      "changeOrigin": true
    }
  }

Could anyone discover o try to help me ? Why is not working my own project ? Any Guide to use Kubernetes from digitalocean.

-- pepe
angular
digital-ocean
kubernetes

1 Answer

2/14/2021

I solve this problem adding the following class on My SpringBoot Projects.

@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 {}

}
-- pepe
Source: StackOverflow