Backend Development 15 min read

Comprehensive Guide to Solving Cross-Origin Issues in Java Web Applications

This article explains the principles of cross-origin restrictions, analyzes various client‑side and server‑side solutions such as disabling browser security, using script tags, JSONP, and especially CORS with Spring Boot filters, annotations, and proxy configurations, providing complete code examples and practical deployment tips.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Comprehensive Guide to Solving Cross-Origin Issues in Java Web Applications

The article begins by introducing the problem of cross‑origin requests that arise when a front‑end AJAX call targets a Java back‑end service on a different domain, port, or protocol, and notes that existing documentation often lacks depth.

It then outlines the learning objectives: to deepen understanding of cross‑origin concepts and to explore multiple mitigation strategies.

Section 3.1 explains that browsers enforce the Same‑Origin Policy to protect user data, citing XSS and CSRF as typical attacks, and illustrates why cross‑origin requests are blocked.

Section 3.2 discusses client‑side workarounds. It shows how launching Chrome with --disable-web-security disables the restriction (though impractical for end users), and how dynamically inserting a <script> tag can fetch resources from other domains (JSONP), but requires the server to wrap responses in a callback function.

Section 3.3 introduces JSONP in detail, providing jQuery examples that set dataType: "jsonp" and jsonp: "callback" , and explains how the server must return JavaScript invoking the supplied callback.

Section 3.4 presents the standard CORS solution. It explains that adding the Access-Control-Allow-Origin header to responses enables simple requests, while non‑simple requests trigger a pre‑flight OPTIONS request that must be handled by the server. The article provides a Spring Boot filter implementation that adds the necessary CORS headers, including handling of credentials, allowed methods, allowed headers, and caching of pre‑flight results.

Further subsections cover handling cookies across origins by setting Access-Control-Allow-Credentials: true and echoing the request’s Origin header, as well as supporting custom request headers by mirroring the Access-Control-Request-Headers value.

The article also shows a concise way to enable CORS in Spring MVC using the @CrossOrigin annotation on controllers, which supersedes the manual filter configuration.

Finally, it discusses proxy‑based approaches. It describes forward proxy configuration on the target server (e.g., Nginx or Apache) and reverse‑proxy setup on the client side to bypass cross‑origin restrictions without modifying the target service.

In conclusion, the article summarizes that while client‑side tricks exist, the robust and standards‑compliant method is to configure CORS on the server, optionally supplemented by proxy settings when server changes are impossible.

JavaproxySpring BootCORSCross-OriginAJAXJSONP
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.