Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.Ordered;
- import org.springframework.core.annotation.Order;
- import org.springframework.stereotype.Component;
- import br.com.mdw.config.MdwApiProperty;
- @Component
- @Order(Ordered.HIGHEST_PRECEDENCE)
- public class CorsFilter implements Filter {
- @Autowired
- private MdwApiProperty mdwApiProperty;
- @Override
- public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
- throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest) req;
- HttpServletResponse response = (HttpServletResponse) resp;
- response.setHeader("Access-Control-Allow-Origin", mdwApiProperty.getOriginPermitida());
- response.setHeader("Access-Control-Allow-Credentials", "true");
- if ("OPTIONS".equals(request.getMethod()) && mdwApiProperty.getOriginPermitida().equals(request.getHeader("Origin"))) {
- response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT, OPTIONS");
- response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, Accept");
- response.setHeader("Access-Control-Max-Age", "3600");
- response.setStatus(HttpServletResponse.SC_OK);
- } else {
- chain.doFilter(req, resp);
- }
- }
- @Override
- public void destroy() {
- }
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- }
- }
- import org.springframework.boot.context.properties.ConfigurationProperties;
- @ConfigurationProperties("mdw")
- public class MdwApiProperty {
- private String originPermitida = "http://localhost:8000";
- private final Seguranca seguranca = new Seguranca();
- public Seguranca getSeguranca() {
- return seguranca;
- }
- public String getOriginPermitida() {
- return originPermitida;
- }
- public void setOriginPermitida(String originPermitida) {
- this.originPermitida = originPermitida;
- }
- public static class Seguranca {
- private boolean enableHttps;
- public boolean isEnableHttps() {
- return enableHttps;
- }
- public void setEnableHttps(boolean enableHttps) {
- this.enableHttps = enableHttps;
- }
- }
- }
- @ConfigurationProperties("mdw")
- @ConfigurationProperties("habilitando-producao")
- habilitando-producao.seguranca.enable-https=true
- package br.com.mdw;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import br.com.mdw.config.MdwApiProperty;
- @SpringBootApplication
- @EnableConfigurationProperties(MdwApiProperty.class)
- public class MdwApplication {
- public static void main(String[] args) {
- SpringApplication.run(MdwApplication.class, args);
- }
- }
- mdw.seguranca.enable-https=true
- spring.datasource.url={JDBC_DATABASE_URL}
- spring.datasource.username={JDBC_DATABASE_USERNAME}
- spring.datasource.password={JDBC_DATABASE_PASSWORD}
- mdw.origin-permitida=https://mdw-arm-wladimir.herokuapp.com
Add Comment
Please, Sign In to add comment