【问题标题】:Invalid Access Token Spring Boot Resource Server无效的访问令牌 Spring Boot 资源服务器
【发布时间】:2018-12-26 21:07:43
【问题描述】:

我有一个 Spring Boot 资源服务器,如下所示:

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
}

还有这样的端点:

@RestController
public class TestResourceOne {

    private static final Logger log = Logger.getLogger(TestResourceOne.class);

    @RequestMapping(value = "/calcsqrt")
    public Double calcSqtr(@RequestParam("value") Double value) {
        return Math.sqrt(value);
    }

    @RequestMapping(value = "/sum")
    public Double calcSqtr(@RequestParam("value1") Double value1, @RequestParam("value2") Double value2) {
        return value1 + value2;
    }
}

我的授权服务器位于 Azure AD 中,因此当我调用此端点“/calcsqrt”时,我传递了 Azure 生成的承载令牌。这是我的要求:

GET /serviceone/calcsqrt?value=3 HTTP/1.1
Host: localhost:8080
Authorization: Bearer MY_ACCESS_TOKEN_HERE
Cache-Control: no-cache
Postman-Token: ef5d493c-39f1-4bc4-9084-4ea510ac1255

但我总是从 spring 收到以下错误:

{
    "error": "invalid_token",
    "error_description": "Invalid access token: MY_ACCESS_TOKEN_HERE"
}

【问题讨论】:

  • 您在资源服务器上使用的是哪个访问令牌转换器?
  • 我没有使用任何转换器
  • 您需要使用一些令牌服务(例如 RemoteTokenServices)来验证您的令牌。

标签: spring spring-boot oauth-2.0


【解决方案1】:

看来您的资源配置类是错误的。我已经实现了这样的资源配置

package com.ig.user.config;

import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
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;

/**
 * @author Jai
 *
 */
@Configuration
@EnableResourceServer
@EnableWebSecurity
public class ResourceConfig extends ResourceServerConfigurerAdapter {

    private final String userInfoUri = "url";

    private final String clientId = "foo";

    @Override
    public void configure(final ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId("user");
    }

    @Override
    public void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/api/v1/user/activate/**").permitAll()//
                .anyRequest().authenticated();
    }

    @Primary
    @Bean
    public UserInfoTokenServices tokenService() {
        final UserInfoTokenServices tokenService = new UserInfoTokenServices(userInfoUri, clientId);
        return tokenService;
    }
}

EDIT-1

  • userInfoUri 是授权该资源的 URL
  • resourceId 对于每个资源服务器,我们必须创建一个 resourceId,并且我们必须将相同的 id 保存在您存储客户端详细信息的位置

希望对你有所帮助

【讨论】:

  • 你能解释一下resourceID和UserInfoTokenServices吗?
  • 创建资源服务器时,必须创建resource-id。您必须使用存储客户详细信息的相同资源 ID
  • 关于 userInfoUri 。在 Oauth2 我有 /token 和 /authorize ,我应该在这里使用哪个 URL?
猜你喜欢
  • 2021-12-26
  • 2020-11-03
  • 2019-10-03
  • 2019-05-27
  • 2016-08-17
  • 1970-01-01
  • 2020-12-29
  • 2018-05-12
  • 2023-04-02
相关资源
最近更新 更多