What method is used to solve cross-domain problems in java

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.

  1. Use @CrossOriginannotations:

    @CrossOriginAnnotations 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.compages from the domain can access /datathe interface.

  2. 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 CorsRegistrythe 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.compages from the domain to access /datathe interface, and only allow GET requests.

  3. 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.

Guess you like

Origin blog.csdn.net/weixin_42279822/article/details/132344349