1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.realm; |
20 | |
|
21 | |
import org.apache.shiro.authc.*; |
22 | |
import org.apache.shiro.authz.AuthorizationInfo; |
23 | |
import org.apache.shiro.authz.SimpleRole; |
24 | |
import org.apache.shiro.subject.PrincipalCollection; |
25 | |
import org.apache.shiro.util.CollectionUtils; |
26 | |
|
27 | |
import java.util.HashSet; |
28 | |
import java.util.LinkedHashMap; |
29 | |
import java.util.Map; |
30 | |
import java.util.Set; |
31 | |
import java.util.concurrent.locks.Lock; |
32 | |
import java.util.concurrent.locks.ReadWriteLock; |
33 | |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class SimpleAccountRealm extends AuthorizingRealm { |
47 | |
|
48 | |
|
49 | |
protected final Map<String, SimpleAccount> users; |
50 | |
protected final Map<String, SimpleRole> roles; |
51 | |
protected final ReadWriteLock USERS_LOCK; |
52 | |
protected final ReadWriteLock ROLES_LOCK; |
53 | |
|
54 | 32 | public SimpleAccountRealm() { |
55 | 32 | this.users = new LinkedHashMap<String, SimpleAccount>(); |
56 | 32 | this.roles = new LinkedHashMap<String, SimpleRole>(); |
57 | 32 | USERS_LOCK = new ReentrantReadWriteLock(); |
58 | 32 | ROLES_LOCK = new ReentrantReadWriteLock(); |
59 | |
|
60 | |
|
61 | 32 | setCachingEnabled(false); |
62 | 32 | } |
63 | |
|
64 | |
public SimpleAccountRealm(String name) { |
65 | 0 | this(); |
66 | 0 | setName(name); |
67 | 0 | } |
68 | |
|
69 | |
protected SimpleAccount getUser(String username) { |
70 | 49 | USERS_LOCK.readLock().lock(); |
71 | |
try { |
72 | 49 | return this.users.get(username); |
73 | |
} finally { |
74 | 49 | USERS_LOCK.readLock().unlock(); |
75 | |
} |
76 | |
} |
77 | |
|
78 | |
public boolean accountExists(String username) { |
79 | 4 | return getUser(username) != null; |
80 | |
} |
81 | |
|
82 | |
public void addAccount(String username, String password) { |
83 | 0 | addAccount(username, password, (String[]) null); |
84 | 0 | } |
85 | |
|
86 | |
public void addAccount(String username, String password, String... roles) { |
87 | 0 | Set<String> roleNames = CollectionUtils.asSet(roles); |
88 | 0 | SimpleAccount account = new SimpleAccount(username, password, getName(), roleNames, null); |
89 | 0 | add(account); |
90 | 0 | } |
91 | |
|
92 | |
protected String getUsername(SimpleAccount account) { |
93 | 37 | return getUsername(account.getPrincipals()); |
94 | |
} |
95 | |
|
96 | |
protected String getUsername(PrincipalCollection principals) { |
97 | 64 | return getAvailablePrincipal(principals).toString(); |
98 | |
} |
99 | |
|
100 | |
protected void add(SimpleAccount account) { |
101 | 37 | String username = getUsername(account); |
102 | 37 | USERS_LOCK.writeLock().lock(); |
103 | |
try { |
104 | 37 | this.users.put(username, account); |
105 | |
} finally { |
106 | 37 | USERS_LOCK.writeLock().unlock(); |
107 | 37 | } |
108 | 37 | } |
109 | |
|
110 | |
protected SimpleRole getRole(String rolename) { |
111 | 60 | ROLES_LOCK.readLock().lock(); |
112 | |
try { |
113 | 60 | return roles.get(rolename); |
114 | |
} finally { |
115 | 60 | ROLES_LOCK.readLock().unlock(); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
public boolean roleExists(String name) { |
120 | 5 | return getRole(name) != null; |
121 | |
} |
122 | |
|
123 | |
public void addRole(String name) { |
124 | 0 | add(new SimpleRole(name)); |
125 | 0 | } |
126 | |
|
127 | |
protected void add(SimpleRole role) { |
128 | 17 | ROLES_LOCK.writeLock().lock(); |
129 | |
try { |
130 | 17 | roles.put(role.getName(), role); |
131 | |
} finally { |
132 | 17 | ROLES_LOCK.writeLock().unlock(); |
133 | 17 | } |
134 | 17 | } |
135 | |
|
136 | |
protected static Set<String> toSet(String delimited, String delimiter) { |
137 | 0 | if (delimited == null || delimited.trim().equals("")) { |
138 | 0 | return null; |
139 | |
} |
140 | |
|
141 | 0 | Set<String> values = new HashSet<String>(); |
142 | 0 | String[] rolenamesArray = delimited.split(delimiter); |
143 | 0 | for (String s : rolenamesArray) { |
144 | 0 | String trimmed = s.trim(); |
145 | 0 | if (trimmed.length() > 0) { |
146 | 0 | values.add(trimmed); |
147 | |
} |
148 | |
} |
149 | |
|
150 | 0 | return values; |
151 | |
} |
152 | |
|
153 | |
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { |
154 | 8 | UsernamePasswordToken upToken = (UsernamePasswordToken) token; |
155 | 8 | SimpleAccount account = getUser(upToken.getUsername()); |
156 | |
|
157 | 8 | if (account != null) { |
158 | |
|
159 | 8 | if (account.isLocked()) { |
160 | 0 | throw new LockedAccountException("Account [" + account + "] is locked."); |
161 | |
} |
162 | 8 | if (account.isCredentialsExpired()) { |
163 | 0 | String msg = "The credentials for account [" + account + "] are expired"; |
164 | 0 | throw new ExpiredCredentialsException(msg); |
165 | |
} |
166 | |
|
167 | |
} |
168 | |
|
169 | 8 | return account; |
170 | |
} |
171 | |
|
172 | |
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { |
173 | 27 | String username = getUsername(principals); |
174 | 27 | USERS_LOCK.readLock().lock(); |
175 | |
try { |
176 | 27 | return this.users.get(username); |
177 | |
} finally { |
178 | 27 | USERS_LOCK.readLock().unlock(); |
179 | |
} |
180 | |
} |
181 | |
} |