我有一個簡單的JEE應用程序,在登錄時會中斷。在我將它從氖轉移到火星之前它工作正常。雖然我改變服務器運行時對WildFly 10.0,但仍說,我的服務器運行時10.x的未綁定(我改變了運行時間都爲Web和EJB項目無論如何,這是我的方法:HttpServletRequest.GetSession返回null
public String login(){
UserDto user = usersBean.validateUser(username, password);
if(null == user){
MessageUtils.addErrorMessage("login.error.invalid.credentials");
return "";
} else{
request.getSession().setAttribute("LOGGED_USER", user);
******the upper code fails********
return SUCCESS_LOGIN_REDIRECT;
}
}
堆棧跟蹤:
Context Path:/JEEProjectWeb
Servlet Path:/page/login.html
Path Info:null
Query String:null
Stack Trace
javax.servlet.ServletException: java.lang.NullPointerException
javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
bg.JEE.web.filter.AuthenticationFilter.doFilter(AuthenticationFilter.java:60)
io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
我在這個非常新的,不知道什麼可能導致這個錯誤,但我一直試圖把request.getSession()中的變量和調試它返回null,所以應該有。 setAttribute函數沒有問題
請幫助我!預先安排!
附加信息:
身份驗證篩選:
package bg.JEE.web.filter;
import java.io.IOException;
import java.io.Serializable;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import bg.JEE.dto.UserDto;
public class AuthenticationFilter implements Filter, Serializable {
private static final long serialVersionUID = 1L;
public static final String PATH_INDEX = "/index.jsp";
public static final String PATH_LOGIN = "/page/login.html";
public static final String PATH_LOGOUT = "/page/logout.jsp";
public void init(FilterConfig filterConfig) throws ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestedPath = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
/*
* Skip the action with path "index.jsp" - there is no logged user, but
* the user is redirected to login screen
*/
if (PATH_INDEX.equals(requestedPath)) {
chain.doFilter(request, response); --row 60
return;
}
/*
* Skip the action with path "/login.login.html" - there is no logged
* user, but the user is trying to log in
*/
if (PATH_LOGIN.equals(requestedPath)) {
chain.doFilter(request, response);
return;
}
/*
* Get logged user from the HttpSession
*/
HttpSession session = httpRequest.getSession();
UserDto loggedUser = (UserDto) session.getAttribute("LOGGED_USER");
/*
* Redirect to login page if there is no logged user and trying to
* access protected resource
*/
if (loggedUser == null) {
RequestDispatcher requestDispatcher = request.getRequestDispatcher(PATH_LOGIN);
requestDispatcher.forward(request, response);
return;
} else {
chain.doFilter(request, response);
return;
}
}
}
而這裏的一排60碼距離LoginBean:
public void setUsername(String username) {
this.username = username;
}
好像你在你的代碼中有一個自定義的Servlet過濾器('bg.JEE.web.filter.AuthenticationFilter')。那個類是包含'public String login()'方法的類嗎?如果是這樣,那麼第60行是什麼? –
我會在上面發佈更多的代碼,但是沒有,Login()方法包含在名爲LoginBean的類中。 bg.JEE.web.filter.AuthenticationFilter包含一個檢查用戶是否有一個會話,據我所知,它正在工作,因爲每次我嘗試訪問另一個頁面而沒有登錄時,我被重定向到登錄頁面。主要問題再次附上代碼。謝謝! – GeorgiG
就在!我發現,@Inject導致了這個問題。出於某種原因,當我做 「@注入\t私人HttpServletRequest的請求」 爲null,但是當我做「HttpServletRequest的請求=(HttpServletRequest的)FacesContext的 \t .getCurrentInstance() \t .getExternalContext() \t \t .getRequest() ;」有用。有人知道爲什麼嗎?從我的位置看起來像容器的一些問題... – GeorgiG