简体   繁体   中英

how to call spring oauth2 resource server uri from servlet

We use spring boot oauth2 configuration in client application for validating user along with access token provided with below yaml config:

security:
  oauth2:
    resource:
      userInfoUri: http://IP:PORT/auth/user

I am trying to build a servlet based web application. How do I achieve same user validation using servlet by calling above spring oauth2 uri?

If you want write your own the security layer in your servlet application, you can create a Filter class that will read the token from the Authorization request header, make an HTTP request to the token introspection endpoint to validate the token and then it can call the user info endpoint (the userInfoUri URL from your Spring Boot config) and get information about the user making the processed request.

To make the HTTP requests (token introspection and user info), you can use any HTTP client library (easier) or the standard Java library (lower level). I would suggest the Apache HTTP client library. The JSON response results can be parsed by libraries such as FasterXml Jackson - ObjectMapper class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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