java jwe/jws,在Spring Boot中使用Keycloak实现JWT,JWE和JWS(带签名的JWT)

I try to implement a simple OAuth2 "Client Authentication with Signed JWT" Demo App using Spring Boot and Keycloak as AuthService.

The idea is:

one secured REST service "The Producer"

offering an endpoint GET /person for all users/principals with the role "read_person"

offering an endpoint POST /person for all users/principals with the role "write_person"

another (unsecured) REST service "The Consumer"

offering an enpoint /api open for everybody

calling internal the "producer" viaFeignclient using an RequestInterceptor to pass the AccessToken (signed JWT / JWS)

I read about the docs:

saying:

Once the client application is started, it allows to download its public >key in JWKS format using a URL such as http://myhost.com/myapp/k_jwks, >assuming that http://myhost.com/myapp is the base URL of your client >application. This URL can be used by Keycloak (see below).

During authentication, the client generates a JWT token and signs it with >its private key and sends it to Keycloak in the particular backchannel >request (for example, code-to-token request) in the client_assertion >parameter.

I googled a lot to find tutorials/demos or docs about this topic but failed so far.

So here my questions:

How do I implement this "k_jwk" endpoint? Do I simple build a @RestController by myself in "the Producer"? How do I configure Keycloak to get aware of this URL?

How do I implement my "Consumer" to get fresh signed JWT from Keycloak?

Update

Removed irritating PS statement.

解决方案

You don't need to implement the k_jwk endpoint, this is handled by the adapter. Keycloak will by default look at http:///your.app.com/k_jwk(but if needed you can override that in the console).

Then you need to configure your Spring Boot client, just use the same properties as the keycloak.json but in the application.properties format:

...

keycloak.credentials.jwt.client-keystore-file=classpath:keystore-client.jks

keycloak.credentials.jwt.client-keystore-type=JKS

etc ...

You need a token to call the producerbut as you said the entry point will be an insecured endpoint so you might want to use a Service Account for this.

I hope this will help.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值