我正在尝试使用 CDI 实现登录机制@javax.enterprise.context.SessionScoped
代码:
@Named
@SessionScoped
public class Auth implements Serializable {
private User user;
@Inject
private UserStore userStore;
@Produces @CurrentUser
public User getUser() {
if (user == null) {
Principal principal= FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
if (principal != null) {
Map parameters = new HashMap();
parameters.put("email", principal.getName());
user = (User) userStore.findWithNamedQuery(User.GET_BY_EMAIL, parameters).get(0);
}
}
return user;
}
public void logout() throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.invalidateSession();
ec.redirect(ec.getRequestContextPath() + "/");
}
}
总是接收相同User
实例的类:
@Stateful
public class NewsService implements Serializable {
@Inject @CurrentUser
private User currentUser;
@Inject
private NewsStore newsStore;
public List<News> getNewsForLoggedUser(Integer start, Integer limit) {
Map<String, Object> params = new HashMap<>();
params.put("user", currentUser);
return (List<News>) newsStore.findWithNamedQuery(News.getAllForUser, params, start, limit);
}
}
方法有问题getUser()
。即使会话无效,它仍然会从第一次登录时返回值。如何销毁 CDI bean 或更改它以使其始终输出实际值?我尝试了注释logout()
方法@PreDestroy
,但它会产生这样的错误:
13:20:50,329 ERROR [org.jboss.weld.Bean] (default task-22) WELD-000019 Error destroying an instance Managed Bean [class com.intenso.presentation.Auth] with qualifiers [@Default @Any @Named] of com.intenso.presentation.Auth@59821e
我在 WildFly 8.0.0.Alpha4 上运行