2
I can get the TOKEN of my application by using Postman as follows.
This works perfectly, and return me the Token.
However when I try to make the same request, using Axios, in my application with Vue@cli I get the following error:
I’m mounting the Axios requisition this way:
axios({
url: '/oauth/token',
method: 'post',
baseURL: 'http://prog06:8080',
headers: { 'Authorization': 'Basic bWFzdGVyOjEyMzQ1', 'Content-Type': 'application/x-www-form-urlencoded', 'scope': 'password', },
data: {
username: 'user1',
password: 'user1'
}
}).then(result => {
console.log(result)
})
Configurações
Basic
I’m passing the following amount master:12345
in Base64.
data
I’m passing the username
and password
of the user.
Back-end is in Java Spring Boot:
pom.xml
<dependencies>
<!-- Segurança | Início -->
<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.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<!-- Segurança | Fim -->
Securityconfig.java
//Edit02
package com.mfac.config;
import java.util.Arrays;
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
@EnableWebSecurity
@EnableAuthorizationServer
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@CrossOrigin
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.csrf().disable();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
CorsConfigurationSource corsConfigurationSource(){
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Collections.singletonList("*"));
configuration.setAllowedMethods(Arrays.asList("GET","POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
How can I fix this problem ? because the request to oauth/token
by Postman works, now with Axios returns me the CORS error quoted above.
I made the changes, but I still have the same mistake. But I do not know if I did it correctly, because I had doubts, what to put here
configuration.setAllowedOrigins(Arrays.asList("http://prog05:8080"));
in the pro05 case is the machine being requested by the Front End using the Axios– Dup
When I deal with these things I usually release everything and close the few, it seems easier so, so I would put Configuration.setAllowedOrigins(Collections.singletonList("*")); but if you want to release only that pc if I’m not mistaken it has to be so Configuration.setAllowedOrigins(Arrays.asList("http://prog05","http://www.prog05"))
– rnd_rss
I made the change in the code as suggested and updated the question to release everything only for testing, however I am getting exactly the same error. Have some possibility of being mounted incorrectly the request in Axios ?
– Dup
It is not a problem with Xios, nor is it still arriving in your call, if you open the Developer tools and see the network tab you will see that first the browser makes a call OPTIONS to see what has released, and only then makes your call, in case is giving problem in the options. I saw it now in my application and has a filter that treats Cors, try to add it in your application
– rnd_rss
I put Filter but it still didn’t work, what I find funny is that if I leave these two annotations Enableresourceserver Enableauthorizationserver. the Server returns me 401 OPTIONS and if you remove them return me OPTIONS 404. In the SPRING documentation link https://www.baeldung.com/spring-security-cors-preflight is informed that if you use ** http.Cors();*? SPRING would use a OPTIONS-ready solution, then I doubt, when I use the annotations Enableresourceserver and Enableauthorizationserver it overwrites http.Cors settings ?
– Dup