Код: Выделить всё
package com.codecraft.Crud_app.service;
import com.codecraft.Crud_app.model.Task;
import com.codecraft.Crud_app.repository.TaskRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
@RequiredArgsConstructor
public class TaskService {
private final TaskRepository taskRepository;
@Secured({"ROLE_ADMIN", "ROLE_PERSON"})
public List getAllTasks() {
return taskRepository.findAll();
}
@Secured({"ROLE_ADMIN", "ROLE_PERSON"})
public Optional getTaskById(Long id) {
return taskRepository.findById(id);
}
@Secured({"ROLE_ADMIN", "ROLE_PERSON"})
public Task saveTask(Task task) {
return taskRepository.save(task);
}
@Secured({"ROLE_ADMIN", "ROLE_PERSON"})
public void deleteTaskById(Long id) {
taskRepository.deleteById(id);
}
}
Код: Выделить всё
package com.codecraft.Crud_app;
import com.codecraft.Crud_app.controller.TaskController;
import com.codecraft.Crud_app.model.Task;
import com.codecraft.Crud_app.service.TaskService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringJUnitConfig
@SpringBootTest
@EnableMethodSecurity
class CrudAppApplicationTests11 {
@Autowired
private TaskService taskService;
@DynamicPropertySource
static void postgresqlProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", () -> "jdbc:postgresql://localhost:5432/crud-db");
registry.add("spring.datasource.username", () -> "postgres");
registry.add("spring.datasource.password", () -> "postgres");
}
@BeforeEach
public void setUp() {
Task task1 = new Task();
task1.setTitle("Task 1");
task1.setDescription("Description 1");
task1.setStatus(0);
taskService.saveTask(task1);
Task task2 = new Task();
task2.setTitle("Task 2");
task2.setDescription("Description 2");
task2.setStatus(1);
taskService.saveTask(task2);
}
@AfterEach
public void tearDown() {
for (Task task : taskService.getAllTasks()) {
taskService.deleteTaskById(task.getId());
}
}
@Test
@Order(1)
@WithMockUser(username = "admin", roles = {"ADMIN"})
public void test_getTaskById() {
ResponseEntity idTask = null;
TaskController taskController = new TaskController(taskService);
for (Task task : taskService.getAllTasks()) {
idTask = taskController.getTaskById(task.getId());
if (idTask.getBody().getId() != null) {
break;
}
}
assertNotNull(idTask.getBody().getId());
}
@Test
@Order(8)
@WithMockUser(username = "person", roles = {"PERSON"})
public void test_createTask_person() {
Task task = new Task();
String title = "Test title for creatTask";
String description = "Test description for creatTask";
int status = 654321;
task.setTitle(title);
task.setDescription(description);
task.setStatus(status);
TaskController taskController = new TaskController(taskService);
Task newTask = taskController.createTask(task);
assertEquals(title, taskController.getTaskById(newTask.getId()).getBody().getTitle());
assertEquals(description, taskController.getTaskById(newTask.getId()).getBody().getDescription());
assertEquals(status, taskController.getTaskById(newTask.getId()).getBody().getStatus());
}
@Test
@Order(9)
@WithMockUser(username = "person", password = "personpassword", roles = {"PERSON"})
public void testt() {
int i = 1;
assertNotNull(i);
}
}
Код: Выделить всё
Hibernate: select t1_0.id,t1_0.asigneed_to,t1_0.description,t1_0.status,t1_0.title from task t1_0
org.springframework.security.access.AccessDeniedException: Access is denied
at org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:77)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.attemptAuthorization(AbstractSecurityInterceptor.java:253)
Подробнее здесь: https://stackoverflow.com/questions/791 ... r-one-role
Мобильная версия