私はかなり基本的な春のブートを設定していると私は春のセキュリティをインストールしていると私は首尾よく私の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