Skip to content

Commit

Permalink
Merge pull request eugenp#9160 from michael-pratt/BAEL-3972
Browse files Browse the repository at this point in the history
BAEL-3972: check user roles in Java
  • Loading branch information
eric-martin authored Apr 26, 2020
2 parents e1e47ff + 38b838d commit 856a4e7
Showing 1 changed file with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
package com.baeldung.app.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baeldung.app.entity.Task;
import com.baeldung.app.service.TaskService;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("api/tasks")
public class TaskController {

@Autowired
private TaskService taskService;

@Autowired(required = false)
private UserDetailsService userDetailsService;

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Iterable<Task>> findAllTasks() {
Iterable<Task> tasks = taskService.findAll();
Expand All @@ -30,4 +42,62 @@ public ResponseEntity<Iterable<Task>> addTasks(@RequestBody Iterable<Task> newTa

return ResponseEntity.ok().body(tasks);
}

/**
* Example of restricting specific endpoints to specific roles using @PreAuthorize.
*/
@GetMapping("/manager")
@PreAuthorize("hasRole('ROLE_MANAGER')")
public ResponseEntity<Iterable<Task>> getAlManagerTasks() {
Iterable<Task> tasks = taskService.findAll();

return ResponseEntity.ok().body(tasks);
}

/**
* Example of restricting specific endpoints to specific roles using SecurityContext.
*/
@GetMapping("/actuator")
public ResponseEntity<Iterable<Task>> getAlActuatorTasks() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ACTUATOR")))
{
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}

Iterable<Task> tasks = taskService.findAll();

return ResponseEntity.ok().body(tasks);
}

/**
* Example of restricting specific endpoints to specific roles using UserDetailsService.
*/
@GetMapping("/admin")
public ResponseEntity<Iterable<Task>> getAlAdminTasks() {
if(userDetailsService != null) {
UserDetails details = userDetailsService.loadUserByUsername("pam");
if (details != null && details.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ADMIN"))) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
}

Iterable<Task> tasks = taskService.findAll();

return ResponseEntity.ok().body(tasks);
}

/**
* Example of restricting specific endpoints to specific roles using HttpServletRequest.
*/
@GetMapping("/admin2")
public ResponseEntity<Iterable<Task>> getAlAdminTasksUsingServlet(HttpServletRequest request) {
if (!request.isUserInRole("ROLE_ADMIN")) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}

Iterable<Task> tasks = taskService.findAll();

return ResponseEntity.ok().body(tasks);
}
}

0 comments on commit 856a4e7

Please sign in to comment.