No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
User userRole = (User) user.getRoles();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Set<String> roles = authentication.getAuthorities().stream()
.map(r -> r.getAuthority()).collect(Collectors.toSet());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
boolean hasUserRole = authentication.getAuthorities().stream()
.anyMatch(r -> r.getAuthority().equals("ROLE_USER"));
@GetMapping ("/customMethod")
public String someMethod(
@AuthenticationPrincipal UserDetails currentUser
) {
if(
SecurityContextHolder.getContext().getAuthentication() != null &&
SecurityContextHolder.getContext().getAuthentication().isAuthenticated() &&
!(SecurityContextHolder.getContext().getAuthentication() instanceof AnonymousAuthenticationToken)
) {
User user = (User) userService.findUserByEmail(currentUser.getUsername()); // Полушаем нашего авторизованного пользователя
// тут получаем роль пользователя, а дальше через switch case или if ... else редиректим на нужный урл
switch(userRole){
case "role1":
return "page1";
case "role2":
return "/page2";
}
}
}
827ccb0eea8a706c4c34a16891f84e7b
А мне бы найти такое чудо решение, которое разрешит только одному кокнретному юзеру редактировать только свою конкретную запись без учёта группы.