로그인 후 자격 증명 풀을 사용하여 AWS 서비스 액세스
사용자가 사용자 풀을 사용하여 로그인한 다음 자격 증명 풀을 사용하여 AWS 서비스에 액세스할 수 있도록 설정할 수 있습니다.
인증 성공 이후 웹 또는 모바일 앱은 Amazon Cognito에서 사용자 풀 토큰을 받습니다. 이러한 토큰을 사용하면 앱이 다른 AWS 서비스에 액세스하도록 허용하는 AWS 자격 증명을 검색할 수 있습니다. 자세한 내용은 Amazon Cognito 자격 증명 풀(페더레이션 자격 증명) 시작하기 섹션을 참조하세요.
자격 증명 풀을 사용자 풀 그룹과 함께 사용하여 AWS 리소스에 대한 액세스를 제어하는 방법에 대한 자세한 내용은 사용자 풀에 그룹 추가 및 역할 기반 액세스 제어 사용 섹션을 참조하세요. 자격 증명 풀 개념 (페더레이션 자격 증명) 에서 자격 증명 풀과 AWS Identity and Access Management에 대한 자세한 내용도 참조하세요.
AWS Management Console을 사용하여 사용자 풀 설정
Amazon Cognito 사용자 풀을 생성하고 사용자 풀 ID와 각 클라이언트 앱의 앱 클라이언트 ID를 기록해 둡니다. 사용자 풀 생성에 대한 자세한 내용은 사용자 풀 시작하기 섹션을 참조하세요.
AWS Management Console을 사용하여 자격 증명 풀 설정
다음 절차에서는 AWS Management Console을 사용하여 자격 증명 풀을 하나 이상의 사용자 풀 및 클라이언트 앱과 통합하는 방법을 설명합니다.
- AWS Management Console
-
자격 증명 풀을 구성하려면
-
Amazon Cognito 콘솔로 이동합니다. 메시지가 표시되면 AWS 자격 증명을 입력합니다.
-
[페더레이션 ID(Federated identities)]를 선택합니다.
-
Amazon Cognito 사용자 풀을 공급자로 사용할 자격 증명 풀의 이름을 선택합니다.
-
[대시보드(Dashboard)] 페이지에서 [자격 증명 풀 편집(Edit identity pool)]을 선택합니다.
-
인증 공급자(Authentication providers) 섹션을 확장합니다.
-
Cognito를 선택합니다.
-
[사용자 풀 ID(User Pool ID)]를 입력합니다.
-
[앱 클라이언트 ID(App Client ID)]를 입력합니다. 콘솔의 [사용자 풀(User pools)] 섹션에서 앱을 생성할 때 받은 것과 동일한 클라이언트 앱 ID여야 합니다.
-
추가 앱 또는 사용자 풀이 있는 경우 [다른 공급자 추가(Add Another Provider)]를 선택하고 각 사용자 풀의 각 앱에 대한 [사용자 풀 ID(User Pool ID)]와 [앱 클라이언트 ID(App Client ID)]를 입력합니다.
-
더 이상 추가할 앱 또는 사용자 풀이 없으면 변경 사항 저장(Save Changes)을 선택합니다. 성공할 경우 [대시보드(Dashboard)] 페이지에 [변경 내용이 저장되었습니다.(Changes saved successfully.)]라는 메시지가 표시됩니다.
자격 증명 풀과 사용자 풀 통합
앱 사용자가 인증되면 자격 증명 공급자의 로그인 맵에 해당 사용자의 자격 증명 토큰을 추가합니다. 공급자 이름은 Amazon Cognito 사용자 풀 ID에 따라 다릅니다. 공급자 이름은 다음과 같은 구조를 갖습니다.
cognito-idp.<region>
.amazonaws.com/<YOUR_USER_POOL_ID>
<region>
의 값은 사용자 풀 ID의 리전과 동일합니다. 예: cognito-idp.us-east-1.amazonaws.com/us-east-1_123456789
.
- JavaScript
-
var cognitoUser = userPool.getCurrentUser();
if (cognitoUser != null) {
cognitoUser.getSession(function(err, result) {
if (result) {
console.log('You are now logged in.');
// Add the User's Id Token to the Cognito credentials login map.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'YOUR_IDENTITY_POOL_ID
',
Logins: {
'cognito-idp.<region>
.amazonaws.com/<YOUR_USER_POOL_ID>
': result.getIdToken().getJwtToken()
}
});
}
});
}
- Android
-
cognitoUser.getSessionInBackground(new AuthenticationHandler() {
@Override
public void onSuccess(CognitoUserSession session) {
String idToken = session.getIdToken().getJWTToken();
Map<String, String> logins = new HashMap<String, String>();
logins.put("cognito-idp.<region>
.amazonaws.com/<YOUR_USER_POOL_ID>
", session.getIdToken().getJWTToken());
credentialsProvider.setLogins(logins);
}
});
- iOS - objective-C
-
AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil];
AWSCognitoIdentityUserPoolConfiguration *userPoolConfiguration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:@"YOUR_CLIENT_ID
" clientSecret:@"YOUR_CLIENT_SECRET
" poolId:@"YOUR_USER_POOL_ID
"];
[AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:userPoolConfiguration forKey:@"UserPool"];
AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@"YOUR_IDENTITY_POOL_ID
" identityProviderManager:pool];
- iOS - swift
-
let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: "YOUR_CLIENT_ID
", clientSecret: "YOUR_CLIENT_SECRET
", poolId: "YOUR_USER_POOL_ID
")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithConfiguration(serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: "UserPool")
let pool = AWSCognitoIdentityUserPool(forKey: "UserPool")
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YOUR_IDENTITY_POOL_ID
", identityProviderManager:pool)