简体   繁体   中英

Can not get token (OAUTH2, Spring, Kotlin)

I try to get token but it not works. Request work in postman, but when I try reproduce this in angular I getting:

ERROR1

My request in postman:

误差2

My request in angular:

getToken() {
const headers = new HttpHeaders();
headers.set('Authorization', 'Basic ZGV2Z2xhbi1jbGllbnQ6ZGV2Z2xhbi1zZWNyZXQ=');
headers.set('Content-Type', 'application/x-www-form-urlencoded');

const body = new URLSearchParams();
body.append('username', 'Alex123');
body.append('password', 'password');
body.append('grant_type', 'password');

this.http.post<AuthToken>('http://localhost:8080/oauth/token', body, {headers: headers})
  .subscribe(
    success => {
      debugger
      this.token = success.access_token;
    }
  );
}

my CORS configure, i thing all is good with it.:

   @Throws(Exception::class)
override fun configure(http: HttpSecurity) {
    http
        .csrf().disable()
        .anonymous().disable()
        .authorizeRequests()
        .antMatchers("/api-docs/**").permitAll()
    http
        .csrf().disable()
        .anonymous().disable()
        .authorizeRequests()
        .antMatchers("/auth/token").permitAll()
}

@Bean
fun corsFilter(): FilterRegistrationBean<*> {
    val source = UrlBasedCorsConfigurationSource()
    val config = CorsConfiguration()
    config.allowCredentials = java.lang.Boolean.TRUE
    config.addAllowedOrigin("*")
    config.addAllowedHeader("*")
    config.addAllowedMethod("*")
    source.registerCorsConfiguration("/**", config)
    val bean = FilterRegistrationBean(CorsFilter(source))
    bean.order = 0
    return bean
}

It seems you have issues with CORS, please read the following article about to learn more about CORS.

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

This answer should be an comment, but I'm unable to comment..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM