此问题可能是由于AWS ALB未配置身份验证策略而导致的。可以使用AWS自带的Cognito或自行实现认证授权策略。另外,也可以在Keycloak上配置SSL证书和ALB的安全组规则以确保令牌有效性。
示例代码:
# 配置Cognito User Pool
import boto3
client = boto3.client('cognito-idp')
response = client.create_user_pool(
PoolName='my-user-pool',
Policies={
'PasswordPolicy': {
'MinimumLength': 8,
'RequireUppercase': True,
'RequireLowercase': True,
'RequireNumbers': True,
'RequireSymbols': True
}
}
)
# 配置Cognito App Client
response = client.create_user_pool_client(
UserPoolId='STRING_VALUE',
ClientName='my-app-client',
GenerateSecret=True,
ExplicitAuthFlows=[
'ALLOW_USER_PASSWORD_AUTH',
'ALLOW_REFRESH_TOKEN_AUTH',
]
)
# 生成证书
keytool -genkey -alias keycloak -keyalg RSA -keystore keycloak.jks
# 将证书导入Keycloak
keytool -importkeystore -srckeystore keycloak.jks -destkeystore keycloak.jks -deststoretype pkcs12
[
{
"IpProtocol": "TCP",
"FromPort": 80,
"ToPort": 80,
"IpRanges": [{"CidrIp": "0.0.0.0/0"}]
},
{
"IpProtocol": "TCP",
"FromPort": 443,
"ToPort": 443,
"IpRanges": [{"CidrIp": "0.0.0.0/0"}]
},
{
"IpProtocol": "TCP",
"FromPort": 8080,
"ToPort": 8080,
"IpRanges": [{"CidrIp": "0.0.0.0/0"}]
}
]