1
That’s the error message you’re giving;
I’m having trouble getting my Front-End API to access my Back-End API which is a Spring Boot project, I’m sure my problem is not my Fron-End API, the problem is in setting up CORS regarding access permission.
I’ll leave my CORS settings just below
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 {
}
}
And here where I set up access:
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;
}
}
}
A class called Mdwapiproperty has been created
Where was placed the following annotation:
@ConfigurationProperties("mdw")
And because of this annotation is why the application-Prod.properties file accepted this line of code.
mdw.seguranca.enable-https=true
If this is true if I happen to modify the annotation of the Mdwapiproperty class to
@ConfigurationProperties("habilitando-producao")
It would look like this in the application-Prod.properties file. .
habilitando-producao.seguranca.enable-https=true
And in this file where enables the nickname for access to the project
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);
}
}
This is where the note is
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
For some reason he’s not accepting access, I need help.