|
| 1 | +package com.baeldung; |
| 2 | + |
| 3 | +import org.apache.shiro.authc.*; |
| 4 | +import org.apache.shiro.authz.AuthorizationInfo; |
| 5 | +import org.apache.shiro.authz.SimpleAuthorizationInfo; |
| 6 | +import org.apache.shiro.realm.jdbc.JdbcRealm; |
| 7 | +import org.apache.shiro.subject.PrincipalCollection; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.sql.Connection; |
| 12 | +import java.sql.SQLException; |
| 13 | +import java.util.*; |
| 14 | + |
| 15 | +public class MyCustomRealm extends JdbcRealm { |
| 16 | + |
| 17 | + private Map<String, String> credentials = new HashMap<>(); |
| 18 | + private Map<String, Set<String>> roles = new HashMap<>(); |
| 19 | + private Map<String, Set<String>> perm = new HashMap<>(); |
| 20 | + |
| 21 | + { |
| 22 | + credentials.put("user", "password"); |
| 23 | + credentials.put("user2", "password2"); |
| 24 | + credentials.put("user3", "password3"); |
| 25 | + |
| 26 | + roles.put("user", new HashSet<>(Arrays.asList("admin"))); |
| 27 | + roles.put("user2", new HashSet<>(Arrays.asList("editor"))); |
| 28 | + roles.put("user3", new HashSet<>(Arrays.asList("author"))); |
| 29 | + |
| 30 | + perm.put("admin", new HashSet<>(Arrays.asList("*"))); |
| 31 | + perm.put("editor", new HashSet<>(Arrays.asList("articles:*"))); |
| 32 | + perm.put("author", |
| 33 | + new HashSet<>(Arrays.asList("articles:compose", |
| 34 | + "articles:save"))); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) |
| 40 | + throws AuthenticationException { |
| 41 | + |
| 42 | + UsernamePasswordToken uToken = (UsernamePasswordToken) token; |
| 43 | + |
| 44 | + if(uToken.getUsername() == null |
| 45 | + || uToken.getUsername().isEmpty() |
| 46 | + || !credentials.containsKey(uToken.getUsername()) |
| 47 | + ) { |
| 48 | + throw new UnknownAccountException("username not found!"); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + return new SimpleAuthenticationInfo( |
| 53 | + uToken.getUsername(), credentials.get(uToken.getUsername()), |
| 54 | + getName()); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { |
| 59 | + Set<String> roleNames = new HashSet<>(); |
| 60 | + Set<String> permissions = new HashSet<>(); |
| 61 | + |
| 62 | + principals.forEach(p -> { |
| 63 | + try { |
| 64 | + Set<String> roles = getRoleNamesForUser(null, (String) p); |
| 65 | + roleNames.addAll(roles); |
| 66 | + permissions.addAll(getPermissions(null, null,roles)); |
| 67 | + } catch (SQLException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames); |
| 73 | + info.setStringPermissions(permissions); |
| 74 | + return info; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected Set<String> getRoleNamesForUser(Connection conn, String username) throws SQLException { |
| 79 | + if(!roles.containsKey(username)) { |
| 80 | + throw new SQLException("username not found!"); |
| 81 | + } |
| 82 | + |
| 83 | + return roles.get(username); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected Set<String> getPermissions(Connection conn, String username, Collection<String> roleNames) throws SQLException { |
| 88 | + for (String role : roleNames) { |
| 89 | + if (!perm.containsKey(role)) { |
| 90 | + throw new SQLException("role not found!"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + Set<String> finalSet = new HashSet<>(); |
| 95 | + for (String role : roleNames) { |
| 96 | + finalSet.addAll(perm.get(role)); |
| 97 | + } |
| 98 | + |
| 99 | + return finalSet; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments