Skip to content

Instantly share code, notes, and snippets.

@jnmronquillo
Created March 15, 2013 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnmronquillo/5171969 to your computer and use it in GitHub Desktop.
Save jnmronquillo/5171969 to your computer and use it in GitHub Desktop.
Custom Realm for dinamic roles
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import com.google.inject.Inject;
import com.rhem.server.dao.PersonalUserDao;
import com.rhem.server.domain.PersonalUser;
import com.rhem.server.domain.Role;
import com.rhem.server.domain.RoleUser;
import com.rhem.server.util.PasswordEncryptionService;
//http://shiro-user.582556.n2.nabble.com/How-to-add-a-role-to-the-subject-td5562700.html
public class MyRealm extends AuthorizingRealm{
@Inject PersonalUserDao udao;
@Inject PasswordEncryptionService pes;
/**
this function loads user authorization data from "userManager" data source (database)
User, Role are custom POJOs (beans) and are loaded from database.
WildcardPermission implements shiros Permission interface, so my permissions in database gets accepted by shiro sigmac
**/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
Set<String> roles = new HashSet<String>();
Set<String> permissions = new HashSet<String>();
Collection<String> principalsList = principals.byType(String.class);
if (principalsList.isEmpty()) {
throw new AuthorizationException("Empty principals list!");
}
//LOADING STUFF FOR PRINCIPAL
for (String username : principalsList) {
PersonalUser user = udao.getPersonalUser(username);
List<RoleUser> userRoles = udao.getRoles(user);
for(RoleUser userRol : userRoles){
Role rol = userRol.getRole();
roles.add(rol.getName());
List<String> userPermissions = udao.getPermissions(rol);
for(String permission : userPermissions){
if (!permissions.contains(permission)) {
permissions.add(permission);
}
}
}
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
info.setRoles(roles); //fill in roles
info.setStringPermissions(permissions);
return info;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
if (upToken.getUsername() == null || upToken.getUsername().isEmpty()) {
throw new AuthenticationException("Authentication failed");
}
String username = upToken.getUsername();
String password = String.valueOf(upToken.getPassword());
PersonalUser user = udao.getPersonalUser(username);
if(user == null)
throw new AuthenticationException("Authentication failed");
byte[] encryptedPassword = user.getPassword();
byte[] salt = user.getSalt();
try {
if(!pes.authenticate(password, encryptedPassword, salt)){
throw new AuthenticationException("Authentication failed");
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return new SimpleAuthenticationInfo(username, password, getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment