休眠+春季安全 [英] Hibernate + Spring Security

查看:132
本文介绍了休眠+春季安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的春天3 + Spring Security的3 +冬眠。
我有一些问题映射类。我不知道为什么,但一些类映射他们可以通过Hibernate的,但同时一些(这是用于春季安全)都没有用!
论坛-security.xml文件:

I am using spring 3 + spring security 3 + hibernate. I have some problems with mapping classes. I don't know why, but some classes are mapped they can be used by Hibernate but at the same time some (which are used for Spring Security) are not! forum-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security" 
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http auto-config='true'>
    <intercept-url pattern="/**" access="ROLE_USER" />
</http>
<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService"/>
</authentication-manager>
</beans:beans>

UserServiceImpl:

UserServiceImpl:

package forum.service;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import forum.domain.ForumUser;

@Service
public class UserServiceImpl implements UserService{
    @Autowired private SessionFactory sessionFactory;
@Autowired private Assembler assembler;
public List<ForumUser> listAllUsers(){
    return null;
}
public List<ForumUser> listUsersBySellingPont(){
    return null;
}
@Transactional
public ForumUser getUserByUsername(String username){
    Session session = sessionFactory.getCurrentSession();
    List<ForumUser> users = session.createQuery("from ForumUser").list();
    ForumUser result = null;
    for(ForumUser user : users){
        if(user.getUsername().equals(username))
            result = user;
    }
    return result;
}
public void addUser(ForumUser user){

}
public void updateUser(ForumUser user){

}
public void deleteUser(Integer id){

}
}

汇编程序:

package forum.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import forum.domain.ForumUser;
import forum.domain.UserDetailsImpl;

@Service("assembler")
public class Assembler {
@Transactional(readOnly = true)
  UserDetailsImpl buildUserFromUserEntity(ForumUser userEntity) {
    Integer id = userEntity.getId();
    String username = userEntity.getUsername();
    String password = userEntity.getPassword();
    String email = userEntity.getEmail();
    Date enabled = userEntity.getEnabled();
    Date lastEntered = userEntity.getLastEntered();
    Date registered = userEntity.getRegistered();

    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    for (GrantedAuthority role : userEntity.getAuthorities()) {
      authorities.add(role);
    }
    UserDetailsImpl user = new UserDetailsImpl();
    user.setId(id);
    user.setUsername(username);
    user.setPassword(password);
    user.setEmail(email);
    user.setEnabled(enabled);
    user.setAuthorities(authorities);
    user.setLastEntered(lastEntered);
    user.setRegistered(registered);
    return user;
  }
}

而现在不受休眠映射类(其他类映射):

And now classes that are not mapped by hibernate (other classes are mapped):

package forum.domain;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.FetchType;
import javax.persistence.CascadeType;

import org.springframework.security.core.GrantedAuthority;

@Entity
@Table(name="users")
public class ForumUser {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="email")
private String email;
@Column(name="registered")
private Date registered;
@Column(name="lastEntered")
private Date lastEntered;
@Column(name="enabled")
private Date enabled;
@OneToMany(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY,mappedBy="forumUser")
private List<GrantedAuthority> authorities;
public Date getEnabled(){
    return enabled;
}
public void setEnabled(Date enabled){
    this.enabled = enabled;
}
public Integer getId(){
    return id;
}
public void setId(Integer id){
    this.id = id;
}
public String getUsername(){
    return username;
}
public void setUsername(String username){
    this.username = username;
}
public String getPassword(){
    return password;
}
public void setPassword(String password){
    this.password = password;
}
public String getEmail(){
    return email;
}
public void setEmail(String email){
    this.email = email;
}
public Date getRegistered(){
    return registered;
}
public void setRegistered(Date registered){
    this.registered = registered;
}
public Date getLastEntered(){
    return lastEntered;
}
public void setLastEntered(Date lastEntered){
    this.lastEntered = lastEntered;
}
public List<GrantedAuthority> getAuthorities() {
    return authorities;
}
public void setAuthorities(List<GrantedAuthority> authorities){
    this.authorities = authorities;
}
}

和第二类:

package forum.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
import javax.persistence.Table;

import org.springframework.security.core.GrantedAuthority;

@Entity
@Table(name="authorities")
public class Authority implements GrantedAuthority{
@ManyToOne(cascade=CascadeType.REFRESH,fetch=FetchType.LAZY)
@JoinColumn(name="userId")
private ForumUser forumUser;
@Column(name="authority")
private String authority;
public String getAuthority() {
    return authority;
}
public void setAuthority(String authority) {
    this.authority = authority;
}
}

所以,当我试图重新找回用户名从(从ForumUser UserServiceImpl.getUserByUsername())DB的用户,它抛出
org.hibernate.hql.ast.QuerySyntaxException:ForumUser没有映射[从ForumUser]

So when I am trying to retrieve an user by username from DB (UserServiceImpl.getUserByUsername() from ForumUser), it throws org.hibernate.hql.ast.QuerySyntaxException: ForumUser is not mapped [from ForumUser]

如果我从论坛改变锡尔HQL到另一个,例如(这是另一个类,即工作),它会抛出另一个异常,并没有真正的问题究竟是什么,但事实是,它mappes另一类,并可以找回。
我怎样才能解决这个问题?

And if I change thir HQL to another, for example "from Forum" (it is another class, that is working) it will throw another exception, doesn't really matter what exactly, but the fact is that it mappes another class and can retrieve it. How can I solve this problem?

推荐答案

呵呵,我已经找到了解决方案!这一切,是因为我自己的疏忽。我已经忘记了新的班级在我的conf添加注释的持久化类的列表。

Oh, I have found a solution! It all was because of my own inattention. I have forgotten to add new classes to a list of annotated persistent classes in my conf.

这篇关于休眠+春季安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆