Hibernate安全性错误 - 找不到当前线程的会话

时间:2013-05-02 20:06:05

标签: spring hibernate

我正在尝试使用用户名和密码对用户进行身份验证,但在输入用户详细信息后,错误页面会出现错误:

  Your login attempt was not successful, try again.

  Reason: No Session found for current thread

的Servlet

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

     http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org

     /schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org

     /schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context     

     http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static 

    resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in 

    the /WEB-INF/views directory -->
<beans:bean 

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.myproject.app" />

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" ></tx:annotation-

   driven>


  </beans:beans>

的applicationContext

    <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation = ///// cont



<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<context:component-scan base-package="com.myproject.app.dao" />


<!-- DATASOURCE -->
<!-- <context:property-placeholder location="classpath:jdbc.properties" /> -->

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/sectest" />
<property name="username" value="root" />
<property name="password" value="llco12" />
   </bean>

<!-- HIBERNATE -->
<bean id="sessionFactory" 

    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.myproject.app.model.UserEntity</value>
            <value>com.myproject.app.model.Role</value>
        </list>
    </property>


            <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>

        </props>
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> </bean>


<!-- Beans -->

<bean id="userEntityDAO" class="com.myproject.app.dao.UserEntityDAOImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


<bean id="roleDAO" class="com.myproject.app.dao.RoleDAOImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="roleService" class="com.myproject.app.service.RoleServiceImpl">
    <property name="roleDAO" ref="roleDAO" />
</bean>

 </beans>

DAO课程

  @Repository
  @Transactional
  public class UserDAOImpl implements UserDAO{

@Autowired
private SessionFactory sessionFactory;

   @Override
@Transactional
public User getUserByName(String username) {
    // TODO Auto-generated method stub
    User user = (User) sessionFactory.getCurrentSession().createQuery(

            "select u from UserEntity u where u.username = '' + 

      username + ''").uniqueResult();

            return user;
}

服务

   @Service("customUserDetailsService")
    public class CustomUserDetailsService implements UserDetailsService{

@Autowired
private UserEntityDAO userEntityDAO;

@Autowired
private Assembler assembler;

@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException {
    // TODO Auto-generated method stub
    UserDetails userDetails = null;

    UserEntity userEntity = userEntityDAO.getUserByName(username);

    if (userEntity == null)

    throw new UsernameNotFoundException("user not found");

    return assembler.buildUserFromUser(userEntity);


  }}

我想知道为什么没有找到会话?

0 个答案:

没有答案