Can not get token (OAUTH2, Spring, Kotlin)












0















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:



error2



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
}









share|improve this question





























    0















    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:



    error2



    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
    }









    share|improve this question



























      0












      0








      0








      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:



      error2



      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
      }









      share|improve this question
















      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:



      error2



      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
      }






      java spring angular kotlin oauth-2.0






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 16:45









      Chris Thompson

      28.8k96898




      28.8k96898










      asked Nov 21 '18 at 16:00









      mremil6mremil6

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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..






          share|improve this answer
























          • add cors to questions body.

            – mremil6
            Nov 21 '18 at 16:08











          • Could you provide me the response headers?

            – Romano Schoonheim
            Nov 21 '18 at 16:13











          • sure (imgur.com/a/0SjrNW5)

            – mremil6
            Nov 22 '18 at 7:57











          • Could you try to add the OPTIONS method to the Access-control-allow-methods header? You are making a OPTIONS request, and only POST has been defined in this header. Information about this header: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…

            – Romano Schoonheim
            Nov 22 '18 at 8:53













          • I try it, but it did not work.. 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)

            – mremil6
            Nov 22 '18 at 9:02













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415982%2fcan-not-get-token-oauth2-spring-kotlin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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..






          share|improve this answer
























          • add cors to questions body.

            – mremil6
            Nov 21 '18 at 16:08











          • Could you provide me the response headers?

            – Romano Schoonheim
            Nov 21 '18 at 16:13











          • sure (imgur.com/a/0SjrNW5)

            – mremil6
            Nov 22 '18 at 7:57











          • Could you try to add the OPTIONS method to the Access-control-allow-methods header? You are making a OPTIONS request, and only POST has been defined in this header. Information about this header: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…

            – Romano Schoonheim
            Nov 22 '18 at 8:53













          • I try it, but it did not work.. 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)

            – mremil6
            Nov 22 '18 at 9:02


















          0














          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..






          share|improve this answer
























          • add cors to questions body.

            – mremil6
            Nov 21 '18 at 16:08











          • Could you provide me the response headers?

            – Romano Schoonheim
            Nov 21 '18 at 16:13











          • sure (imgur.com/a/0SjrNW5)

            – mremil6
            Nov 22 '18 at 7:57











          • Could you try to add the OPTIONS method to the Access-control-allow-methods header? You are making a OPTIONS request, and only POST has been defined in this header. Information about this header: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…

            – Romano Schoonheim
            Nov 22 '18 at 8:53













          • I try it, but it did not work.. 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)

            – mremil6
            Nov 22 '18 at 9:02
















          0












          0








          0







          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..






          share|improve this answer













          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..







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 16:04









          Romano SchoonheimRomano Schoonheim

          6810




          6810













          • add cors to questions body.

            – mremil6
            Nov 21 '18 at 16:08











          • Could you provide me the response headers?

            – Romano Schoonheim
            Nov 21 '18 at 16:13











          • sure (imgur.com/a/0SjrNW5)

            – mremil6
            Nov 22 '18 at 7:57











          • Could you try to add the OPTIONS method to the Access-control-allow-methods header? You are making a OPTIONS request, and only POST has been defined in this header. Information about this header: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…

            – Romano Schoonheim
            Nov 22 '18 at 8:53













          • I try it, but it did not work.. 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)

            – mremil6
            Nov 22 '18 at 9:02





















          • add cors to questions body.

            – mremil6
            Nov 21 '18 at 16:08











          • Could you provide me the response headers?

            – Romano Schoonheim
            Nov 21 '18 at 16:13











          • sure (imgur.com/a/0SjrNW5)

            – mremil6
            Nov 22 '18 at 7:57











          • Could you try to add the OPTIONS method to the Access-control-allow-methods header? You are making a OPTIONS request, and only POST has been defined in this header. Information about this header: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…

            – Romano Schoonheim
            Nov 22 '18 at 8:53













          • I try it, but it did not work.. 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)

            – mremil6
            Nov 22 '18 at 9:02



















          add cors to questions body.

          – mremil6
          Nov 21 '18 at 16:08





          add cors to questions body.

          – mremil6
          Nov 21 '18 at 16:08













          Could you provide me the response headers?

          – Romano Schoonheim
          Nov 21 '18 at 16:13





          Could you provide me the response headers?

          – Romano Schoonheim
          Nov 21 '18 at 16:13













          sure (imgur.com/a/0SjrNW5)

          – mremil6
          Nov 22 '18 at 7:57





          sure (imgur.com/a/0SjrNW5)

          – mremil6
          Nov 22 '18 at 7:57













          Could you try to add the OPTIONS method to the Access-control-allow-methods header? You are making a OPTIONS request, and only POST has been defined in this header. Information about this header: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…

          – Romano Schoonheim
          Nov 22 '18 at 8:53







          Could you try to add the OPTIONS method to the Access-control-allow-methods header? You are making a OPTIONS request, and only POST has been defined in this header. Information about this header: developer.mozilla.org/en-US/docs/Web/HTTP/Headers/…

          – Romano Schoonheim
          Nov 22 '18 at 8:53















          I try it, but it did not work.. 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)

          – mremil6
          Nov 22 '18 at 9:02







          I try it, but it did not work.. 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)

          – mremil6
          Nov 22 '18 at 9:02




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415982%2fcan-not-get-token-oauth2-spring-kotlin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno