Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // this is embedded in blog post https://phip1611.de/?p=9898
  2. /*
  3.    Copyright 2018 Philipp Schuster
  4.  
  5.    Web:      phip1611.de
  6.    E-Mail:   [email protected]
  7.    Twitter:  @phip1611
  8.  */
  9. package de.phips_photoblog.controller;
  10.  
  11. import de.phips_photoblog.controller.dto.UserDto;
  12. import de.phips_photoblog.service.api.TokenService;
  13. import de.phips_photoblog.service.api.UserService;
  14. import org.springframework.security.core.Authentication;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18.  
  19. import java.util.UUID;
  20.  
  21. /**
  22.  * Rest-Controller used for creating an login and verifying the user token. Also giving the
  23.  * current active user dto to the client.
  24.  */
  25. @RestController
  26. @RequestMapping("api/auth")
  27. public class AuthController {
  28.  
  29.     public AuthController() {
  30.     }
  31.  
  32.     /**
  33.      * This endpoint is used by clients to do a login. They send a basic http auth
  34.      * request to this service and spring security makes the magic. (see config).
  35.      * If this method got actually invoked it means that spring security authenticated
  36.      * the user. It returns the secret id (token) that the client needs to use for
  37.      * all further requests.
  38.      * @param authentication Authenticated User
  39.      * @return token
  40.      */
  41.     @GetMapping(path = "/login")
  42.     public UUID getToken(Authentication authentication) {
  43.         return (UUID)authentication.getCredentials();
  44.     }
  45.  
  46.     /**
  47.      * Endpoint for testing the token. If it works, it returns the current active user
  48.      * to the client. Only get's invoked if spring security verifies the request
  49.      * (or if something went horribly wrong with spring security config)
  50.      *
  51.      * It returns the current authenticated principal (userdto)
  52.      *
  53.      * @return Principal (UserDto)
  54.      */
  55.     @GetMapping(path = "/token")
  56.     public UserDto getUserDtoOfAuthenticatedUser(Authentication authentication) {
  57.         return (UserDto)authentication.getPrincipal();
  58.     }
  59. }