【发布时间】:2013-05-12 21:25:00
【问题描述】:
我在我的应用程序的 JPA/EJB 3/JSF 2/Spring Security 3 中使用 glassfish 3.1.2。 我想写一个像这样的自定义 UserdetailsService:
import org.apache.log4j.Logger;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
public class MyUserDetailsService implements UserDetailsService {
private static final Logger logger = Logger.getLogger(MyUserDetailsService.class);
@EJB
private CollaborateurFacadeLocal collaborateurFacade;
@Override
public UserDetails loadUserByUsername(String userName) {
Collaborateur collab = getUser(userName);
if (collab == null) {
throw new UsernameNotFoundException(userName + " not found");
}
User user = new User(collab);
if (user == null) {
throw new UsernameNotFoundException(userName + " not found");
}
return user;
}
}
所以我试图在 MyUserDetailsService 中注入一个 EJB,以便能够在我的 Spring Security 上下文文件中将其用作身份验证提供程序:“applicationContext-security.xml”,如下所示:
<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsService">
</authentication-provider>
</authentication-manager>
问题是我陷入了由 null 协作者外观引起的 NullPointerException。
我尝试了几件事,其中:
-
使用上下文查找 EJB(例如位于 here 的那个):解决方案有效,但这不是我想要的:因为 EJB 的名称可能会被修改。
将 MyUserDetailsService 类作为 JSF 2 托管 bean,collectorurFacade 一直保持为空。
将 MyUserDetailsService 类作为 JSF 2 托管 bean 并具有 ApplicationScoped 范围,collectionurFacade 将一直保持到 null。
问题: 有什么干净的方法可以在 MyUserDetailsService 类中注入 EJB 吗?
我知道我可以使用@Service 注释来注释我的类(参见this link),然后我将陷入我不想要的 EJB 和 Spring 的混合中
【问题讨论】:
标签: java jakarta-ee jsf-2 spring-security ejb-3.1