AWS Cognito Angular SpringBoot Oauth2 - invalid_token error











up vote
0
down vote

favorite












we are using AWS Cognito for Oauth2. Our UI is built on Angular. After my user logs in, I initiate a call to Cognito to get Authorization token. I am using Authorization Code Grant with PKCE for getting the token from Cognito. After getting the toke from Cognito, I call my Spring Boot REST service. When calling Spring Boot service from Angular, I send the token in the Authorization header as "Bearer" token.



Here is my ResourceServerConfiguration.java:



import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
private static final String RESOURCE_ID = "resource-server-rest-api";
private static final String SECURED_READ_SCOPE = "#oauth2.hasScope('openid')";
private static final String SECURED_WRITE_SCOPE = "#oauth2.hasScope('openid')";
private static final String SECURED_PATTERN = "/**";
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(RESOURCE_ID);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatchers()
.antMatchers(SECURED_PATTERN).and().authorizeRequests()
.antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
.anyRequest().access(SECURED_READ_SCOPE);
}
}


When calling the REST service my Angular UI gets HTTP Response 401 with the following error message:



DEBUG o.s.s.o.p.a.OAuth2AuthenticationProcessingFilter - Authentication request failed: error="invalid_token", error_description="Invalid access token: eyJraWQiOiIy.......



  1. Any idea why I am getting invalid_token?

  2. Does spring makes a call to Cognito to verify the token?

  3. I am NOT storing the token in my REST servic layer. Is this required?

  4. I enabled DEBUG using logging.level.root=DEBUG. But I do not see descriptive message in the output. How do i trouble shoot this issue?


Thanks in advance for any help in resolving this issue.










share|improve this question




























    up vote
    0
    down vote

    favorite












    we are using AWS Cognito for Oauth2. Our UI is built on Angular. After my user logs in, I initiate a call to Cognito to get Authorization token. I am using Authorization Code Grant with PKCE for getting the token from Cognito. After getting the toke from Cognito, I call my Spring Boot REST service. When calling Spring Boot service from Angular, I send the token in the Authorization header as "Bearer" token.



    Here is my ResourceServerConfiguration.java:



    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpMethod;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
    import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
    import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

    @Configuration
    @EnableResourceServer
    public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
    private static final String RESOURCE_ID = "resource-server-rest-api";
    private static final String SECURED_READ_SCOPE = "#oauth2.hasScope('openid')";
    private static final String SECURED_WRITE_SCOPE = "#oauth2.hasScope('openid')";
    private static final String SECURED_PATTERN = "/**";
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
    resources.resourceId(RESOURCE_ID);
    }
    @Override
    public void configure(HttpSecurity http) throws Exception {
    http.requestMatchers()
    .antMatchers(SECURED_PATTERN).and().authorizeRequests()
    .antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
    .anyRequest().access(SECURED_READ_SCOPE);
    }
    }


    When calling the REST service my Angular UI gets HTTP Response 401 with the following error message:



    DEBUG o.s.s.o.p.a.OAuth2AuthenticationProcessingFilter - Authentication request failed: error="invalid_token", error_description="Invalid access token: eyJraWQiOiIy.......



    1. Any idea why I am getting invalid_token?

    2. Does spring makes a call to Cognito to verify the token?

    3. I am NOT storing the token in my REST servic layer. Is this required?

    4. I enabled DEBUG using logging.level.root=DEBUG. But I do not see descriptive message in the output. How do i trouble shoot this issue?


    Thanks in advance for any help in resolving this issue.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      we are using AWS Cognito for Oauth2. Our UI is built on Angular. After my user logs in, I initiate a call to Cognito to get Authorization token. I am using Authorization Code Grant with PKCE for getting the token from Cognito. After getting the toke from Cognito, I call my Spring Boot REST service. When calling Spring Boot service from Angular, I send the token in the Authorization header as "Bearer" token.



      Here is my ResourceServerConfiguration.java:



      import org.springframework.context.annotation.Configuration;
      import org.springframework.http.HttpMethod;
      import org.springframework.security.config.annotation.web.builders.HttpSecurity;
      import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
      import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
      import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

      @Configuration
      @EnableResourceServer
      public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
      private static final String RESOURCE_ID = "resource-server-rest-api";
      private static final String SECURED_READ_SCOPE = "#oauth2.hasScope('openid')";
      private static final String SECURED_WRITE_SCOPE = "#oauth2.hasScope('openid')";
      private static final String SECURED_PATTERN = "/**";
      @Override
      public void configure(ResourceServerSecurityConfigurer resources) {
      resources.resourceId(RESOURCE_ID);
      }
      @Override
      public void configure(HttpSecurity http) throws Exception {
      http.requestMatchers()
      .antMatchers(SECURED_PATTERN).and().authorizeRequests()
      .antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
      .anyRequest().access(SECURED_READ_SCOPE);
      }
      }


      When calling the REST service my Angular UI gets HTTP Response 401 with the following error message:



      DEBUG o.s.s.o.p.a.OAuth2AuthenticationProcessingFilter - Authentication request failed: error="invalid_token", error_description="Invalid access token: eyJraWQiOiIy.......



      1. Any idea why I am getting invalid_token?

      2. Does spring makes a call to Cognito to verify the token?

      3. I am NOT storing the token in my REST servic layer. Is this required?

      4. I enabled DEBUG using logging.level.root=DEBUG. But I do not see descriptive message in the output. How do i trouble shoot this issue?


      Thanks in advance for any help in resolving this issue.










      share|improve this question















      we are using AWS Cognito for Oauth2. Our UI is built on Angular. After my user logs in, I initiate a call to Cognito to get Authorization token. I am using Authorization Code Grant with PKCE for getting the token from Cognito. After getting the toke from Cognito, I call my Spring Boot REST service. When calling Spring Boot service from Angular, I send the token in the Authorization header as "Bearer" token.



      Here is my ResourceServerConfiguration.java:



      import org.springframework.context.annotation.Configuration;
      import org.springframework.http.HttpMethod;
      import org.springframework.security.config.annotation.web.builders.HttpSecurity;
      import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
      import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
      import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

      @Configuration
      @EnableResourceServer
      public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
      private static final String RESOURCE_ID = "resource-server-rest-api";
      private static final String SECURED_READ_SCOPE = "#oauth2.hasScope('openid')";
      private static final String SECURED_WRITE_SCOPE = "#oauth2.hasScope('openid')";
      private static final String SECURED_PATTERN = "/**";
      @Override
      public void configure(ResourceServerSecurityConfigurer resources) {
      resources.resourceId(RESOURCE_ID);
      }
      @Override
      public void configure(HttpSecurity http) throws Exception {
      http.requestMatchers()
      .antMatchers(SECURED_PATTERN).and().authorizeRequests()
      .antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
      .anyRequest().access(SECURED_READ_SCOPE);
      }
      }


      When calling the REST service my Angular UI gets HTTP Response 401 with the following error message:



      DEBUG o.s.s.o.p.a.OAuth2AuthenticationProcessingFilter - Authentication request failed: error="invalid_token", error_description="Invalid access token: eyJraWQiOiIy.......



      1. Any idea why I am getting invalid_token?

      2. Does spring makes a call to Cognito to verify the token?

      3. I am NOT storing the token in my REST servic layer. Is this required?

      4. I enabled DEBUG using logging.level.root=DEBUG. But I do not see descriptive message in the output. How do i trouble shoot this issue?


      Thanks in advance for any help in resolving this issue.







      spring angular spring-boot amazon-cognito spring-security-oauth2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 15:32

























      asked Nov 7 at 22:33









      Jegan

      14




      14





























          active

          oldest

          votes











          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',
          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%2f53198910%2faws-cognito-angular-springboot-oauth2-invalid-token-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53198910%2faws-cognito-angular-springboot-oauth2-invalid-token-error%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()