スプリングブートとスプリングセキュリティを使用すると、ユーザーの詳細が主要なオブジェクトで使用できます。しかし、それはgetName()
のような詳細を取り出す方法はほんのわずかです。春のセキュリティ:プリンシパルからの詳細を取得するには?
他の詳細はどのように取得できますか?それがこれを返す
@SpringBootApplication
@RestController
public class DemoOAuth2Application {
@RequestMapping("/user")
public Principal user(Principal principal) {
return principal;
}
public static void main(String[] args) {
SpringApplication.run(DemoOAuth2Application.class, args);
}
}
現在、私のクラスは次のようになり、
{
"authorities": [
{
"authority": "ROLE_USER"
}
],
"details": {
"remoteAddress": "0:0:0:0:0:0:0:1",
"sessionId": "43Fxxxxxx",
"tokenValue": "ya29.xxxxxxxxx",
"tokenType": "Bearer",
"decodedDetails": null
},
"authenticated": true,
"userAuthentication": {
"authorities": [
{
"authority": "ROLE_USER"
}
],
"details": {
"id": "106xxxxx",
"email": "[email protected]",
"verified_email": true,
"name": "xxxx yyyyyy",
"given_name": "xxxxxx",
"family_name": "yyyyy",
"link": "https://plus.google.com/xxxxxxxxxx",
"picture": "https://lh5.googleusercontent.com/xxxxxx/photo.jpg",
"locale": "en"
},
"authenticated": true,
"principal": "106xxxxx",
"credentials": "N/A",
"name": "106xxxxxxx"
},
"principal": "106xxxxxxxxxxx",
"clientOnly": false,
"credentials": "",
"oauth2Request": {
"clientId": "xxxxxxxxx.apps.googleusercontent.com",
"scope": [],
"requestParameters": {},
"resourceIds": [],
"authorities": [],
"approved": true,
"refresh": false,
"redirectUri": null,
"responseTypes": [],
"extensions": {},
"refreshTokenRequest": null,
"grantType": null
},
"name": "106xxxxxxxxxx"
}
しかし、その代わりに、すべてのデータを返す、私は私が必要とする特定のデータだけを返すようにしたいと思います。そのデータ(具体的には電子メール、名前、リンク、画像)を取得するにはどうすればいいですか?
どのようにデータをコピーしますか?プリンシパルオブジェクトは、その内容を読み取るためのメソッドを提供しません。 –
申し訳ありませんが、私はまだそれを取得していません。別のクラスを作成し、それをPrincipleからキャストして使用することをお勧めしますか?その場合、それらを開示していない場合、私はどのようにPrincipleからその詳細にアクセスしますか?また、Principleには50以上の実装があり、どれが使用されているか把握する方法はわかりません。 –
おそらくアクセサーメソッドを持つ実装にPrincipalオブジェクトをキャストすることを提案しました。実装クラスを取得するには、デバッグモードでサーバーを起動するか、principal.class.getName()のログステートメントを入力します。 – Deadron