【问题标题】:Spring Security authenticationFailure : error org.hibernate.HibernateException: No Session found for current threadSpring Security authenticationFailure:错误org.hibernate.HibernateException:没有为当前线程找到会话
【发布时间】:2014-05-16 03:39:41
【问题描述】:

我正在尝试使用 Spring 安全性进行身份验证,所以我所做的是实现UserDetailsService,并且我使用了 UserDao 从数据库中获取用户。所以我有两个文件配置 applicationContext 和 security:问题是当我调试时,我得到:

error org.hibernate.HibernateException: No Session found for current threadDaoAuthenticationProvider 类中,奇怪的是在UserDetailsService 会话被实例化。

应用程序上下文:

    <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:

<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>

自定义用户详细信息服务:

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:

<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>

【问题讨论】:

    标签: java multithreading spring hibernate spring-security


    【解决方案1】:

    声明到ApplicationContext中:

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

    为了使用 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
    

    这将启用@Transactional 注释。

    然后用@Transactional注释你的CustomUserDetailsService

    希望这会有所帮助。

    【讨论】:

    • 我正在使用
    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2014-01-10
    • 2013-06-21
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多