Skip to content
This repository has been archived by the owner on May 31, 2022. It is now read-only.

Access to XMLHttpRequest ''http://192.168.3.106:8080/sac/oauth/token' from origin 'http://localhost:3001' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. #1944

Open
vitor-ferreira-sousa opened this issue Dec 18, 2021 · 0 comments

Comments

@vitor-ferreira-sousa
Copy link

good morning, I have an API raised in Payara Server, when I try to log in on the front of the error in cors but testing on Postmam the token request works normally because Postmam does not check the Cors, the error I have returned on the login screen is: "Access to XMLHttpRequest at 'http://192.168.3.106:8080/sac/oauth/token' from origin 'http://localhost:3001' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."

The normal thing is to login correctly in the fronto which is controlled by Cors / Oauth2

CorsConfig:

import java.util.Collections;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@configuration
public class CorsConfig {

@Bean
public FilterRegistrationBean<CorsFilter> corsFilterRegistrationBean() {
	CorsConfiguration config = new CorsConfiguration();
	config.setAllowCredentials(true);
	config.setAllowedOriginPatterns(Collections.singletonList("*"));
	config.setAllowedMethods(Collections.singletonList("*"));
	config.setAllowedHeaders(Collections.singletonList("*"));

	UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	source.registerCorsConfiguration("/**", config);

	FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>();
	bean.setFilter(new CorsFilter(source));
	bean.setOrder(Ordered.HIGHEST_PRECEDENCE);

	return bean;
}

}

Pom.xml

</parent>

<groupId>com.ivitech</groupId>
<artifactId>sac-api</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>sac-api</name>
<description>API de Atendimento ao Cliente</description>

<properties>
	<java.version>11</java.version>
	<modelmapper.version>2.3.0</modelmapper.version>
	<squiggly.version>1.3.18</squiggly.version>
	<lombok.version>1.18.22</lombok.version>
	<jasperreports.version>6.16.0</jasperreports.version>
	<springfox.version>2.9.2</springfox.version>
	<spring-security-jwt.version>1.1.1.RELEASE</spring-security-jwt.version>
	<spring-security-oauth2.version>2.5.1.RELEASE</spring-security-oauth2.version>
	<dockerfile-maven-version>1.4.13</dockerfile-maven-version>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
		<exclusions>
			<exclusion>
				<groupId>org.json</groupId>
				<artifactId>json</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-configuration-processor</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<scope>runtime</scope>
	</dependency>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<version>${lombok.version}</version>
	</dependency>
	<dependency>
		<groupId>org.flywaydb</groupId>
		<artifactId>flyway-core</artifactId>
	</dependency>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
	</dependency>
	<dependency>
		<groupId>io.rest-assured</groupId>
		<artifactId>rest-assured</artifactId>
		<scope>test</scope>
	</dependency>

	<dependency>
		<groupId>org.modelmapper</groupId>
		<artifactId>modelmapper</artifactId>
		<version>${modelmapper.version}</version>
	</dependency>

	<dependency>
		<groupId>com.github.bohnman</groupId>
		<artifactId>squiggly-filter-jackson</artifactId>
		<version>${squiggly.version}</version>
		<exclusions>
			<exclusion>
				<groupId>com.google.guava</groupId>
				<artifactId>guava</artifactId>
			</exclusion>
		</exclusions>
	</dependency>

	<dependency>
		<groupId>net.sf.jasperreports</groupId>
		<artifactId>jasperreports</artifactId>
		<version>${jasperreports.version}</version>
	</dependency>

	<dependency>
		<groupId>net.sf.jasperreports</groupId>
		<artifactId>jasperreports-functions</artifactId>
		<version>${jasperreports.version}</version>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-mail</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-freemarker</artifactId>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>${springfox.version}</version>
		<exclusions>
			<exclusion>
				<groupId>org.springframework.plugin</groupId>
				<artifactId>spring-plugin-core</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>${springfox.version}</version>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-bean-validators</artifactId>
		<version>${springfox.version}</version>
		<exclusions>
			<exclusion>
				<groupId>org.springframework.plugin</groupId>
				<artifactId>spring-plugin-core</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-hateoas</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-oauth2-client</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.security</groupId>
		<artifactId>spring-security-jwt</artifactId>
		<version>${spring-security-jwt.version}</version>
	</dependency>

	<dependency>
		<groupId>org.springframework.security.oauth</groupId>
		<artifactId>spring-security-oauth2</artifactId>
		<version>${spring-security-oauth2.version}</version>
	</dependency>

	<!-- Thymeleaf. -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-validation</artifactId>
	</dependency>		
	
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		<scope>provided</scope>
	</dependency>
</dependencies>
<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
		<plugin>
			<artifactId>maven-failsafe-plugin</artifactId>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-resources-plugin</artifactId>
			<version>3.2.0</version>
		</plugin>
	</plugins>
</build>

<profiles>
	<profile>
		<id>docker</id>

		<build>
			<plugins>
				<plugin>
					<groupId>com.spotify</groupId>
					<artifactId>dockerfile-maven-plugin</artifactId>
					<version>${dockerfile-maven-version}</version>
					<executions>
						<execution>
							<id>default</id>
							<goals>
								<goal>build</goal>
								<goal>push</goal>
							</goals>
						</execution>
					</executions>
					<configuration>
						<repository>sac-api</repository>
						<buildArgs>
							<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
						</buildArgs>
					</configuration>
				</plugin>
			</plugins>
		</build>
	</profile>
</profiles>

-->

Sample

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants