Spring boot OAuth 2 security get access token from Refresh token(if expired)











up vote
0
down vote

favorite












I have implemented Spring boot Oauth 2 security it works fine but when I try to get access token (if expired) from refresh token it gives me an error



{
"error": "unauthorized",
"error_description": "admin"
}


Console Log



Handling error: UsernameNotFoundException, admin


Following is my code



1.WebSecurityConfigure



@Configuration
@EnableWebSecurity
public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/user/getEmployeesList")
.hasAnyRole("USER").anyRequest().authenticated().and().formLogin()
.permitAll().and().logout().permitAll();
http.csrf().disable();
}

@Override
public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception {
authenticationMgr.inMemoryAuthentication().withUser("admin").password("admin")
.authorities("ROLE_USER");
;
}
}


2.AuthorizationServerConfigure



@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired
private AuthenticationManager authenticationManager;


@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()")
.allowFormAuthenticationForClients();
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("MagicUser").authorizedGrantTypes("authorization_code", "refresh_token","password")
.authorities("CLIENT").scopes("openid", "read", "write", "trust").resourceIds("oauth2-resource")
.redirectUris("http://10.9.6.31:8090/showEmployees").accessTokenValiditySeconds(5000).secret("secret")
.refreshTokenValiditySeconds(50000);

}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
;
}
}


Please help me to figure out this issue



Access token request (if expired)



    http://10.9.6.31:8091/oauth/token
Body parameter
grant_type=refresh_token
refresh_token=78d2ab82-46a2-4b70-a9e8-e3f9e5ddfec6









share|improve this question


























    up vote
    0
    down vote

    favorite












    I have implemented Spring boot Oauth 2 security it works fine but when I try to get access token (if expired) from refresh token it gives me an error



    {
    "error": "unauthorized",
    "error_description": "admin"
    }


    Console Log



    Handling error: UsernameNotFoundException, admin


    Following is my code



    1.WebSecurityConfigure



    @Configuration
    @EnableWebSecurity
    public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/resources/**");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/user/getEmployeesList")
    .hasAnyRole("USER").anyRequest().authenticated().and().formLogin()
    .permitAll().and().logout().permitAll();
    http.csrf().disable();
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception {
    authenticationMgr.inMemoryAuthentication().withUser("admin").password("admin")
    .authorities("ROLE_USER");
    ;
    }
    }


    2.AuthorizationServerConfigure



    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;


    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security
    .tokenKeyAccess("permitAll()")
    .checkTokenAccess("isAuthenticated()")
    .allowFormAuthenticationForClients();
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory().withClient("MagicUser").authorizedGrantTypes("authorization_code", "refresh_token","password")
    .authorities("CLIENT").scopes("openid", "read", "write", "trust").resourceIds("oauth2-resource")
    .redirectUris("http://10.9.6.31:8090/showEmployees").accessTokenValiditySeconds(5000).secret("secret")
    .refreshTokenValiditySeconds(50000);

    }
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManager)
    .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
    ;
    }
    }


    Please help me to figure out this issue



    Access token request (if expired)



        http://10.9.6.31:8091/oauth/token
    Body parameter
    grant_type=refresh_token
    refresh_token=78d2ab82-46a2-4b70-a9e8-e3f9e5ddfec6









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have implemented Spring boot Oauth 2 security it works fine but when I try to get access token (if expired) from refresh token it gives me an error



      {
      "error": "unauthorized",
      "error_description": "admin"
      }


      Console Log



      Handling error: UsernameNotFoundException, admin


      Following is my code



      1.WebSecurityConfigure



      @Configuration
      @EnableWebSecurity
      public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter {

      @Override
      public void configure(WebSecurity web) throws Exception {
      web.ignoring().antMatchers("/resources/**");
      }
      @Override
      protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/user/getEmployeesList")
      .hasAnyRole("USER").anyRequest().authenticated().and().formLogin()
      .permitAll().and().logout().permitAll();
      http.csrf().disable();
      }

      @Override
      public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception {
      authenticationMgr.inMemoryAuthentication().withUser("admin").password("admin")
      .authorities("ROLE_USER");
      ;
      }
      }


      2.AuthorizationServerConfigure



      @Configuration
      @EnableAuthorizationServer
      public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

      @Autowired
      private AuthenticationManager authenticationManager;


      @Override
      public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
      security
      .tokenKeyAccess("permitAll()")
      .checkTokenAccess("isAuthenticated()")
      .allowFormAuthenticationForClients();
      }

      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory().withClient("MagicUser").authorizedGrantTypes("authorization_code", "refresh_token","password")
      .authorities("CLIENT").scopes("openid", "read", "write", "trust").resourceIds("oauth2-resource")
      .redirectUris("http://10.9.6.31:8090/showEmployees").accessTokenValiditySeconds(5000).secret("secret")
      .refreshTokenValiditySeconds(50000);

      }
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
      endpoints.authenticationManager(authenticationManager)
      .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
      ;
      }
      }


      Please help me to figure out this issue



      Access token request (if expired)



          http://10.9.6.31:8091/oauth/token
      Body parameter
      grant_type=refresh_token
      refresh_token=78d2ab82-46a2-4b70-a9e8-e3f9e5ddfec6









      share|improve this question













      I have implemented Spring boot Oauth 2 security it works fine but when I try to get access token (if expired) from refresh token it gives me an error



      {
      "error": "unauthorized",
      "error_description": "admin"
      }


      Console Log



      Handling error: UsernameNotFoundException, admin


      Following is my code



      1.WebSecurityConfigure



      @Configuration
      @EnableWebSecurity
      public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter {

      @Override
      public void configure(WebSecurity web) throws Exception {
      web.ignoring().antMatchers("/resources/**");
      }
      @Override
      protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/user/getEmployeesList")
      .hasAnyRole("USER").anyRequest().authenticated().and().formLogin()
      .permitAll().and().logout().permitAll();
      http.csrf().disable();
      }

      @Override
      public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception {
      authenticationMgr.inMemoryAuthentication().withUser("admin").password("admin")
      .authorities("ROLE_USER");
      ;
      }
      }


      2.AuthorizationServerConfigure



      @Configuration
      @EnableAuthorizationServer
      public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

      @Autowired
      private AuthenticationManager authenticationManager;


      @Override
      public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
      security
      .tokenKeyAccess("permitAll()")
      .checkTokenAccess("isAuthenticated()")
      .allowFormAuthenticationForClients();
      }

      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory().withClient("MagicUser").authorizedGrantTypes("authorization_code", "refresh_token","password")
      .authorities("CLIENT").scopes("openid", "read", "write", "trust").resourceIds("oauth2-resource")
      .redirectUris("http://10.9.6.31:8090/showEmployees").accessTokenValiditySeconds(5000).secret("secret")
      .refreshTokenValiditySeconds(50000);

      }
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
      endpoints.authenticationManager(authenticationManager)
      .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
      ;
      }
      }


      Please help me to figure out this issue



      Access token request (if expired)



          http://10.9.6.31:8091/oauth/token
      Body parameter
      grant_type=refresh_token
      refresh_token=78d2ab82-46a2-4b70-a9e8-e3f9e5ddfec6






      spring spring-boot oauth-2.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 6:17









      Ganesh Gudghe

      574424




      574424
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You might have to send an authorization header in the request to authenticate the client.
          Check OAuth client authentication and Refreshing an access token specifications in OAuth 2.0 specification.






          share|improve this answer





















            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%2f53387279%2fspring-boot-oauth-2-security-get-access-token-from-refresh-tokenif-expired%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








            up vote
            0
            down vote













            You might have to send an authorization header in the request to authenticate the client.
            Check OAuth client authentication and Refreshing an access token specifications in OAuth 2.0 specification.






            share|improve this answer

























              up vote
              0
              down vote













              You might have to send an authorization header in the request to authenticate the client.
              Check OAuth client authentication and Refreshing an access token specifications in OAuth 2.0 specification.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You might have to send an authorization header in the request to authenticate the client.
                Check OAuth client authentication and Refreshing an access token specifications in OAuth 2.0 specification.






                share|improve this answer












                You might have to send an authorization header in the request to authenticate the client.
                Check OAuth client authentication and Refreshing an access token specifications in OAuth 2.0 specification.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 8:18









                RakihthaRR

                18911




                18911






























                    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%2f53387279%2fspring-boot-oauth-2-security-get-access-token-from-refresh-tokenif-expired%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

                    Tonle Sap (See)

                    I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                    Guatemaltekische Davis-Cup-Mannschaft