我已经使用Spring Security设置了JSF。
I narrowed the problem down to the createCriteria method which returns nothing.
有什么想法吗?
为什么我的userdao返回null?
我有一个MyUserDetailsService:
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;
}
}
我能够获取最终的String用户名并将其传递给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();
}
}
标准条件= 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;
}
}
我的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映射可能是错误的......