1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools;
12
13 import jakarta.servlet.ServletContext;
14
15 import java.util.Collection;
16
17 import org.springframework.security.core.GrantedAuthority;
18 import org.springframework.security.core.context.SecurityContextHolder;
19
20
21
22
23 public final class SecurityUtils {
24
25
26
27
28 private SecurityUtils() {
29
30 }
31
32
33
34
35
36
37
38
39 public static boolean hasAttributeValueRole(ServletContext servletContext) {
40
41 String[] privilegedRoles = getPrivilegedRoles(servletContext).split(",", -1);
42 for (String privilegedRole : privilegedRoles) {
43 if (userHasRole(privilegedRole)) {
44 return true;
45 }
46 }
47 return false;
48 }
49
50
51
52
53
54
55
56
57 private static boolean userHasRole(String privilegedRole) {
58 Collection<? extends GrantedAuthority> authorities =
59 SecurityContextHolder.getContext().getAuthentication().getAuthorities();
60
61 boolean result = false;
62 for (GrantedAuthority authority : authorities) {
63 if (privilegedRole.equals(authority.getAuthority())) {
64 result = true;
65 break;
66 }
67 }
68 return result;
69 }
70
71
72
73
74
75
76
77
78 private static String getPrivilegedRoles(ServletContext servletContext) {
79 return servletContext.getInitParameter("attribute.value.roles");
80 }
81
82 }