为什么spring-security-oauth2在令牌端点抛出未经授权的错误?如何打开对端点的访问?

为什么spring-security-oauth2在令牌端点抛出未经授权的错误?如何打开对端点的访问?,spring-security,spring-security-oauth2,Spring Security,Spring Security Oauth2,我正在尝试使用SpringSecurityOAuth2框架创建一个示例授权服务器。与任何其他与spring相关的示例相比,这些教程令人困惑 更新:如果您正在寻找一个有效的解决方案,请转到我的答案。忽略下面的代码 当我调用令牌问题端点时,抛出了以下错误 { "error": "unauthorized", "error_description": "Full authentication is required to access this resource" } 这是我的设置(

我正在尝试使用SpringSecurityOAuth2框架创建一个示例授权服务器。与任何其他与spring相关的示例相比,这些教程令人困惑

更新:如果您正在寻找一个有效的解决方案,请转到我的答案。忽略下面的代码

当我调用令牌问题端点时,抛出了以下错误

{
    "error": "unauthorized",
    "error_description": "Full authentication is required to access this resource"
}
这是我的设置(使用Groovy)。我使用的是spring-security-oauth2:2.3.4.RELEASE、spring-cloudsecurity:2.0.1.RELEASE和boot:2.1.1.RELEASE

@Configuration
@CompileStatic
class OAuth2ClientSpringConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    AuthenticationManager authenticationManager

    @Autowired
    UserDetailsService userDetailsService

    // register clients
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient('clientone')
                .secret('secret')
                .authorizedGrantTypes('password')
                .scopes('one', 'two')
    }

    //use default auth manager and user details service
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService)
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients()
        security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()")
                .allowFormAuthenticationForClients()  //<--- update
    }
}
邮差设置

  • 在两个位置(授权标头和普通标头)中添加了客户端凭据


  • 使用正文表单参数发送用户凭据


当我点击时遇到以下错误


我研究过不同的教程,但没有找到答案。任何投入都会有帮助

根据规范,
令牌颁发端点必须受到保护

您必须提供
client\u id
client\u secret
作为参数(表单)或http基本授权标头

直接取自规范

 POST /token HTTP/1.1
 Host: server.example.com
 Content-Type: application/x-www-form-urlencoded

 grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
 &client_id=s6BhdRkqt3&client_secret=7Fjfp0ZBr1KtDRbnfVdmIw
请复习

为什么我们不把客户机从等式中去掉一点,只关注您的令牌端点呢

curl是您的朋友,下面是一个表单示例(针对CloudFoundry UAA local验证的工作示例,其中客户端机密是空字符串)

以及使用http basic

curl -v \
    -H "Content-Type=application/x-www-form-urlencoded" \
    -H "Accept=application/json" \
    -d "grant_type=password" \
    -d "client_id=cf" \
    -d "response_type=token" \
    -d "username=marissa" \
    -d "password=koala" \
    -u "cf:" \
    http://localhost:8080/uaa/oauth/token

您能否使用实际数据运行这些命令,并让我们知道这些命令(可能会更新您的问题),并让我们知道结果。您知道,我们不知道您的UI应用程序实际在做什么,使用curl可以让我们从问题中消除这一点。

根据规范,
令牌问题端点必须受到保护

您必须提供
client\u id
client\u secret
作为参数(表单)或http基本授权标头

直接取自规范

 POST /token HTTP/1.1
 Host: server.example.com
 Content-Type: application/x-www-form-urlencoded

 grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
 &client_id=s6BhdRkqt3&client_secret=7Fjfp0ZBr1KtDRbnfVdmIw
请复习

为什么我们不把客户机从等式中去掉一点,只关注您的令牌端点呢

curl是您的朋友,下面是一个表单示例(针对CloudFoundry UAA local验证的工作示例,其中客户端机密是空字符串)

以及使用http basic

curl -v \
    -H "Content-Type=application/x-www-form-urlencoded" \
    -H "Accept=application/json" \
    -d "grant_type=password" \
    -d "client_id=cf" \
    -d "response_type=token" \
    -d "username=marissa" \
    -d "password=koala" \
    -u "cf:" \
    http://localhost:8080/uaa/oauth/token

您能否使用实际数据运行这些命令,并让我们知道这些命令(可能会更新您的问题),并让我们知道结果。你看,我们不知道你的UI应用程序实际上在做什么,使用curl可以让我们从问题中消除这一点。

有几个在线教程,但其中一些没有提到他们正在使用Boot 1.x。这就是我在将这些指令与Boot2.x结合使用时遇到的问题。这些教程可能对Boot2.x有效,但事情已经够混乱的了,我想不出一个可行的模型

这里有一个boot2.x的有效解决方案。我从下面的代码示例重新开始

导入
spring-security-oauth2-autoconfigure
依赖项。 将
@EnableAuthorizationServer
添加到主类。这就是最简单的工作模型所需要的。无需添加AuthorizationServerConfigurerAdapter等,因为它是使用application.yml中的数据自动配置的。查看上面github链接中的application.yml

这是邮递员的配置

从application.yml复制示例客户端id和secret,并将其作为身份验证头提供

从启动日志复制生成的密码,并将其与其他属性一起放入正文数据中,如图所示

就这样。点击,你会看到下面的内容


网上有几个教程,但其中一些没有提到他们正在使用Boot 1.x。这就是我在将这些指令与Boot2.x结合使用时遇到的问题。这些教程可能对Boot2.x有效,但事情已经够混乱的了,我想不出一个可行的模型

这里有一个boot2.x的有效解决方案。我从下面的代码示例重新开始

导入
spring-security-oauth2-autoconfigure
依赖项。 将
@EnableAuthorizationServer
添加到主类。这就是最简单的工作模型所需要的。无需添加AuthorizationServerConfigurerAdapter等,因为它是使用application.yml中的数据自动配置的。查看上面github链接中的application.yml

这是邮递员的配置

从application.yml复制示例客户端id和secret,并将其作为身份验证头提供

从启动日志复制生成的密码,并将其与其他属性一起放入正文数据中,如图所示

就这样。点击,你会看到下面的内容


感谢您的回复,可能会重复。我添加了
allowFormAuthenticationForClients()
,但仍然存在相同的错误。您还必须将
client\u id
添加到正文(而不是标题)。谢谢。是的,我也试过了。没有帮助。现在,client_id和client_secret被添加到body表单数据、头和授权头中。没有提到密码编码器。有意思。可能是重复的感谢回复。我添加了
allowFormAuthenticationForClients()
,但仍然存在相同的错误。您还必须将
client\u id
添加到正文(而不是标题)。谢谢。是的,我也试过了。没有帮助。现在,client_id和client_secret被添加到body表单数据、头和授权头中。没有提到密码编码器。很有趣,谢谢你的回复。我正在发送多个客户端id和客户端机密