Search code examples
javaspringspring-bootspring-oauth2

How to create webapp, REST API and OAuth2 at the same server?


I'm still learning spring and currently I'm trying to create webapplication and RESTful API at the same server. It was easy until I tried to add OAuth2 authorizaton to be able to consume that API in Android application. I have a problem because I don't completely understand what is happening under the hood of spring.

I want achieve something like this:

  1. localhost:8080**/** - webapp authorized in normal way
  2. localhost:8080**/login** - webapp login page
  3. localhost:8080**/api/** -RESTfull API
  4. localhost:8080**/oauth/[authorize|token]** - OAuth2
  5. Android application to consume API

Is it even possible? Can you provide an example code or just the way I should do it?


Solution

  • Well, Spring Boot has plenty of documentation.

    I'd recommend the Spring Boot manual

    As for what you want, yes, it's possible. See this demo which has OAuth, REST API and normal HTML, CSS, JS files all in one. Also, this demo is based on spring-boot, so what more do you want :)

    Again, the demo url: https://github.com/szerhusenBC/jwt-spring-security-demo/

    As I said above, it's possible. Not recommended, but possible. It would be recommended to split them, for a better separation of concerns. Also for performance reasons: Apache for instance can serve static files much faster than SpringBoot/Tomcat can. Of course you can start this way and do it later. Enjoy.