Created
March 10, 2017 07:21
-
-
Save gorshkov-leonid/e2e35cf18dc0a0f4f9d2e73ca800c132 to your computer and use it in GitHub Desktop.
CORS in Wildfly SWARM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.wildfly.swarm.examples.netflix.ribbon.time; | |
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.ext.Provider; | |
/** | |
* @author Lance Ball | |
*/ | |
// works only in JAX-RS, not working with static resources | |
@Provider | |
public class CORSFilter implements ContainerResponseFilter { | |
public void filter(ContainerRequestContext paramContainerRequestContext, | |
ContainerResponseContext paramContainerResponseContext) | |
throws IOException { | |
paramContainerResponseContext.getHeaders().add("Access-Control-Allow-Origin", "*"); | |
paramContainerResponseContext.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization"); | |
paramContainerResponseContext.getHeaders().add("Access-Control-Allow-Credentials", "true"); | |
paramContainerResponseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); | |
paramContainerResponseContext.getHeaders().add("Access-Control-Max-Age", "1209600"); | |
} | |
} | |
jaxrs.addPackage(CORSFilter.class.getPackage()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) throws Exception { | |
Swarm swarm = new Swarm(); | |
JGroupsFraction fraction = new JGroupsFraction() | |
.defaultChannel( "swarm-jgroups") | |
.stack( "udp", (s)->{ | |
s.transport( "UDP", (t)->{ | |
t.socketBinding("jgroups-udp"); | |
}); | |
s.protocol("TCP", (p -> { | |
p.property("bind_port", "9090"); | |
})); | |
s.protocol("TCPPING", (p)-> { | |
p.property("initial_hosts", "localhost[9090],localhost[9091],localhost[9092],localhost[9093]") | |
.property("port_range", "4") | |
.property("timeout", "3000") | |
.property("num_initial_members", "4"); | |
}); | |
s.protocol( "FD_SOCK", (p)->{ | |
p.socketBinding( "jgroups-udp-fd" ); | |
}); | |
s.protocol( "FD_ALL" ); | |
s.protocol( "VERIFY_SUSPECT" ); | |
s.protocol( "pbcast.NAKACK2" ); | |
s.protocol( "UNICAST3" ); | |
s.protocol( "pbcast.STABLE" ); | |
s.protocol( "pbcast.GMS" ); | |
s.protocol( "UFC" ); | |
s.protocol( "MFC" ); | |
s.protocol( "FRAG2" ); | |
s.protocol( "RSVP" ); | |
}) | |
.channel( "swarm-jgroups", (c)->{ | |
c.stack( "udp" ); | |
}); | |
swarm.fraction(fraction); | |
// alternative way to configure filter - works only in JAX-RS, not working with static resources | |
// todo how to add headers programmatically? | |
// UndertowFraction undertowFraction = UndertowFraction.createDefaultFraction() | |
// .filterConfiguration(new FilterConfiguration() | |
// .responseHeader(new ResponseHeader("Access-Control-Allow-Origin").headerName("Access-Control-Allow-Origin").headerValue("*")) | |
// .responseHeader(new ResponseHeader("Access-Control-Allow-Headers").headerName("Access-Control-Allow-Headers").headerValue("origin, content-type, accept, authorization")) | |
// .responseHeader(new ResponseHeader("Access-Control-Allow-Credentials").headerName("Access-Control-Allow-Credentials").headerValue("true")) | |
// .responseHeader(new ResponseHeader("Access-Control-Allow-Methods").headerName("Access-Control-Allow-Methods").headerValue("GET, POST, PUT, DELETE, OPTIONS, HEAD, undefined")) | |
// .responseHeader(new ResponseHeader("Access-Control-Max-Age").headerName("Access-Control-Max-Age").headerValue("1209600")) | |
// ); | |
// swarm.fraction(undertowFraction); | |
// alternative way: works with jax-rs and static resources | |
// swarm.withXmlConfig(Main.class.getResource("/standalone.xml")); | |
JAXRSArchive jaxrs = ShrinkWrap.create(JAXRSArchive.class, "time.war"); | |
jaxrs.addPackage(TimeResource.class.getPackage()); | |
jaxrs.addAllDependencies(); | |
jaxrs.staticContent(); | |
jaxrs.as(TopologyArchive.class).advertise(); | |
swarm.start().deploy(jaxrs); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<subsystem xmlns="urn:jboss:domain:undertow:3.0"> | |
<buffer-cache name="default"/> | |
<server name="default-server"> | |
<http-listener name="default" socket-binding="http" redirect-socket="https"/> | |
<host name="default-host" alias="localhost"> | |
<filter-ref name="server-header"/> | |
<filter-ref name="x-powered-by-header"/> | |
<filter-ref name="Access-Control-Allow-Origin"/> | |
<filter-ref name="Access-Control-Allow-Methods"/> | |
<filter-ref name="Access-Control-Allow-Headers"/> | |
<filter-ref name="Access-Control-Allow-Credentials"/> | |
<filter-ref name="Access-Control-Max-Age"/> | |
</host> | |
</server> | |
<servlet-container name="default"> | |
<jsp-config/> | |
<websockets/> | |
</servlet-container> | |
<filters> | |
<response-header name="server-header" header-name="Server" header-value="WildFly/10"/> | |
<response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/> | |
<response-header name="Access-Control-Allow-Origin" header-name="Access-Control-Allow-Origin" header-value="*"/> | |
<response-header name="Access-Control-Allow-Methods" header-name="Access-Control-Allow-Methods" header-value="GET, POST, OPTIONS, PUT"/> | |
<response-header name="Access-Control-Allow-Headers" header-name="Access-Control-Allow-Headers" header-value="accept, authorization, content-type, x-requested-with"/> | |
<response-header name="Access-Control-Allow-Credentials" header-name="Access-Control-Allow-Credentials" header-value="true"/> | |
<response-header name="Access-Control-Max-Age" header-name="Access-Control-Max-Age" header-value="1"/> | |
</filters> | |
</subsystem> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment