使用spring安全性实现UserDetailsS​​ervice时,EntityManager为null

时间:2012-07-22 14:24:29

标签: spring-security nullpointerexception

我正在尝试将Spring安全性包含在Web应用程序中。为此,我编写了自己的UserDetailsS​​ervice实现,如下所示:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("userRepositoryImpl")
public class UserRepositoryImpl implements UserDetailsService {

        @PersistenceContext
        private EntityManager entityManager;

        @Override
        @Transactional(readOnly = true)
        public User loadUserByUsername(String username)
                        throws UsernameNotFoundException {
                User user = entityManager.find(User.class, username);
                if (user == null)
                        throw new UsernameNotFoundException("Username not found: "
                                        + username);
                return user;
        }

}

我的问题是,在调用loadUserByUserName方法时,entityManager始终为null。 我在一些类似的答案中尝试了一些建议,但没有任何帮助。

这是我当前的security-app-context.xml:

<http use-expressions="true" auto-config="true">
    <intercept-url pattern="/task/" access="permitAll" />
    <intercept-url pattern="/task/**" access="isAuthenticated()" />

    <!-- <intercept-url pattern="/**" access="denyAll" /> -->
    <form-login />
</http>

<beans:bean id="userRepositoryImpl" class="de.sveri.jeiwomisa.model.UserRepositoryImpl" autowire="byType">
</beans:bean>        
<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder">
</beans:bean>
<authentication-manager>
    <authentication-provider user-service-ref="userRepositoryImpl">
        <password-encoder ref="passwordEncoder" />
    </authentication-provider>
</authentication-manager>

任何想法那里可能有什么问题?

此致 斯文

编辑:尝试了一下我发现在登录过程中我无法在我的通用DaoImpl中使用entityManager。 我可以使用它在另一个进程中发送和接收数据。这是道:

@Transactional
public abstract class DaoImpl<T> implements Dao<T> {

    private Class<T> type;

    @PersistenceContext
    protected EntityManager em;

    public DaoImpl() {
            Type t = getClass().getGenericSuperclass();
            ParameterizedType pt = (ParameterizedType) t;
            type = (Class) pt.getActualTypeArguments()[0];
    }
...
}

我想知道为什么会这样。显然我不太了解弹簧的工作原理,所以如果有人可以发光,那将会很棒。

1 个答案:

答案 0 :(得分:0)

感谢来自论坛春天团队的好人,我得到了它的工作。 我不得不更改xml定义并添加标记,以便我的security-app-context.xml看起来像这样:

<b:beans xmlns:b="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/security"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/security     http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:annotation-config/>

....

这是论坛条目的链接:http://forum.springsource.org/showthread.php?128626-EntityManager-null-when-implementing-UserDetailsService