2015-04-29 57 views
0

我已经用Spring Security设置了JSF。Spring Security:认证userdao为空

I narrowed the problem down to the createCriteria method which returns nothing. 

任何想法?

为什么我的userdao返回null?

我有一个MyUserDetailsS​​ervice:

public class MyUserDetailsService implements UserDetailsService, Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 1864276199817921495L; 
private UserDao userDao; 

@Override 
public UserDetails loadUserByUsername(final String username) 
      throws UsernameNotFoundException { 

    bla.bla.bla.model.User user = userDao.getUser(username); 

    List<GrantedAuthority> authorities = buildUserAuthority(user.getPermissions()); 

    return buildUserForAuthentication(user, authorities); 


} 

private User buildUserForAuthentication(bla.bla.bla.model.User user, 
    List<GrantedAuthority> authorities) { 
    return new User(user.getUsername(), 
     user.getPassword(), user.getEnabled(), 
        true, true, true, authorities); 
} 

private List<GrantedAuthority> buildUserAuthority(Set<Permission> Permissions) { 

    Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>(); 

    // Build user's authorities 
    for (Permission permission : Permissions) { 
     setAuths.add(new SimpleGrantedAuthority(permission.getPermission())); 
    } 

    List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths); 

    return Result; 
} 

/* 
* Get the UserDao 
*/ 
public UserDao getUserDao() { 
    return userDao; 
} 

/* 
* Set the userDao 
*/ 
public void setUserDao(UserDao userDao) { 
    this.userDao = userDao; 
} 

} 

我能够得到最终的字符串的用户名和它传递给userDao.getUser(用户名),但我的方法为用户对象,这样,当我访问返回null方法我收到以下错误:

[ERROR]  org.springframework.security.web.authentication.UsernamePasswordAuthenticationFi lter:226 - An internal error occurred while trying to authenticate the user. 
org.springframework.security.authentication.InternalAuthenticationServiceException 

这是由的getUser(用户名)呼吁:

public GenericDaoImpl(final Class<T> type) { 
    super(); 
    this.type = type; 
} 

@SuppressWarnings("unchecked") 
public T getBySingleValue(String field, Object value){ 

    Session session = sessionFactory.getCurrentSession(); 

    try{ 
     if(!session.getTransaction().isActive()) 
      session.beginTransaction(); 

     //Create a new Criteria instance, for the given entity class 
     //Executes a query against a particular persistent class 
     Criteria criteria = session.createCriteria(type); 
     criteria.add(Restrictions.eq(field, value)); 
     T object = (T)criteria.uniqueResult(); 

     return object; 
    } catch (Exception e){ 
     System.out.println("ERROR"); 
     e.printStackTrace(); 
     return null; 
    } finally { 
     if(session.getTransaction().isActive()) 
      session.getTransaction().commit(); 
    }  
} 

Criteria criteria = session.createCriteria(type);不应该返回null。没有打印出hibernate sql语句。

我的JPA映射在这里:

@Entity 
@Table(name="users") 
public class User { 

private Integer id; 
private String username; 
private String firstname; 
private String lastname; 
private String password; 
private Set<Permission> permissions = new HashSet<Permission>(0); 

/** 
* Default Constructor 
*/ 
public User(){ 
} 

public User(String username, String firstname, 
     String lastname,String password){ 
    this.username = username; 
    this.firstname = firstname; 
    this.lastname = lastname; 
    this.password = password; 
} 

public User(String username, String firstname, 
     String lastname,String password, 
     Set<Permission> permissions) { 
    this.username = username; 
    this.firstname = firstname; 
    this.lastname = lastname; 
    this.password = password; 
    this.permissions = permissions; 
} 

/**Get the id 
* @return the id 
*/ 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name="id", unique = true, nullable = false) 
public Integer getId() { 
    return this.id; 
} 

/**Set the id 
* @param id the id to set 
*/ 
public void setId(Integer id) { 
    this.id = id; 
} 

/**Get the username 
* @return the username 
*/ 
@Column(name="username", unique=true, nullable = false, length=25) 
public String getUsername() { 
    return this.username; 
} 

/**Set the username 
* @param username the username to set 
*/ 
public void setUsername(String username) { 
    this.username = username; 
} 

/** 
* @return the firstname 
*/ 
@Column(name="firstname", nullable = false, length=20) 
public String getFirstName() { 
    return this.firstname; 
} 

/** 
* @param firstname the firstname to set 
*/ 
public void setFirstName(String firstname) { 
    this.firstname = firstname; 
} 

/** 
* @return the lastname 
*/ 
@Column(name="lastname", nullable = false, length=20) 
public String getLastName() { 
    return this.lastname; 
} 

/** 
* @param lastname the lastname to set 
*/ 
public void setLastName(String lastname) { 
    this.lastname = lastname; 
} 

/** 
* @return the password 
*/ 
@Column(name="password", nullable = false, length=60) 
public String getPassword() { 
    return this.password; 
} 

/** 
* @param password the password to set 
*/ 
public void setPassword(String password) { 
    this.password = password; 
} 

/** 
* @return the userPermissions 
*/ 
@ManyToMany(fetch=FetchType.LAZY) 
@JoinTable(name="user_permission", 
     joinColumns = @JoinColumn(name="user_id", nullable=false), 
     inverseJoinColumns = @JoinColumn(name="permission_id",nullable=false)) 
public Set<Permission> getPermissions() { 
    return this.permissions; 
} 

/** 
* @param userPermissions the userPermissions to set 
*/ 
public void setPermissions(Set<Permission> userPermissions) { 
    this.permissions = userPermissions; 
} 
} 

@Entity 
@Table(name="permission") 
public class Permission { 

private Integer id; 
private String permission; 
private String description; 
private Set<User> users = new HashSet<User>(0); 

/** 
* Default constructor 
*/ 
public Permission(){ 

} 

public Permission(String permission, String description, 
     Set<User> users) { 
    this.permission = permission; 
    this.description = description; 
    this.users = users; 
} 

/** 
* @return the id 
*/ 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name="id", unique = true, nullable = false) 
public Integer getId() { 
    return this.id; 
} 
/** 
* @param id the id to set 
*/ 
public void setId(Integer id) { 
    this.id = id; 
} 
/** 
* @return the permission 
*/ 
@Column(name = "permission", unique = true, nullable = false, length = 50) 
public String getPermission() { 
    return this.permission; 
} 
/** 
* @param permission the permission to set 
*/ 
public void setPermission(String permission) { 
    this.permission = permission; 
} 
/** 
* @return the description 
*/ 
@Column(name = "description", length = 150) 
public String getDescription() { 
    return this.description; 
} 
/** 
* @param description the description to set 
*/ 
public void setDescription(String description) { 
    this.description = description; 
} 
/** 
* @return the users 
*/ 
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "permissions") 
public Set<User> getUsers() { 
    return this.users; 
} 
/** 
* @param users the users to set 
*/ 
public void setUsers(Set<User> users) { 
    this.users = users; 
} 




} 

MY userDAO的和MyUserServiceDetail在XML文件中定义:

<bean id="userDao" class="bla.bla.bla.dao.UserDaoImpl"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<bean id="myUserDetailsService" 
       class="bla.bla.bla.service.MyUserDetailsService"> 
    <property name="userDao" ref="userDao" /> 
</bean> 

我觉得我的JPA映射可能是错的...

+0

哪里设置了userdao?如果应该自动填写,则忘记了注释。 – Jens

+0

我在我的XML中定义它们,Spring会注入它们,因为我有setter/getter – wwjdm

+0

这里输入了什么样的对象?Criteria criteria = session.createCriteria(type);'? – Jens

回答

0

提花它出:

我添加到休眠会话配置:

<property name="annotatedClasses"> 
    <list> 
     <value>bla.blah.bla.User</value> 
     <value>bla.blah.bla.model.UserOrganization</value> 
     <value>bla.blah.bla.model.Permission</value> 
    </list> 
</property>