Last active
September 21, 2020 13:44
-
-
Save dipold/9781126 to your computer and use it in GitHub Desktop.
CORS in VRaptor 4
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
import java.util.Set; | |
import javax.inject.Inject; | |
import br.com.caelum.vraptor.Controller; | |
import br.com.caelum.vraptor.Options; | |
import br.com.caelum.vraptor.Result; | |
import br.com.caelum.vraptor.controller.HttpMethod; | |
import br.com.caelum.vraptor.core.RequestInfo; | |
import br.com.caelum.vraptor.http.route.Router; | |
import br.com.caelum.vraptor.view.Results; | |
@Controller | |
public class CORSController { | |
@Inject private Result result; | |
@Inject private Router router; | |
@Inject private RequestInfo requestInfo; | |
@Options("/*") | |
public void options() { | |
Set<HttpMethod> allowed = router.allowedMethodsFor(requestInfo.getRequestedUri()); | |
String allowMethods = allowed.toString().replaceAll("\\[|\\]", ""); | |
result.use(Results.status()).header("Allow", allowMethods); | |
result.use(Results.status()).header("Access-Control-Allow-Methods", allowMethods); | |
result.use(Results.status()).header("Access-Control-Allow-Headers", "Content-Type, accept, Authorization, origin"); | |
result.use(Results.status()).noContent(); | |
} | |
} |
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
import javax.inject.Inject; | |
import javax.servlet.http.HttpServletRequest; | |
import br.com.caelum.vraptor.BeforeCall; | |
import br.com.caelum.vraptor.InterceptionException; | |
import br.com.caelum.vraptor.Intercepts; | |
import br.com.caelum.vraptor.Result; | |
import br.com.caelum.vraptor.view.Results; | |
@Intercepts | |
public class CORSInterceptor { | |
@Inject private Result result; | |
@Inject private HttpServletRequest request; | |
@BeforeCall | |
public void intercept() throws InterceptionException { | |
//Fix your origin if possible for security reasons | |
String origin = request.getHeader("origin") != null ? request.getHeader("origin") : "*"; | |
result.use(Results.status()).header("Access-Control-Allow-Origin", origin); | |
result.use(Results.status()).header("Access-Control-Allow-Credentials", "true"); | |
result.use(Results.status()).header("Access-Control-Expose-Headers", "Content-Type, Location"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rafael, cara eu estava utilizando esse CORS tbm, porem ele da problemas na hora que tu precisa fazer um download. estou tentando arrumar uma solução se eu descobrir te aviso, se vc quiser pode dar uma olhada por ai tbm.. abracos