Spring Security Hibernate + Annotations用户身份验证 - SQL Server异常:无效列

时间:2014-12-30 11:28:32

标签: java spring hibernate spring-mvc spring-security

我正在尝试使用Spring Security + Hibernate + Annotations创建用户身份验证演示。以下是我使用的文件

User.java [Model]

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

private String email;
private String password;
private String lastLog;
private String archive;
private Set<Role> userRoles = new HashSet<Role>(0);    

@Id
@Column(name = "Email", unique = true, nullable = false, length = 50)
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

@Column(name = "Password", nullable = false, length = 50)
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

@Column(name = "Last_Log", nullable = true, length = 50)
public String getLastLog() {
    return lastLog;
}
public void setLastLog(String lastLog) {
    this.lastLog = lastLog;
}

@Column(name = "Archive", nullable = true, length = 1)
public boolean getArchive() {
    if(archive.equals("x"))
    {
        System.out.println(false);
        return false;
    }
    else
    {
        System.out.println(true);
        return true;
    }
}
public void setArchive(String archive) {
    this.archive = archive;
}

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)  
@JoinTable(name = "Link_Role_User", 
    joinColumns = { @JoinColumn(name = "email") }, 
    inverseJoinColumns = { @JoinColumn(name = "rId") })  
public Set<Role> getUserRoles() {
    return userRoles;
}
public void setUserRoles(Set<Role> userRoles) {
    this.userRoles = userRoles;
}
}

Role.java

@Entity  
@Table(name = "Roles")  
public class Role {

private String rId;
private String roleName;
private Set<User> user = new HashSet<User>();  

@Id
@Column(name = "RId", unique = true, nullable = false)
public String getrId() {
    return rId;
}
public void setrId(String rId) {
    this.rId = rId;
}

@Column(name = "RoleName", nullable = false, length = 50)
public String getRoleName() {
    return roleName;
}
public void setRoleName(String roleName) {
    this.roleName = roleName;
}

@ManyToMany(mappedBy = "userRoles")  
public Set<User> getUser() {
    return user;
}
public void setUser(Set<User> user) {
    this.user = user;
}
}

UserDaoImpl [DAO - 实现UsreDao接口的类]

@Repository("userDao")
public class UserDaoImpl implements UserDao {

@Autowired
private SessionFactory sessionFactory;

Session session = null;  
Transaction tx = null;  

@SuppressWarnings("unchecked")
@Override
public User getUserDetails(String email) throws Exception
{
    System.out.println(email);
    session = sessionFactory.openSession();
    tx = session.beginTransaction(); 
    Query q = session.createQuery("FROM User WHERE email=:e");

    q.setParameter("e", email);
    List<User> users = q.list();

    System.out.println("Size: " + users.size());
    tx.commit();  
    session.close();
    if (users.size() > 0) {
        return users.get(0);
    } else {
        return null;
    }
}  

@SuppressWarnings("unchecked")
@Override
public List<User> getUserList() throws Exception{
    session = sessionFactory.openSession();  
    tx = session.beginTransaction();  
    List<User> toDoList = session.createQuery("from User").list();

    tx.commit();  
    session.close();  
    return toDoList;
}
}

MyUserDetailsS​​ervice(自定义身份验证提供程序)

@Transactional(readOnly=true)
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

@Autowired
private UserDao userDao;

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

    System.out.println("MyUserDetailsService: "+ email);

    com.automagic.admin.model.User user = new com.automagic.admin.model.User();
    try {
        user = userDao.getUserDetails(email);
    }
    catch(Exception e){
        e.printStackTrace();
    }

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

    return buildUserForAuthentication(user, authorities);

}

private User buildUserForAuthentication(com.automagic.admin.model.User user, 
    List<GrantedAuthority> authorities) {

    System.out.println("MyUserDetailsService 2 : "+ user.getEmail());


    return new User(user.getEmail(), user.getPassword(), 
        user.getArchive(), true, true, true, authorities);
}

private List<GrantedAuthority> buildUserAuthority(Set<Role> userRoles) {

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

    for (Role userRole : userRoles) {
        setAuths.add(new SimpleGrantedAuthority(userRole.getrId()));
    }

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

    return Result;
}
}

安全context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/security
                    http://www.springframework.org/schema/security/spring-security.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context.xsd">

<security:authentication-manager>
    <security:authentication-provider  user-service-ref='userDetailsService'>
            <security:password-encoder hash="md5"   />                  
    </security:authentication-provider> 
</security:authentication-manager>

<security:http auto-config="true"  use-expressions="true">
    <security:access-denied-handler error-page="/403" />

    <security:intercept-url pattern="/usermanagement" access="isAuthenticated()">

    <security:intercept-url pattern="/logout" access="permitAll"></security:intercept-url>
    <security:intercept-url pattern="/**" access="permitAll"></security:intercept-url>

    <security:intercept-url pattern="/resources/**" access="permitAll"></security:intercept-url>
    <security:intercept-url pattern="/login" access="permitAll"></security:intercept-url>

    <security:form-login login-page="/login"
        authentication-failure-url="/login?error=true"
        username-parameter="email" password-parameter="password"
        always-use-default-target="true" default-target-url="/usermangement" />

    <security:logout logout-success-url="/logout" delete-cookies="JSESSIONID" />
</security:http>
</beans:beans>

我收到以下错误

org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'user_Email'.

但是我已经彻底检查了我的代码,我没有在代码中的任何地方使用“user_Email”。请告知出了什么问题?

0 个答案:

没有答案