Javaの春のセキュリティのOAuth2:POSTを介してクライアントの資格情報を受け入れます

ジョナサン:

私はかなり基本的な春のブートを設定していると私は春のセキュリティをインストールしていると私は首尾よく私のAPIを保護するためのOAuth2を設定するために管理しています。

私は数日戻っていくつかの問題を持っていたし、質問尋ね(と答え)私の当たっに関しての/oauth/tokenエンドポイントを。私はすぐに、問題は、私は私の体に私のクライアントの資格情報を送信しようとしていたということであったことを考え出したPOST要求が、トークンエンドポイントは、クライアントの資格情報(受け入れるように春のセキュリティに設定されているclient_idし、secret代わりにHTTP基本認証を経由し)。

OAuth2のAPIを消費するとの私の経験のほとんどは、身体の中で、クライアントの資格情報を送信して関与しているPOST要求と、同じように関数に春のセキュリティを設定することが可能であった場合、私は不思議でしたか?

私は、次の設定オプションを設定するように、成功しませんいくつかの異なるものを試してみたが、私はのOAuth2クライアントを設定するときにのみ使用されているかもしれないように感じます。

security.oauth2.client.clientAuthenticationScheme=form

これは私の認証サーバの構成です。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
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;
import org.springframework.security.oauth2.provider.approval.UserApprovalHandler;
import org.springframework.security.oauth2.provider.token.TokenStore;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private UserApprovalHandler userApprovalHandler;

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Autowired
    private PasswordEncoder passwordEncoder;

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

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(this.tokenStore)
                .userApprovalHandler(this.userApprovalHandler)
                .authenticationManager(this.authenticationManager);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) {
        security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()")
                .passwordEncoder(this.passwordEncoder);
    }
}
ジョナサン:

コメント欄で@chrylisで指摘したように、トリックは使用することですallowFormAuthenticationForClients上のメソッドをAuthorizationServerSecurityConfigurer認証サーバを設定するとき。私の場合、私は自分でこれを持っているAuthorizationServerConfigクラス:

@Override
public void configure(AuthorizationServerSecurityConfigurer security) {
    security.tokenKeyAccess("permitAll()")
            .checkTokenAccess("isAuthenticated()")
            .passwordEncoder(this.passwordEncoder)
            .allowFormAuthenticationForClients();
}

すなわち、このような本体におけるような標準的なパラメータを介してクライアントの資格情報を渡すことを可能にするPOSTスプリングが接合してHTTP基本認証を使用して好むも要求(またはクエリ文字列で)、client_id及びsecret(コロンと一緒に<client_id>:<secret>)、ベース64は、結果をコード、とそれを接頭辞Basicとすることを渡すAuthorizationヘッダので、あなたはこのようなもので終わるでしょう。

Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=191074&siteId=1