|
@@ -4,12 +4,9 @@ import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
-import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
-import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
@@ -18,49 +15,36 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import es.uv.garcosda.models.AuthenticatedUser;
|
|
|
|
|
|
@RestController
|
|
|
-@PreAuthorize("isAuthenticated()")
|
|
|
-@RequestMapping(value="/api/v1/users", produces=MediaType.APPLICATION_JSON_VALUE)
|
|
|
+@RequestMapping("/api/v1/users")
|
|
|
public class UserRestController {
|
|
|
|
|
|
@GetMapping("authenticated")
|
|
|
- public ResponseEntity<AuthenticatedUser> getAuthenticatedUser() {
|
|
|
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
- if(authentication!= null) {
|
|
|
- Object userDetails = authentication.getPrincipal();
|
|
|
- if(userDetails != null && userDetails instanceof UserDetails)
|
|
|
- {
|
|
|
- UserDetails secUser = (UserDetails) userDetails;
|
|
|
- String username = secUser.getUsername();
|
|
|
-
|
|
|
- List<String> roles = secUser.getAuthorities()
|
|
|
- .stream()
|
|
|
- .map(authority -> authority.getAuthority())
|
|
|
- .collect(Collectors.toList());
|
|
|
- AuthenticatedUser authenticatedUser = new AuthenticatedUser(username, roles);
|
|
|
- return new ResponseEntity<>(authenticatedUser,HttpStatus.OK);
|
|
|
- }
|
|
|
+ public ResponseEntity<AuthenticatedUser> getAuthenticatedUser(Authentication auth) {
|
|
|
+ if(auth.isAuthenticated()) {
|
|
|
+ String username = auth.getName();
|
|
|
+ List<String> roles = auth.getAuthorities()
|
|
|
+ .stream()
|
|
|
+ .map(authority -> authority.getAuthority())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ AuthenticatedUser authenticatedUser = new AuthenticatedUser(username, roles);
|
|
|
+ return new ResponseEntity<>(authenticatedUser,HttpStatus.OK);
|
|
|
}
|
|
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- @GetMapping("roles/{username}")
|
|
|
- @PreAuthorize("#username == authentication.principal.username")
|
|
|
- public ResponseEntity<AuthenticatedUser> getMyRoles(@PathVariable String username) {
|
|
|
- Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
- if(authentication!= null){
|
|
|
- Object userDetails = authentication.getPrincipal();
|
|
|
- if(userDetails != null && userDetails instanceof UserDetails){
|
|
|
- UserDetails secUser = (UserDetails) userDetails;
|
|
|
-
|
|
|
- List<String> roles = secUser.getAuthorities()
|
|
|
- .stream()
|
|
|
- .map(authority -> authority.getAuthority())
|
|
|
- .collect(Collectors.toList());
|
|
|
- AuthenticatedUser authenticatedUser = new AuthenticatedUser(username, roles);
|
|
|
- return new ResponseEntity<>(authenticatedUser,HttpStatus.OK);
|
|
|
- }
|
|
|
+ @GetMapping("roles/{username_}")
|
|
|
+ @PreAuthorize("#username_ == authentication.principal.username")
|
|
|
+ public ResponseEntity<AuthenticatedUser> getMyRoles(@PathVariable String username_, Authentication auth) {
|
|
|
+ if(auth.isAuthenticated()) {
|
|
|
+ String username = auth.getName();
|
|
|
+ List<String> roles = auth.getAuthorities()
|
|
|
+ .stream()
|
|
|
+ .map(authority -> authority.getAuthority())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ AuthenticatedUser authenticatedUser = new AuthenticatedUser(username, roles);
|
|
|
+ return new ResponseEntity<>(authenticatedUser,HttpStatus.OK);
|
|
|
}
|
|
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
|
|
}
|