Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Security authenticationFailure:错误org.hibernate.hibernateeException:未找到当前线程的会话_Java_Multithreading_Spring_Hibernate_Spring Security - Fatal编程技术网

Java Spring Security authenticationFailure:错误org.hibernate.hibernateeException:未找到当前线程的会话

Java Spring Security authenticationFailure:错误org.hibernate.hibernateeException:未找到当前线程的会话,java,multithreading,spring,hibernate,spring-security,Java,Multithreading,Spring,Hibernate,Spring Security,我尝试使用Spring security进行身份验证,所以我所做的是实现一个UserDetailsService,并使用一个UserDao从数据库获取用户。因此,我有两个文件配置applicationContext和security:问题是当我调试时,我得到: error org.hibernate.hibernateeexception:在DaoAuthenticationProvider类中找不到当前线程的会话,奇怪的是在userdetails服务中,会话被实例化了 应用程序上下文:

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

error org.hibernate.hibernateeexception:在
DaoAuthenticationProvider
类中找不到当前线程的会话,奇怪的是在
userdetails服务中,会话被实例化了

应用程序上下文:

    <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>
<tx:annotation-driven transaction-manager="transactionManager"/>

......
真1,假0,是“Y”,否“N”
假的
真的
更新
org.hibernate.dialogue.mysqldialogue
org.hibernate.search.store.FSDirectoryProvider
/tmp/lucene_-dev
>expression=“执行(*
>ma.csimaroc.core.profil.services.interfaces.***(..)”/>
>切入点ref=“transactionPointcut”/>
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>

CustomUserDetails服务:

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;
}
公共类CustomUserDetailsService实现UserDetailsService{
UserDao UserDao;
公共用户详细信息loadUserByUsername(字符串用户名)
抛出UsernameNotFoundException{
UserDetails user=null;
UserBD userBean=userDao.getUserByName(用户名);
System.out.println(userBean.getUsername());
List authList=new ArrayList();
add(新的SimpleGrantedAuthority(userBean.getUserRole())
.getRole());
user=新用户(userBean.getUsername(),userBean.getPassword())
.toLowerCase(),true,true,true,true,authList);
返回用户;
}
公共UserDao getUserDao(){
返回userDao;
}
公共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>

上下文配置位置
/WEB-INF/applicationContext.xml
/WEB-INF/security.xml
org.springframework.web.context.ContextLoaderListener
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

在应用程序上下文中声明:

    <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>
<tx:annotation-driven transaction-manager="transactionManager"/>
这将启用
@Transactional
注释

然后用
@Transactional
注释您的
CustomUserDetailsService

希望这有帮助。

我正在使用