Java Spring OAuth2:始终返回无效令牌

Java Spring OAuth2:始终返回无效令牌,java,spring,spring-boot,oauth-2.0,spring-security-oauth2,Java,Spring,Spring Boot,Oauth 2.0,Spring Security Oauth2,我试图实现一个OAuth2服务器和一个具有Spring Boot的资源服务器。 我可以请求令牌,但如果我使用它们从资源服务器请求资源,结果总是“无效令牌” 这是授权服务器配置: @配置 @EnableAuthorizationServer 公共类AuthserverConfiguration扩展了AuthorizationServerConfigurerAdapter{ @自动连线 私人AuthenticationManager AuthenticationManager; @凌驾 public

我试图实现一个OAuth2服务器和一个具有Spring Boot的资源服务器。 我可以请求令牌,但如果我使用它们从资源服务器请求资源,结果总是“无效令牌”

这是授权服务器配置:

@配置
@EnableAuthorizationServer
公共类AuthserverConfiguration扩展了AuthorizationServerConfigurerAdapter{
@自动连线
私人AuthenticationManager AuthenticationManager;
@凌驾
public void configure(AuthorizationServerEndpointsConfigurer端点)引发异常{
endpoints.authorizationCodeServices(authorizationCodeServices())
//注入Spring安全身份验证管理器(在WebSecurity配置中设置)
.authenticationManager(authenticationManager)
.tokenStore(tokenStore());
}
@凌驾
public void configure(AuthorizationServerSecurityConfigure安全)引发异常{
//为/oauth/check_令牌和/oauth/token_密钥端点配置安全性
security.tokenKeyAccess(“permitAll()”)
.checkTokenAccess(“permitAll()”);//应进行身份验证()
}
@凌驾
公共无效配置(ClientDetailsServiceConfigurer客户端)引发异常{
clients.inMemory()
.withClient(“clientId”).secret(“noop}clientsecret”)
.authorizedGrantTypes(“授权码”、“刷新令牌”、“密码”)
.范围(“读取”);
}
@豆子
公共令牌库令牌库(){
返回新的InMemoryTokenStore();
}
@豆子
受保护的AuthorizationCodeServices AuthorizationCodeServices(){
//创建授权代码,并将代码存储在内存中。
返回新的InMemoryAuthorizationCodeServices();
}
}
Web安全配置为:

@EnableWebSecurity
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 20)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user").password("{noop}password").roles("USER");
        /* for now testing with inMemoryAuthentication, later I want to use: 
         * auth.userDetailsService(userDetailsService); */
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
            .and()
            .formLogin().loginPage("/login").permitAll().failureUrl("/login?error")
            .and()
            .authorizeRequests().anyRequest().authenticated();
    }

    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
以及UserInfoController:

@RestController
public class UserInfoController {

    @RequestMapping(value = "/user")
    public Principal userInfo(@AuthenticationPrincipal Principal user) {
        return user;
    }
}
pom.xml包含以下依赖项:+

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.0.5.RELEASE</version>
    </dependency>
</dependencies>
其中8090是上述认证服务器的端口

卷曲测试:

> curl clientId:clientsecret@localhost:8090/oauth/token -d grant_type=password -d username=user -d password=password -d scope=read`
{"access_token":"1cdd6dc2-42fe-4b55-b16d-78d189a88cc4","token_type":"bearer","refresh_token":"d59d78b5-43c8-4d12-b4ee-007da8548744","expires_in":43199,"scope":"read"}

> curl -H 'Authorization: Bearer 1cdd6dc2-42fe-4b55-b16d-78d189a88cc4' 'localhost:8090/oauth/check_token?token=1cdd6dc2-42fe-4b55-b16d-78d189a88cc4'
{"exp":1561956415,"user_name":"user","authorities":["ROLE_USER"],"client_id":"clientId","scope":["read"]}+ set +o xtrace
该令牌似乎有效且可识别(如果我篡改该令牌,则会出现另一个错误:“Unrecogned token”)

但如果我尝试使用令牌从资源服务器请求数据:

> curl -H 'Authorization: Bearer 1cdd6dc2-42fe-4b55-b16d-78d189a88cc4' localhost:8081/products
{"error":"invalid_token","error_description":"Invalid access token: 1cdd6dc2-42fe-4b55-b16d-78d189a88cc4"}
这个错误是非常一致的,我尝试了许多不同的配置更改,甚至添加了TokenServices、UserDetailsService等的自定义实现

在调试日志中,每当我通过curl执行请求时,我都会在资源服务器的输出中找到以下行:

Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@2b139cc0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
这让我感到困惑,因为在我的理解中,委托人不应该是匿名用户


如何解决此问题并从资源服务器请求数据?非常感谢您的帮助。

如果您使用的是2.0.0以上的spring boot starter家长版。客户端密码应在AuthserverConfiguration类中加密。您的意思是这可能是问题所在?还是为了一般安全?当我使用spring boot版本2及更高版本实施时,这对我来说是个问题,请检查,谢谢,这是解决方案的主要部分。如果您使用的是2.0.0以上的SpringBootStarter父级,我将很快发布更详细的描述。客户端密码应在AuthserverConfiguration类中加密。您的意思是这可能是问题所在?还是为了一般安全?当我使用spring boot版本2及更高版本实施时,这对我来说是个问题,请检查,谢谢,这是解决方案的主要部分。我将很快发布更详细的描述。
Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@2b139cc0: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS