'Getting Invalid access token

I have one rest api and i'm trying to generate token for same. when i call validateUser it is providing one proper token but when i call my api, it is giving an error of Invalid access token

this is my authserver main class :

import java.security.Principal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableResourceServer
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @RequestMapping("/validateUser")
    public Principal user(Principal user) {
        return user;
    }
    
}

Here i have provided all config Adaptor implementation :

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
    }
    
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {     
        security.allowFormAuthenticationForClients().checkTokenAccess("permitAll()");       
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("ABC_XYZ").secret("ABC_XYZ_1357")
                .accessTokenValiditySeconds(3600)
                .refreshTokenValiditySeconds(3600)
                .authorizedGrantTypes("client_credentials","authorization_code", "refresh_token", "password").scopes("read", "write");

    }
}

in application.properties file i have declared port as 8086 for this authorisation server

Now in Resource project I have added url for server in Application.properties file as below

security.oauth2.resource.userInfoUri=http://localhost:8086/validateUser

this one is my sample API which i want to call :

@RestController
public class TrueUpController {


    @RequestMapping(value = "/saveData",method=RequestMethod.POST)
    public String executeProcedure(@RequestBody Request request) {
        
        return "Success";
        }
    }

When i call validateUser for tokan i'm getting proper token

you can see here

But when i pass same token to api it is giving Error error

I have tried some solution but unable to solve, if anyone knows this error please guide me to solve this one.



Solution 1:[1]

Try change the secret value in method configure of AuthorizationServer. I think you have an extra 'c' character.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gtrev