spring-security-oauth-cognito
Spring Securityを使用してAmazon Cognitoで認証する
-
link:/category/security-2/ [セキュリティ]
1. 前書き
このチュートリアルでは、https://www.baeldung.com/security-spring [Spring Security]のOAuth 2.0サポートを使用してhttps://aws.amazon.com/cognitoで認証する方法を見ていきます。 / [Amazon Cognito]。
途中で、Amazon Cognitoとは何か、それがサポートするhttps://oauth.net/2/[OAuth 2.0]フローの種類について簡単に説明します。
最後に、簡単な1ページのアプリケーションを作成します。 空想は何もない。
2. Amazon Cognitoとは何ですか?
-
Cognitoは、複数のデバイスでアプリのユーザーデータを簡単に管理できるユーザーIDおよびデータ同期サービス*です。
Amazon Cognitoを使用すると、次のことができます。
-
アプリケーションのユーザーを作成、認証、および承認する
-
他のパブリックを使用するアプリのユーザーのIDを作成する
IDプロバイダー Google、Facebook、Twitterなど -
アプリのユーザーデータをキーと値のペアで保存する
3. セットアップ
3.1. Amazon Cognitoセットアップ
アイデンティティプロバイダーとして、Cognitoはhttps://oauth.net/2/grant-types/[_authorization_code、implicit、_および_client_credentials_ grants]をサポートしています。 ここでは、_authorization_code_付与タイプを使用するように設定してみましょう。
まず、Cognitoのセットアップが少し必要です。
-
https://docs.aws.amazon.com/cognito/latest/developerguide/tutorial-create-user-pool.html [作成
ユーザープール] -
https://docs.aws.amazon.com/cognito/latest/developerguide/signing-up-users-in-your-app.html [追加
ユーザー] –このユーザーを使用して、Springアプリケーションにログインします -
https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-client-apps.html [作成
アプリクライアント] -
アプリケーションクライアントの構成で、* _ CallbackURL_がSpring構成ファイルの_redirectUriTemplate_ *と一致することを確認します。 私たちの場合、これは次のようになります。
http://localhost:8080/login/oauth2/code/cognito
_Allowed OAuth flow_は_Authorization code grant._である必要があります。その後、同じページで_Allowed OAuth scope_を_openidに設定する必要があります。
3.2. 春のセットアップ
OAuth 2.0ログインを使用するため、適切なSpring Security依存関係をアプリケーションに追加する必要があります。
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-boot-starter-security-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
そして、すべてを一緒にバインドするための設定が必要になります。
spring:
security:
oauth2:
client:
registration:
cognito:
clientId: clientId
clientSecret: clientSecret
scope: openid
redirectUriTemplate: "http://localhost:8080/login/oauth2/code/cognito"
clientName: cognito-client-name
provider:
cognito:
issuerUri: https://cognito-idp.{region}.amazonaws.com/{poolId}
usernameAttribute: cognito:username
**それで、SpringとAmazon Cognitoをセットアップする必要があります! **チュートリアルの残りの部分では、いくつかのルーズエンドを結び付けています。
4. ランディングページを追加する
次に、単純なlink:/thymeleaf-in-spring-mvc[Thymeleaf]ランディングページを追加して、ログインしたことを確認します。
<div>
<h1 class="title">OAuth 2.0 Spring Security Cognito Demo</h1>
<div sec:authorize="isAuthenticated()">
<div class="box">
Hello, <strong th:text="${#authentication.name}"></strong>!
</div>
</div>
<div sec:authorize="isAnonymous()">
<div class="box">
<a class="button login is-primary" th:href="@{/oauth2/authorization/cognito}">
Log in with Amazon Cognito</a>
</div>
</div>
</div>
簡単に言えば、ログインするとユーザー名が表示され、ログインしていない場合はログインリンクが表示されます。 *設定ファイルから_cognito_部分を選択するため、リンクがどのように見えるかに注意してください。*
そして、*アプリケーションのルートをウェルカムページに結び付けます:*
@Configuration
public class CognitoWebConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
5. アプリを実行する
これは、authに関連するすべてを実行するクラスです。
@SpringBootApplication
public class SpringCognitoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCognitoApplication.class, args);
}
}
*これでアプリケーションを開始できます*、http:// localhost:8080、_にアクセスして、ログインリンクをクリックします。
6. 結論
このチュートリアルでは、Spring SecurityとAmazon Cognitoをいくつかの簡単な構成で統合する方法を検討しました。 そして、ほんの数個のコードですべてを組み立てます。
いつものように、この記事で紹介したコードはhttps://github.com/eugenp/tutorials/tree/master/spring-5-security-cognito[Github上]で入手できます。