2014-07-21 110 views
0

我有Picketlink IDM JPA身份驗證模型的自定義實現。我通過應對https://github.com/jboss-developer/jboss-picketlink-quickstarts/tree/master/picketlink-idm-custom-identity-model的示例來解決它,以包org.picketlink.idm.jpa.model.custom.simple。然後我實現了這個例子https://github.com/pedroigor/picketlink-quickstarts/tree/master/picketlink-deltaspike-authorization。 我改變了用戶級和UserTypeEntity在picketlink-IDM-定製身份的典範如下:自定義IDM身份驗證和授權人

@IdentityStereotype(USER) 
public class User extends AbstractIdentityType implements Account { 
    public static final QueryParameter USER_NAME = QUERY_ATTRIBUTE.byName("userName"); 
    @StereotypeProperty(IDENTITY_USER_NAME) 
@AttributeProperty 
@Unique 
private String userName; 
    @AttributeProperty 
private String firstName; 
@AttributeProperty 
private String lastName; 
@AttributeProperty 
private String email; 
@AttributeProperty 
private String middleName; 
@AttributeProperty 
private String telephone; 
@AttributeProperty 
private String address; 
@AttributeProperty 
private int postIndex; 
@AttributeProperty 
private Date registerDate; 
@AttributeProperty 
private Date lastVisitDate; 
@AttributeProperty 
private boolean isOrganizer; 
@AttributeProperty 
private boolean isAdmin; 
@Embedded 
private Organizer organizer; 
@Embedded 
private Customer customer; 
// getters and setters 
} 

類UserTypeEntity

@IdentityManaged(User.class) 
@Entity 
public class UserTypeEntity extends AbstractIdentityTypeEntity { 
@AttributeValue 
private String userName; 
@OwnerReference 
@ManyToOne(fetch = FetchType.LAZY) 
private RealmTypeEntity realm; 
@AttributeProperty 
private String firstName; 
@AttributeProperty 
private String lastName; 
@AttributeProperty 
private String email; 
@AttributeProperty 
@Column(length = 255) 
private String middleName; 
@AttributeProperty 
@Size(max = 12) 
@Column(length = 12) 
private String telephone; 
@AttributeProperty 
@Column(length = 5000) 
@Size(max = 5000) 
private String address; 
@AttributeProperty 
private int postIndex; 
@AttributeProperty 
private Date registerDate; 
@AttributeProperty 
private Date lastVisitDate; 
@AttributeProperty 
private boolean isOrganizer; 
@AttributeProperty 
private boolean isAdmin; 
@Embedded 
private Organizer organizer; 
@Embedded 
private Customer customer; 
// getters and setters 
} 

然後我實現登錄控制器:

@Named 
@Stateless 
public class LoginController { 
@Inject 
private Identity identity; 
@Inject 
private FacesContext facesContext; 
public void login() { 
AuthenticationResult result = identity.login(); 
    if (AuthenticationResult.FAILED.equals(result)) { 
facesContext.addMessage(
null, 
new FacesMessage("Invalid user name or password")); 
    } 
    } 
} 

和註冊控制器:

@Named 
@RequestScoped 
public class RegistrationController { 
    private IdentityManager identityManager; 
    @Inject 
    private PartitionManager partitionManager; 
    @Inject 
    private FacesContext facesContext; 
    @Inject 
    private User user; 
    private String password; 
    private String passwordVerify; 
    private boolean isOrganizer; 
    public RegistrationController() { 
    } 
    public User getUser() { 
    return user; 
    } 
    public void setUser(User user) { 
    this.user = user; 
    } 
    public String getPasswordVerify() { 
    return passwordVerify; 
    } 
    public void setPasswordVerify(String passwordVerify) { 
    this.passwordVerify = passwordVerify; 
    } 
    public boolean getIsOrganizer() { 
    return isOrganizer; 
    } 
    public void setIsOrganizer(boolean isOrganizer) { 
    this.isOrganizer = isOrganizer; 
    } 
    public String getPassword() { 
    return password; 
    } 
    public void setPassword(String password) { 
    this.password = password; 
    } 
    @Transactional 
    public String register() throws Exception { 
    if (password.isEmpty()) { 
     String message = LocaleBean.loadErrorMessage(facesContext, LocaleBean.EX_RESOURCE_BUNDLE_NAME, "password.empty"); 
     facesContext.addMessage("signup:registrationPassword", new FacesMessage(message)); 
     return "returnToSignup"; 
    } 
    if (!password.equals(passwordVerify)) { 
     String message = LocaleBean.loadErrorMessage(facesContext, LocaleBean.EX_RESOURCE_BUNDLE_NAME, "password.NotEqual"); 
     facesContext.addMessage("signup:registrationPassword", new FacesMessage(message)); 
     return "returnToSignup"; 
    } 
     identityManager = partitionManager.createIdentityManager(partitionManager.getPartition(Realm.class, 
      Resources.REALM_ACME_NAME)); 
     if (isOrganizer) { 
     user.setOrganizer(true); 
     user.setOrganizer(new Organizer()); 
     try { 
      identityManager.add(user); 
     } catch (IdentityManagementException e) { 
      String message = LocaleBean.loadErrorMessage(facesContext, LocaleBean.EX_RESOURCE_BUNDLE_NAME, "login.Registered"); 
      facesContext.addMessage(null, new FacesMessage(message)); 
      return "returnToSignup"; 
     } 
     Password password = new Password(this.password); 
     identityManager.updateCredential(user, password); 
     RelationshipManager relationshipManager = partitionManager.createRelationshipManager(); 
     IdentityQuery<Group> query = identityManager.createIdentityQuery(Group.class); 
     // query all childs of sales unit 
     query.setParameter(Group.NAME, Resources.ORGANIZERS_GROUP_NAME); 
     List<Group> groups = query.getResultList(); 
     Group organizersGroup = groups.get(0); 
     relationshipManager.add(new GroupMembership(user, organizersGroup)); 
     } else { 
     } 
    return "signin"; 
    } 
} 

在那之後,我實現授權人:

@ApplicationScoped 
public class SPAuthorizer { 
@Secures 
@Admins 
public boolean doAdminsCheck(Identity identity, IdentityManager identityManager, RelationshipManager relationshipManager) throws Exception { 
return hasGroup(identity, identityManager, relationshipManager, Resources.ADMINS_GROUP_NAME); 
    } 
@Secures 
@Organizers 
public boolean doOrganizersCheck(Identity identity, IdentityManager identityManager, RelationshipManager relationshipManager) throws Exception { 
return hasGroup(identity, identityManager, relationshipManager, Resources.ORGANIZERS_GROUP_NAME); 
    } 
@Secures 
@Customers 
public boolean doCustomersCheck(Identity identity, IdentityManager identityManager, RelationshipManager relationshipManager) throws Exception { 
return hasGroup(identity, identityManager, relationshipManager, Resources.CUSTOMERS_GROUP_NAME); 
    } 
private boolean hasGroup(Identity identity, IdentityManager identityManager, RelationshipManager relationshipManager, 
String groupName) { 
IdentityQuery<Group> queryGroup = identityManager.createIdentityQuery(Group.class); 
    // query all childs of sales unit 
queryGroup.setParameter(Group.NAME, groupName); 
List<Group> groups = queryGroup.getResultList(); 
    if (groups.size() == 1) { 
Group group = groups.get(0); 
Account user = identity.getAccount(); 
if (user == null) { 
return false; 
    } 
RelationshipQuery<GroupMembership> query = relationshipManager.createRelationshipQuery(GroupMembership.class); 
query.setParameter(GroupMembership.GROUP, group); 
query.setParameter(GroupMembership.MEMBER, user); 
// user is assigned with two groups 
List<GroupMembership> resultList = query.getResultList(); 
return resultList.size() > 0; 
    } 
return false; 
    } 
} 

所以我實現授權檢查器檢查屬於用戶的一些組JSF:

@Named 
@Stateless 
public class AuthorizationChecker { 
@Inject 
private Identity identity; 
@Inject 
private PartitionManager partitionManager; 
public boolean hasGroup(String groupName) { 
IdentityManager identityManager = partitionManager.createIdentityManager(partitionManager.getPartition(Realm.class, 
Resources.REALM_ACME_NAME)); 
IdentityQuery<Group> queryGroup = identityManager.createIdentityQuery(Group.class); 
    // query all childs of sales unit 
queryGroup.setParameter(Group.NAME, groupName); 
List<Group> groups = queryGroup.getResultList(); 
    if (groups.size() == 1) { 
Group group = groups.get(0); 
Account user = identity.getAccount(); 
RelationshipManager relationshipManager = partitionManager.createRelationshipManager(); 
RelationshipQuery<GroupMembership> query = relationshipManager.createRelationshipQuery(GroupMembership.class); 
query.setParameter(GroupMembership.GROUP, group); 
query.setParameter(GroupMembership.MEMBER, user); 
// user is assigned with two groups 
List<GroupMembership> resultList = query.getResultList(); 
return resultList.size() > 0; 
    } 
return false; 
    } 
} 

但hasGroup身份的實例SPAuthorizer的沒有按對應於我在AuthorizationChecker中的實例。我在調試器中檢查過它。當我做identity.getAccount();它雖然用戶被認證,但返回null。任何想法該怎麼辦?

回答