简体   繁体   English

Spring Security authenticationFailure:错误org.hibernate.HibernateException:没有找到当前线程的会话

[英]Spring Security authenticationFailure : error org.hibernate.HibernateException: No Session found for current thread

I am trying to use Spring security to do authentication,so what i done is to implements a UserDetailsService , and i used a UserDao to get user from database.so i have two file configurations applicationContext and security : the problem is when i debug , i get: 我正在尝试使用Spring安全性进行身份验证,所以我要做的是实现UserDetailsService ,并且我使用UserDao从database.user中获取用户,所以我有两个文件配置applicationContext和security:问题是当我调试时,我得到:

error org.hibernate.HibernateException: No Session found for current thread at DaoAuthenticationProvider class and the weird thing is in UserDetailsService the session is instantiated . error org.hibernate.HibernateException: No Session found for current threadDaoAuthenticationProvidererror org.hibernate.HibernateException: No Session found for current thread ,而奇怪的是在UserDetailsService中实例化了该会话。

ApplicationContext: ApplicationContext的:

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost/InTouch" />
            <property name="username" value="root" />
            <property name="password" value="" />
        </bean>

        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource" />
            </property>
            <property name="annotatedClasses">
                <list>
    ......
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.use_sql_comments">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</prop>
                    <prop key="hibernate.search.default.indexBase">/tmp/lucene_dev</prop>
                </props>
            </property>
        </bean>

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

> <aop:config>      <aop:pointcut id="transactionPointcut"
>           expression="execution(*
> ma.csimaroc.core.profil.services.interfaces..*.*(..))" />
>       <aop:advisor advice-ref="txAdvice"
> pointcut-ref="transactionPointcut" />     </aop:config>

security.xml : security.xml:

<beans:bean
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"
    id="passwordEncoder" />

    <beans:bean id="customUserDetailsService"
        class="ma.csimaroc.core.profil.services.impl.CustomUserDetailsService"
        autowire="byName" />

    <beans:bean id="authProvider"
        class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="customUserDetailsService" />
        <beans:property name="passwordEncoder" ref="passwordEncoder" />
    </beans:bean>

    <authentication-manager>
        <authentication-provider ref="authProvider" />
    </authentication-manager>

CustomUserDetailsService : CustomUserDetailsS​​ervice:

public class CustomUserDetailsService implements UserDetailsService {

UserDao userDao;

public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException {

    UserDetails user = null;

    UserBD userBean = userDao.getUserByName(username);

    System.out.println(userBean.getUsername());

    List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();

    authList.add(new SimpleGrantedAuthority(userBean.getUserRole()
            .getRole()));

    user = new User(userBean.getUsername(), userBean.getPassword()
            .toLowerCase(), true, true, true, true, authList);

    return user;
}

public UserDao getUserDao() {
    return userDao;
}

public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}

web.xml : web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml
                     /WEB-INF/security.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Declare into ApplicationContext: 声明为ApplicationContext:

<tx:annotation-driven transaction-manager="transactionManager"/>

In order to use tx declare the following xml namespace: 为了使用tx,声明以下xml名称空间:

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

This will enable @Transactional annotation. 这将启用@Transactional批注。

Then annotate with @Transactional your CustomUserDetailsService 然后使用@Transactional注释您的CustomUserDetailsService

hope this helps. 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.HibernateException:使用tx:advise找不到当前线程的会话 - org.hibernate.HibernateException: No Session found for current thread with tx:advise org.hibernate.HibernateException: 没有找到当前线程 5 的会话 - org.hibernate.HibernateException: No Session found for current thread 5 Hibernate 4:org.hibernate.HibernateException:当前会话找不到会话 - Hibernate 4 : org.hibernate.HibernateException: No Session found for current thread Spring + Hibernate应用程序中的问题:org.hibernate.HibernateException:找不到当前线程的Session - Problems in Spring + Hibernate application: org.hibernate.HibernateException: No Session found for current thread 迁移到Hibernate 4 + Spring 4.2.2:org.hibernate.HibernateException:无法获取当前线程的事务同步Session - migration to hibernate 4 + spring 4.2.2: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread 请求处理失败; 嵌套的异常是org.hibernate.HibernateException:当前会话找不到会话 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread Estado HTTP 500-请求处理失败; 嵌套的异常是org.hibernate.HibernateException:当前会话找不到会话 - Estado HTTP 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread 嵌套的异常是org.hibernate.HibernateException:当前会话addDepartment控制器找不到会话 - nested exception is org.hibernate.HibernateException: No Session found for current thread addDepartment controller 由于&#39;org.hibernate.HibernateException所需的@Transactional注释:找不到当前线程的异常&#39;异常 - @Transactional annotation required due to 'org.hibernate.HibernateException: No Session found for current thread' exception Micronaut MySQL删除记录org.hibernate.HibernateException:当前会话找不到会话 - Micronaut MySQL delete record org.hibernate.HibernateException: No Session found for current thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM