keycloak 获取 access token validate token

import requests as req
import json

reqJson = {
    "client_id":"jwt-service",
    "client_secret": "qaaaaa-468a-4ba6-b71a-21672d1376be",
    "username": "aaa@aaa.com",
    "password": "123123",
    "grant_type": "password"
}

tokenUrl = 'http://localhost:8080/auth/realms/dev/protocol/openid-connect/token'

header = {
    "Content-Type": "application/x-www-form-urlencoded",
} 

response = req.post(tokenUrl, data=reqJson, headers=header)
if response.status_code > 200:
    print (response.text)
else:
    accessToken = json.loads(response.text)['access_token']
    print(accessToken)

 

validate token

header = {
    "Authorization": "Bearer " + access_token
} 
userinfoUrl = 'https://localhost:8080/auth/realms/customer/protocol/openid-connect/userinfo'

response = req.get(userinfoUrl, headers=header)    
print(response.text)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I don't have access to any specific token. However, I can provide you with the basic steps to implement token-based authentication in a Spring Boot application using OpenAPI: 1. Add the necessary dependencies to your pom.xml file: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-api</artifactId> <version>0.11.2</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-impl</artifactId> <version>0.11.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt-jackson</artifactId> <version>0.11.2</version> <scope>runtime</scope> </dependency> ``` 2. Create a User model class with username and password fields. 3. Create a JwtUtil class to generate and validate JWT tokens: ``` public class JwtUtil { private final String secret; public JwtUtil(String secret) { this.secret = secret; } public String generateToken(User user) { Date now = new Date(); Date expiryDate = new Date(now.getTime() + 3600000); return Jwts.builder() .setSubject(user.getUsername()) .setIssuedAt(now) .setExpiration(expiryDate) .signWith(SignatureAlgorithm.HS512, secret) .compact(); } public String getUsernameFromToken(String token) { return Jwts.parser() .setSigningKey(secret) .parseClaimsJws(token) .getBody() .getSubject(); } public boolean validateToken(String token, User user) { String username = getUsernameFromToken(token); return username.equals(user.getUsername()); } } ``` 4. Create a LoginController to handle user login requests: ``` @RestController @RequestMapping("/login") public class LoginController { private final JwtUtil jwtUtil; public LoginController(JwtUtil jwtUtil) { this.jwtUtil = jwtUtil; } @PostMapping public String login(@RequestBody User user) { // Your authentication logic here if (user.getUsername().equals("admin") && user.getPassword().equals("password")) { return jwtUtil.generateToken(user); } else { throw new BadCredentialsException("Invalid username or password"); } } } ``` 5. Add the necessary security configurations to your application.properties file: ``` spring.security.oauth2.resourceserver.jwt.issuer-uri= spring.security.oauth2.resourceserver.jwt.jwk-set-uri= spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://example.com/.well-known/jwks.json ``` 6. Add the necessary Swagger annotations to your API endpoints: ``` @Operation(summary = "Get all users") @ApiResponse(responseCode = "200", description = "Successfully returned all users") @GetMapping("/users") public List<User> getAllUsers() { // Your logic here } @Operation(summary = "Get a user by ID") @ApiResponse(responseCode = "200", description = "Successfully returned the user") @ApiResponse(responseCode = "404", description = "User not found") @GetMapping("/users/{id}") public User getUserById(@PathVariable Long id) { // Your logic here } // And so on... ``` 7. Test your API endpoints using a tool like Postman, passing the JWT token in the Authorization header: ``` Authorization: Bearer <JWT token> ``` That's it! You should now have a Spring Boot application with token-based authentication using OpenAPI.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值