|
| 1 | +package com.baeldung.comparison.shiro; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import org.apache.shiro.authc.AuthenticationException; |
| 13 | +import org.apache.shiro.authc.AuthenticationInfo; |
| 14 | +import org.apache.shiro.authc.AuthenticationToken; |
| 15 | +import org.apache.shiro.authc.SimpleAuthenticationInfo; |
| 16 | +import org.apache.shiro.authc.UnknownAccountException; |
| 17 | +import org.apache.shiro.authc.UsernamePasswordToken; |
| 18 | +import org.apache.shiro.authz.AuthorizationInfo; |
| 19 | +import org.apache.shiro.authz.SimpleAuthorizationInfo; |
| 20 | +import org.apache.shiro.realm.jdbc.JdbcRealm; |
| 21 | +import org.apache.shiro.subject.PrincipalCollection; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +public class CustomRealm extends JdbcRealm { |
| 26 | + |
| 27 | + private Logger logger = LoggerFactory.getLogger(CustomRealm.class); |
| 28 | + |
| 29 | + private Map<String, String> credentials = new HashMap<>(); |
| 30 | + private Map<String, Set<String>> roles = new HashMap<>(); |
| 31 | + private Map<String, Set<String>> permissions = new HashMap<>(); |
| 32 | + |
| 33 | + { |
| 34 | + credentials.put("Tom", "password"); |
| 35 | + credentials.put("Jerry", "password"); |
| 36 | + |
| 37 | + roles.put("Jerry", new HashSet<>(Arrays.asList("ADMIN"))); |
| 38 | + roles.put("Tom", new HashSet<>(Arrays.asList("USER"))); |
| 39 | + |
| 40 | + permissions.put("ADMIN", new HashSet<>(Arrays.asList("READ", "WRITE"))); |
| 41 | + permissions.put("USER", new HashSet<>(Arrays.asList("READ"))); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { |
| 46 | + |
| 47 | + UsernamePasswordToken userToken = (UsernamePasswordToken) token; |
| 48 | + |
| 49 | + if (userToken.getUsername() == null || userToken.getUsername() |
| 50 | + .isEmpty() || !credentials.containsKey(userToken.getUsername())) { |
| 51 | + throw new UnknownAccountException("User doesn't exist"); |
| 52 | + } |
| 53 | + |
| 54 | + return new SimpleAuthenticationInfo(userToken.getUsername(), credentials.get(userToken.getUsername()), getName()); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { |
| 59 | + Set<String> roles = new HashSet<>(); |
| 60 | + Set<String> permissions = new HashSet<>(); |
| 61 | + |
| 62 | + for (Object user : principals) { |
| 63 | + try { |
| 64 | + roles.addAll(getRoleNamesForUser(null, (String) user)); |
| 65 | + permissions.addAll(getPermissions(null, null, roles)); |
| 66 | + } catch (SQLException e) { |
| 67 | + logger.error(e.getMessage()); |
| 68 | + } |
| 69 | + } |
| 70 | + SimpleAuthorizationInfo authInfo = new SimpleAuthorizationInfo(roles); |
| 71 | + authInfo.setStringPermissions(permissions); |
| 72 | + return authInfo; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected Set<String> getRoleNamesForUser(Connection conn, String username) throws SQLException { |
| 77 | + if (!roles.containsKey(username)) { |
| 78 | + throw new SQLException("User doesn't exist"); |
| 79 | + } |
| 80 | + return roles.get(username); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected Set<String> getPermissions(Connection conn, String username, Collection<String> roles) throws SQLException { |
| 85 | + Set<String> userPermissions = new HashSet<>(); |
| 86 | + |
| 87 | + for (String role : roles) { |
| 88 | + if (!permissions.containsKey(role)) { |
| 89 | + throw new SQLException("Role doesn't exist"); |
| 90 | + } |
| 91 | + userPermissions.addAll(permissions.get(role)); |
| 92 | + } |
| 93 | + return userPermissions; |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments