0

我正在尝试在 Web 应用程序中包含 Spring Security。为此,我编写了自己的 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;
        }

}

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

这是我当前的 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];
    }
...
}

我想知道为什么会这样。显然,我不太了解弹簧的工作原理,所以如果有人能阐明一下,那就太好了。

4

1 回答 1

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-UserDetailsS​​ervice

于 2012-07-24T07:30:49.793 回答