|
| 1 | +package org.baeldung.methodsecurity.service; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
| 6 | +import javax.annotation.security.RolesAllowed; |
| 7 | + |
| 8 | +import org.baeldung.methodsecurity.annotation.IsViewer; |
| 9 | +import org.baeldung.methodsecurity.entity.CustomUser; |
| 10 | +import org.baeldung.methodsecurity.repository.UserRoleRepository; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.security.access.annotation.Secured; |
| 13 | +import org.springframework.security.access.prepost.PostAuthorize; |
| 14 | +import org.springframework.security.access.prepost.PostFilter; |
| 15 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 16 | +import org.springframework.security.access.prepost.PreFilter; |
| 17 | +import org.springframework.security.core.context.SecurityContext; |
| 18 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 19 | +import org.springframework.stereotype.Service; |
| 20 | + |
| 21 | +@Service |
| 22 | +public class UserRoleService { |
| 23 | + |
| 24 | + @Autowired |
| 25 | + UserRoleRepository userRoleRepository; |
| 26 | + |
| 27 | + @Secured("ROLE_VIEWER") |
| 28 | + public String getUsername() { |
| 29 | + SecurityContext securityContext = SecurityContextHolder.getContext(); |
| 30 | + return securityContext.getAuthentication().getName(); |
| 31 | + } |
| 32 | + |
| 33 | + @Secured({ "ROLE_VIEWER", "ROLE_EDITOR" }) |
| 34 | + public boolean isValidUsername(String username) { |
| 35 | + return userRoleRepository.isValidUsername(username); |
| 36 | + } |
| 37 | + |
| 38 | + @RolesAllowed("ROLE_VIEWER") |
| 39 | + public String getUsername2() { |
| 40 | + SecurityContext securityContext = SecurityContextHolder.getContext(); |
| 41 | + return securityContext.getAuthentication().getName(); |
| 42 | + } |
| 43 | + |
| 44 | + @RolesAllowed({ "ROLE_VIEWER", "ROLE_EDITOR" }) |
| 45 | + public boolean isValidUsername2(String username) { |
| 46 | + return userRoleRepository.isValidUsername(username); |
| 47 | + } |
| 48 | + |
| 49 | + @PreAuthorize("hasRole('ROLE_VIEWER')") |
| 50 | + public String getUsernameInUpperCase() { |
| 51 | + return getUsername().toUpperCase(); |
| 52 | + } |
| 53 | + |
| 54 | + @PreAuthorize("hasAuthority('SYS_ADMIN')") |
| 55 | + public String getUsernameLC() { |
| 56 | + return getUsername().toLowerCase(); |
| 57 | + } |
| 58 | + |
| 59 | + @PreAuthorize("hasRole('ROLE_VIEWER') or hasRole('ROLE_EDITOR')") |
| 60 | + public boolean isValidUsername3(String username) { |
| 61 | + return userRoleRepository.isValidUsername(username); |
| 62 | + } |
| 63 | + |
| 64 | + @PreAuthorize("#username == authentication.principal.username") |
| 65 | + public String getMyRoles(String username) { |
| 66 | + SecurityContext securityContext = SecurityContextHolder.getContext(); |
| 67 | + return securityContext.getAuthentication().getAuthorities().stream().map(auth -> auth.getAuthority()).collect(Collectors.joining(",")); |
| 68 | + } |
| 69 | + |
| 70 | + @PostAuthorize("#username == authentication.principal.username") |
| 71 | + public String getMyRoles2(String username) { |
| 72 | + SecurityContext securityContext = SecurityContextHolder.getContext(); |
| 73 | + return securityContext.getAuthentication().getAuthorities().stream().map(auth -> auth.getAuthority()).collect(Collectors.joining(",")); |
| 74 | + } |
| 75 | + |
| 76 | + @PostAuthorize("returnObject.username == authentication.principal.nickName") |
| 77 | + public CustomUser loadUserDetail(String username) { |
| 78 | + return userRoleRepository.loadUserByUserName(username); |
| 79 | + } |
| 80 | + |
| 81 | + @PreFilter("filterObject != authentication.principal.username") |
| 82 | + public String joinUsernames(List<String> usernames) { |
| 83 | + return usernames.stream().collect(Collectors.joining(";")); |
| 84 | + } |
| 85 | + |
| 86 | + @PreFilter(value = "filterObject != authentication.principal.username", filterTarget = "usernames") |
| 87 | + public String joinUsernamesAndRoles(List<String> usernames, List<String> roles) { |
| 88 | + return usernames.stream().collect(Collectors.joining(";")) + ":" + roles.stream().collect(Collectors.joining(";")); |
| 89 | + } |
| 90 | + |
| 91 | + @PostFilter("filterObject != authentication.principal.username") |
| 92 | + public List<String> getAllUsernamesExceptCurrent() { |
| 93 | + return userRoleRepository.getAllUsernames(); |
| 94 | + } |
| 95 | + |
| 96 | + @IsViewer |
| 97 | + public String getUsername4() { |
| 98 | + SecurityContext securityContext = SecurityContextHolder.getContext(); |
| 99 | + return securityContext.getAuthentication().getName(); |
| 100 | + } |
| 101 | + |
| 102 | + @PreAuthorize("#username == authentication.principal.username") |
| 103 | + @PostAuthorize("returnObject.username == authentication.principal.nickName") |
| 104 | + public CustomUser securedLoadUserDetail(String username) { |
| 105 | + return userRoleRepository.loadUserByUserName(username); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments