JSF登录过滤器,会话为空 [英] JSF login filter, session is null
问题描述
我一直试图遵循此答案,但是我总是被重定向到我的login.xhtml(从登录页面登录时除外),因为这...
I've been trying to follow this answer primarily but I always get redirected to my login.xhtml (except for when i log in from the login page) because this...
AppManager am = (AppManager) req.getSession().getAttribute("appManager");
始终为空. 我一直在尝试在登录屏幕上打印出用户信息,无论我如何到达那里,所有字段(用户名,密码,loginIn ...)始终为空,即使我直接从管理页面键入地址(也就是登录时到达的位置). 我该如何保存会话,而不是每次我手动输入地址/离开页面时都要浪费时间?
Is always null. I've been trying to print out user info on the login screen and no matter how i get there all fields(username, password, loggedIn...) are always null, even if i type the adress straight from the admin page (that's where you get when you log in). How do I make it so that the session is saved, not whiped everytime i type in the adress manually/leave the page?
AppManager:
AppManager:
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import jobapp.controller.Controller;
@ManagedBean(name="appManager")
@SessionScoped
public class AppManager implements Serializable {
private static final long serialVersionUID = 16247164405L;
@EJB
private Controller controller;
private String username;
private String password;
private boolean loggedIn;
private Exception failure;
...
/**
*
* @param e an exception to handle.
*/
private void handleException(Exception e) {
e.printStackTrace(System.err);
failure = e;
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
}
/**
* The login method.
* calls the controllers login method.
*
*/
public void login(){
try{
failure = null;
loggedIn = controller.login(username, password);
}catch (Exception e){
handleException(e);
}
}
/**
* The logout method.
* Sets the user's info to null
* and stops the conversation.
*/
public void logout(){
username = null;
password = null;
loggedIn = false;
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
}
...
过滤器:
@WebFilter("/faces/admin.xhtml")
public class LoginFilter implements Filter {
...
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) request;
//TODO fix "am" nullpointer
AppManager am = (AppManager) req.getSession().getAttribute("appManager");
if (am != null && am.isLoggedIn()) {
// User is logged in, so just continue request.
chain.doFilter(request, response);
} else {
// User is not logged in, so redirect to login.
HttpServletResponse res = (HttpServletResponse) response;
res.sendRedirect(req.getContextPath() + "/faces/login.xhtml");
}
}
推荐答案
@SessionScoped来自javax.enterprise.context.SessionScoped
此选项仅与CDI @Named
结合使用.使用JSF时 @ManagedBean
,则应该改用javax.faces.bean
包中的范围注释.
This one works in combination with CDI @Named
only. As you're using JSF @ManagedBean
, you should be using the scope annotations from javax.faces.bean
package instead.
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class AppManager implements Serializable {
Without a valid scope, a JSF managed bean would behave like @RequestScoped
which effectively means that it's constructed again and again on every request.
这篇关于JSF登录过滤器,会话为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!