如何使用Post方法获取OAuth2访问令牌

use*_*005 3 authentication cxf spring-security java-ee oauth-2.0

我试图用Spring安全配置oauth2,我举了一个例子

https://github.com/neel4software/SpringSecurityOAuth2
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我需要使用post方法而不是get获取oauth2访问令牌,下面是如何在spring security xml中配置令牌端点

这是正在运行的GET方法URL端点

http://localhost:8080/SpringRestSecurityOauth/oauth/token?grant_type=password&client_id=restapp&client_secret=restapp&username=beingjavaguys&password=spring@java 
Run Code Online (Sandbox Code Playgroud)

这就是spring security xml中完成端点配置的方式

<http pattern="/oauth/token" create-session="stateless"
        authentication-manager-ref="clientAuthenticationManager"
        xmlns="http://www.springframework.org/schema/security">
        <intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
        <anonymous enabled="false" />
        <http-basic entry-point-ref="clientAuthenticationEntryPoint" />
        <!-- include this only if you need to authenticate clients via request 
            parameters -->
        <custom-filter ref="clientCredentialsTokenEndpointFilter"
            after="BASIC_AUTH_FILTER" />
        <access-denied-handler ref="oauthAccessDeniedHandler" />
    </http>
Run Code Online (Sandbox Code Playgroud)

为了使用POST方法访问它,我试图使用以下端点访问它

http://localhost:8080/SpringRestSecurityOauth/oauth/token
Run Code Online (Sandbox Code Playgroud)

将给定参数作为休息客户端中的有效负载(我也尝试将这些参数作为标头参数提供)

grant_type: password
client_id: restapp
client_secret: restapp
username: beingjavaguys
password: spring@java
Run Code Online (Sandbox Code Playgroud)

但每次我尝试使用rest客户端访问它时,总是提示我输入我不知道的用户名和密码.我给用户名为"beingjavaguys",密码为"spring @ java",但没有用.我是第一次使用Oauth2

小智 6

要使用密码grant_type生成oauth 2.0令牌,以下是该过程

credential = base64Encode(clientId:secret)
Run Code Online (Sandbox Code Playgroud)

凭证的价值是 cmVzdGFwcDpyZXN0YXBw

现在这里是您的请求应该是什么样子,

POST /SpringRestSecurityOauth/oauth/token

HOST: localhost:8080
Authorization: Basic cmVzdGFwcDpyZXN0YXBw
Content-Type: application/x-www-form-urlencoded
Payload:
grant_type=password&username=beingjavaguys&password=spring%40java
Run Code Online (Sandbox Code Playgroud)