Example usage for org.springframework.web.cors CorsUtils isCorsRequest

List of usage examples for org.springframework.web.cors CorsUtils isCorsRequest

Introduction

In this page you can find the example usage for org.springframework.web.cors CorsUtils isCorsRequest.

Prototype

public static boolean isCorsRequest(HttpServletRequest request) 

Source Link

Document

Returns true if the request is a valid CORS one by checking Origin header presence and ensuring that origins are different.

Usage

From source file:ca.uhn.fhir.rest.server.interceptor.CorsInterceptor.java

@Override
public boolean incomingRequestPreProcessed(HttpServletRequest theRequest, HttpServletResponse theResponse) {
    if (CorsUtils.isCorsRequest(theRequest)) {
        boolean isValid;
        try {/*w w  w  . j  ava  2 s.  co  m*/
            isValid = myCorsProcessor.processRequest(myConfig, theRequest, theResponse);
        } catch (IOException e) {
            throw new InternalErrorException(e);
        }
        if (!isValid || CorsUtils.isPreFlightRequest(theRequest)) {
            return false;
        }
    }

    return super.incomingRequestPreProcessed(theRequest, theResponse);
}

From source file:org.springframework.web.cors.DefaultCorsProcessor.java

@Override
@SuppressWarnings("resource")
public boolean processRequest(@Nullable CorsConfiguration config, HttpServletRequest request,
        HttpServletResponse response) throws IOException {

    if (!CorsUtils.isCorsRequest(request)) {
        return true;
    }/*from   w  w  w .j  av  a 2 s  .  c  o m*/

    ServletServerHttpResponse serverResponse = new ServletServerHttpResponse(response);
    if (responseHasCors(serverResponse)) {
        logger.debug("Skip CORS processing: response already contains \"Access-Control-Allow-Origin\" header");
        return true;
    }

    ServletServerHttpRequest serverRequest = new ServletServerHttpRequest(request);
    if (WebUtils.isSameOrigin(serverRequest)) {
        logger.debug("Skip CORS processing: request is from same origin");
        return true;
    }

    boolean preFlightRequest = CorsUtils.isPreFlightRequest(request);
    if (config == null) {
        if (preFlightRequest) {
            rejectRequest(serverResponse);
            return false;
        } else {
            return true;
        }
    }

    return handleInternal(serverRequest, serverResponse, config, preFlightRequest);
}

From source file:org.springframework.web.socket.sockjs.support.AbstractSockJsService.java

@Override
@Nullable/* w  w  w  .  j  a v  a 2 s  .co m*/
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
    if (!this.suppressCors && CorsUtils.isCorsRequest(request)) {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowedOrigins(new ArrayList<>(this.allowedOrigins));
        config.addAllowedMethod("*");
        config.setAllowCredentials(true);
        config.setMaxAge(ONE_YEAR);
        config.addAllowedHeader("*");
        return config;
    }
    return null;
}