-2

I want overwite CorsUtils abstract class from here https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/cors/reactive/CorsUtils.html. I tried this:

import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.reactive.CorsUtils;

@Component
public class CustomPostGlobalFilter extends CorsUtils {

    public static boolean isCorsRequest(ServerHttpRequest request) {
        return false;
    }

    public static boolean isPreFlightRequest(ServerHttpRequest request) {
        return false;
    }

    public static boolean isSameOrigin(ServerHttpRequest request) {
        return false;
    }
}

But looks like this is not the correct way to implement this. Can you guide me what is the proper way to implement this?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Can `static` (statically dispatched) methods be virtually overridden (virtually dispatched)? I **guess** no... Additionally, the Spring Framework (sometimes) uses a stupid strategy of making utility classes `abstract` not making their constructor `private` (hence not throwing an instantiation exception) letting users to extend them, literally, with no sense of doing that. In short, you can't "replace" statically dispatched methods (unless you replace class files). Period. What you need is extending a CORS-aware filter (if there's any, no clue) and override the methods from that class. – terrorrussia-keeps-killing Jan 11 '22 at 11:21
  • 1
    Not sure how close your problem is to what I worked with everytime I configured CORS for Spring MVC apps, but it looks like you're actually looking for something like the following: https://stackoverflow.com/questions/40418441/spring-security-cors-filter – terrorrussia-keeps-killing Jan 11 '22 at 11:25
  • This is the issue that I want to solve: https://github.com/spring-cloud/spring-cloud-gateway/issues/830 – Peter Penzov Jan 11 '22 at 11:44
  • Can't help you on that. You question is more about general Java and FAR away from what you're trying to find a work-around for. You can try overriding that bad bean (I haven't read it in deep) by making some analysis and debugging, or fix the upstream source code and let everyone be happy with your fix. Good luck. – terrorrussia-keeps-killing Jan 11 '22 at 12:03

1 Answers1

0

You cannot override static methods. You can provide an alternative version in your extending class but the caller controls which method they employ by specifying the class.

vsfDawg
  • 1,425
  • 1
  • 9
  • 12
  • I want to solve this problem: https://github.com/spring-cloud/spring-cloud-gateway/issues/830 Is there some other solution? – Peter Penzov Jan 11 '22 at 11:44
  • That is a separate question. I don't know enough about Spring to provide you with specific guidance on that problem. Any case where Spring is calling `CorsUtils` internally is going to use that class. However, if `CorsUtils` is a method parameter then you can pass your own version. – vsfDawg Jan 11 '22 at 12:03
  • Can you show me code example, please? – Peter Penzov Jan 11 '22 at 12:16
  • I wasn't clear - that was intended to be a hypothetical comment. An alternate strategy for utility methods is to instantiate the utility class and invoke instance methods rather than static access (this instance is usually a singleton in these cases). This allows you to override behavior via polymorphism. `CorsUtils` does not follow this pattern. – vsfDawg Jan 11 '22 at 13:16