Skip to content

Instantly share code, notes, and snippets.

@jnmronquillo
Created March 15, 2013 18:33
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/5171990 to your computer and use it in GitHub Desktop.
Save jnmronquillo/5171990 to your computer and use it in GitHub Desktop.
Custom realm for static 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.Permission;
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.img.server.dao.UsuarioDao;
import com.img.server.domain.JoinedUsuarioRol;
import com.img.server.domain.Rol;
import com.img.server.domain.Usuario;
import com.img.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 UsuarioDao 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 security
**/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
Set<String> roles = 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) {
Usuario user = udao.getUser(username);
List<JoinedUsuarioRol> userRoles = udao.getRoles(user);
for(JoinedUsuarioRol userRol : userRoles){
Rol rol = userRol.getRol();
roles.add(rol.getDescripcion());
}
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
info.setRoles(roles); //fill in roles
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());
Usuario user = udao.getUser(username);
if(user == null)
throw new AuthenticationException("Authentication failed");
byte[] encryptedPassword = user.getClave();
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