Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.keycloak.adapters.tomcat7;
 
 

Author(s):
Davide Ungari
Version:
$Revision: 1 $
public class CorsPreflightChecker {
    private static final Logger log = Logger.getLogger(""+CorsPreflightChecker.class);
    protected KeycloakDeployment deployment;
    public CorsPreflightChecker(KeycloakDeployment deployment) {
        this. = deployment;
    }
    public boolean checkCorsPreflight(Request requestResponse response) {
        .finer("checkCorsPreflight " + request.getRequestURI());
        if (!request.getMethod().equalsIgnoreCase("OPTIONS")) {
            .finer("checkCorsPreflight: not options ");
            return false;
        }
        if (request.getHeader("Origin") == null) {
            .finer("checkCorsPreflight: no origin header");
            return false;
        }
        .finer("Preflight request returning");
        response.setStatus(.);
        String origin = request.getHeader("Origin");
        response.setHeader("Access-Control-Allow-Origin"origin);
        response.setHeader("Access-Control-Allow-Credentials""true");
        String requestMethods = request.getHeader("Access-Control-Request-Method");
        if (requestMethods != null) {
            if (.getCorsAllowedMethods() != null) {
                requestMethods = .getCorsAllowedMethods();
            }
            response.setHeader("Access-Control-Allow-Methods"requestMethods);
        }
        String allowHeaders = request.getHeader("Access-Control-Request-Headers");
        if (allowHeaders != null) {
            if (.getCorsAllowedHeaders() != null) {
                allowHeaders = .getCorsAllowedHeaders();
            }
            response.setHeader("Access-Control-Allow-Headers"allowHeaders);
        }
        if (.getCorsMaxAge() > -1) {
            response.setHeader("Access-Control-Max-Age", Integer.toString(.getCorsMaxAge()));
        }
        return true;
    }
New to GrepCode? Check out our FAQ X