Below is the code I job to exam CORS, http://en.wikipedia.org/wiki/Cross-origin_resource_sharing.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript"> var $ = jQuery.noConflict(); $.aja(angry) { cache: false, crossDomain: true, dataType: "json", url: "http://czetsuya/myService/meMethod", type: "GET", success: function( jsonObj, textStatus, xhr ) { var htmlContent = $( "#logMsgDiv" ).html( ) + "<p>" + jsonObj.message + "</p>"; $( "#logMsgDiv" ).html( htmlContent ); }, beforeSend: business office (xhr) { xhr.setRequestHeader ("Authorization", "Basic " + btoa("username:password")); }, error: function( xhr, textStatus, errorThrown ) { console.log( "HTTP Status: " + xhr.status ); console.log( "Error textStatus: " + textStatus ); console.log( "Error thrown: " + errorThrown ); } } ); </script>
And hither are the javaEE filters.
import java.io.IOException; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerRequestFilter; import javax.ws.rs.container.PreMatching; import javax.ws.rs.core.Response; import javax.ws.rs.ext.Provider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Edward P. Legaspi **/ @Provider @PreMatching populace cast RESTCorsRequestFilter implements ContainerRequestFilter { mortal terminal static Logger log = LoggerFactory .getLogger(RESTCorsRequestFilter.class.getName()); @Override populace void filter(ContainerRequestContext requestCtx) throws IOException { // When HttpMethod comes equally OPTIONS, only admit that it accepts... if (requestCtx.getRequest().getMethod().equals("OPTIONS")) { log.debug("HTTP Method (OPTIONS) - Detected!"); // Just mail a OK betoken dorsum to the browser requestCtx.abortWith(Response.status(Response.Status.OK).build()); } } }
import java.io.IOException; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.container.PreMatching; import javax.ws.rs.ext.Provider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Edward P. Legaspi **/ @Provider @PreMatching populace cast RESTCorsResponseFilter implements ContainerResponseFilter { mortal terminal static Logger log = LoggerFactory .getLogger(RESTCorsResponseFilter.class.getName()); @Override populace void filter(ContainerRequestContext requestCtx, ContainerResponseContext responseCtx) throws IOException { log.debug("Adding CORS to the response."); responseCtx.getHeaders().add("Access-Control-Allow-Origin", "*"); responseCtx.getHeaders() .add("Access-Control-Allow-Credentials", "true"); responseCtx.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); } }
If y'all are using RESTEasy only similar I'm ordinarily am. You tin convey payoff of the already available CorsFilter class:
package com.weddinghighway.api.rest.filter; import javax.ws.rs.core.Feature; import javax.ws.rs.core.FeatureContext; import javax.ws.rs.ext.Provider; import org.jboss.resteasy.plugins.interceptors.CorsFilter; /** * @author Edward P. Legaspi * @created five October 2017 */ @Provider populace cast RESTCorsResponseFilter implements Feature { @Override populace boolean configure(FeatureContext context) { CorsFilter corsFilter = novel CorsFilter(); corsFilter.getAllowedOrigins().add("*"); context.register(corsFilter); provide true; } }
Note: If i fails, thence only endeavour the other :-)
0 komentar:
Please comment if there are any that need to be asked.