Solving cross-origin issues in Java usually involves setting appropriate CORS (Cross-Origin Resource Sharing) headers. The following are specific methods and examples of using Spring Framework to solve cross-domain problems:
Suppose you have a Spring Boot application with a controller handling data requests. You need to allow pages under other domain names to access this interface.
-
Use
@CrossOrigin
annotations:@CrossOrigin
Annotations can be applied directly to controller methods to specify domains that allow cross-domain requests. Here is an example:import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DataController { @CrossOrigin(origins = "http://example.com") @GetMapping("/data") public String getData() { return "This is some data from the server."; } }
In the example above, only
http://example.com
pages from the domain can access/data
the interface. -
Use global CORS configuration:
You can also configure CORS globally in your application's configuration to allow cross-origin requests for all or specific domains. Create a configuration class, configure
CorsRegistry
the object, as follows:import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/data") .allowedOrigins("http://example.com") .allowedMethods("GET"); // 允许的请求方法 } }
The above example will allow
http://example.com
pages from the domain to access/data
the interface, and only allow GET requests. -
Use a filter or interceptor:
You can write a custom filter or interceptor to set the appropriate CORS headers in the response. This way, you can reuse the same cross-origin setup logic in multiple places in your application.
import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter("/*") public class CorsFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setHeader("Access-Control-Allow-Origin", "http://example.com"); response.setHeader("Access-Control-Allow-Methods", "GET"); chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
With the above methods, you can solve cross-domain issues in Java Spring Boot applications. Choosing the right method for your project depends on your application architecture and requirements.