【问题标题】:Can not get token (OAUTH2, Spring, Kotlin)无法获取令牌(OAUTH2、Spring、Kotlin)
【发布时间】:2019-04-24 06:05:16
【问题描述】:

我尝试获取令牌,但它不起作用。请求在邮递员中工作,但是当我尝试以角度重现此内容时,我得到:

我对邮递员的要求:

我的角度要求:

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;
    }
  );
}

我的 CORS 配置,我的一切都很好。:

   @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
}

【问题讨论】:

    标签: java spring angular kotlin oauth-2.0


    【解决方案1】:

    您似乎对 CORS 有疑问,请阅读以下文章以了解有关 CORS 的更多信息。

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

    这个答案应该是评论,但我无法评论..

    【讨论】:

    • 将 cors 添加到问题正文中。
    • 你能给我提供响应头吗?
    • 您可以尝试将 OPTIONS 方法添加到 Access-control-allow-methods 标头吗?您正在发出 OPTIONS 请求,并且此标头中仅定义了 POST。有关此标头的信息:developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…
    • 我试过了,但没用.. val config = CorsConfiguration() config.allowCredentials = java.lang.Boolean.TRUE config.addAllowedOrigin("localhost:4200") config.addAllowedHeader(" ") config.addAllowedMethod("GET, OPTIONS, HEAD, PUT, POST") source.registerCorsConfiguration("/*", config)
    • 您收到了哪些响应标头?
    猜你喜欢
    • 2015-06-02
    • 2018-12-30
    • 2011-10-04
    • 2015-02-11
    • 2016-06-02
    • 2015-07-21
    • 2016-06-22
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多