キークロークとのQuarkusセキュリティ統合を試していました
ここに私の残りのエンドポイントがあります
@Path("/jwt")
@RequestScoped
public class JWTRestController {
@Inject
@Claim(standard = Claims.preferred_username)
Optional<JsonString> currentUsername;
@GET
@Path("/user")
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed({"user"})
public String userData() {
return "data for user ";
}
@GET
@Path("/admin")
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed({"admin"})
public String adminData() {
return "data for admin ";
}
}
私のアプリケーションのプロパティ
# Configuration file
quarkus.http.port=8082
# MP-JWT Config
mp.jwt.verify.publickey.location=http://localhost:8180/auth/realms/demo/protocol/openid-connect/certs
mp.jwt.verify.issuer=http://localhost:8180/auth/realms/demo
quarkus.smallrye-jwt.auth-mechanism=MP-JWT
quarkus.smallrye-jwt.realmName=quarkus-keycloak-demo
quarkus.smallrye-jwt.enabled=true
ポート8180のローカル開発マシンでキークロークのインスタンスを実行していますキークロークの必要条件をすべて実行し、レルム、ユーザー、ロールを作成しました
次のようにキークロークからトークンを取得できます-
export access_token=$(\
curl -X POST http://localhost:8180/auth/realms/demo/protocol/openid-connect/token \
--user demo-client:e0da2ad7-5f4c-49b3-ae54-dbd7a28d532a \
-H 'content-type: application/x-www-form-urlencoded' \
-d 'username=user&password=user&grant_type=password' | jq --raw-output '.access_token' \
)
しかし、レストポイント/jwt/userにアクセスしようとすると問題が発生します
curl -v -H "Authorization: Bearer $access_token" http://localhost:8082/jwt/user
その結果、
< HTTP/1.1 403 Forbidden
< Content-Length: 9
< Content-Type: text/plain;charset=UTF-8
<
* Connection #0 to host localhost left intact
Forbidden* Closing connection 0
トークンの詳細は次のとおりです(私はjwt.ioデバッガーを使用してトークン内を調べます)
どんな助けでも大歓迎です