简体   繁体   English

如何获取和使用 JHipster 微服务的身份验证令牌 JWT

[英]How to get and use authentication token for JHipster Microservice JWT

I created a JHipster microservice App with JWT authentication (I only have the "backend", no Angular GUI).我创建了一个具有 JWT 身份验证的 JHipster 微服务应用程序(我只有“后端”,没有 Angular GUI)。

In my application-dev.yml, I have the following lines:在我的 application-dev.yml 中,我有以下几行:

jhipster:
    security:
      authentication:
        jwt:
            secret: password
            # Token is valid 24 hours
            token-validity-in-seconds: 86400
            token-validity-in-seconds-for-remember-me: 2592000

How can I access the API with a client like "Restlet" (Google Chrome extension).如何使用“Restlet”(Google Chrome 扩展)之类的客户端访问 API。

I read something about getting the token when accessing /api/authenticate but it didn't work ( JHipster authentication using Postman and JWT )我读了一些关于访问 /api/authenticate 时获取令牌的内容,但它不起作用( JHipster 身份验证使用 Postman 和 JWT

Where can I retrieve the JWT Token and how to use it in subsequent requests? JWT Token在哪里获取,后续请求如何使用?

You've chosen a microservices architecture: so now you need a registry and a gateway( read the doc ). 您选择了微服务架构:所以现在您需要一个注册表和一个网关(阅读文档 )。

You get a token by authenticating against the gateway then you use this token by passing it on each request with Authorization http header. 您通过对网关进行身份验证来获取令牌,然后通过使用Authorization http标头在每个请求上传递令牌来使用此令牌。

You need a token from the JHipster registry.您需要来自 JHipster 注册表的令牌。 Use a Post to: http://[JhipsterRegistryIP]:[JHPORT]/api/authenticate使用 Post 到:http://[JhipsterRegistryIP]:[JHPORT]/api/authenticate

with in header:在 header 中:

Content-Type: application/json内容类型:application/json

and in body:在身体里:

{"password": "YOURADMINPASSWORD","rememberMe": true,"username": "admin"} {“密码”:“YOURADMINPASSWORD”,“rememberMe”:真,“用户名”:“管理员”}

You get a response with token like this:您会收到这样的令牌响应:

{ "id_token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJST0xFX0FETUlOLFJPTEVfVVNFUiIsImV4cCI6MTY1MTMxMjE3M30.ywNnrHv-QTjeCDEAjxYhfpOabzmYpSsJufQYlL-dV7NB683rFiCtFvwTYTZuqlu6XBMEKI13_SLFZM3eF8kxgQ" } { "id_token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJST0xFX0FETUlOLFJPTEVfVVNFUiIsImV4cCI6MTY1MTMxMjE3M30.ywNnrHv-QTjeCDEAjxYhfpOabzmYpSsJufQYlL-dV7NB683rFiCtFvwTYTZuqlu6XBMEKI13_SLFZM3eF8kxgQ" }

Now you can make a request to your microservice with in header:现在您可以使用 header 向您的微服务发出请求:

Authorization: "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJST0xFX0FETUlOLFJPTEVfVVNFUiIsImV4cCI6MTY1MTMxMjE3M30.ywNnrHv-QTjeCDEAjxYhfpOabzmYpSsJufQYlL-dV7NB683rFiCtFvwTYTZuqlu6XBMEKI13_SLFZM3eF8kxgQ" Authorization: "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJST0xFX0FETUlOLFJPTEVfVVNFUiIsImV4cCI6MTY1MTMxMjE3M30.ywNnrHv-QTjeCDEAjxYhfpOabzmYpSsJufQYlL-dV7NB683rFiCtFvwTYTZuqlu6XBMEKI13_SLFZM3eF8kxgQ"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM