- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 JSF 和 servlet 过滤器创建登录应用程序,但是当用户登录时没有任何反应。它再次重定向到登录页面。
这是项目目录:
这是 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>javaeetutorial.guessnumber.filters.LoggedInFilter</filter-class>
</filter>
<!-- Set the login filter to secure all the pages in the /secured/* path of the application -->
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/secured/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>greeting.xhtml</welcome-file>
</welcome-file-list>
</web-app>
这是 LoggedInFilter.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaeetutorial.guessnumber.filters;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javaeetutorial.guessnumber.TestBean;
/**
*
* @author salih
*/
public class LoggedInFilter implements Filter {
FilterConfig fc;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
fc = filterConfig;
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Enumeration<String> t = ((HttpServletRequest) request).getSession().getAttributeNames();
while(t.hasMoreElements()) { System.out.println(t.nextElement().toString());}
TestBean loginBean = (TestBean) ((HttpServletRequest) request).getSession().getAttribute("testBean");
// For the first application request there is no loginBean in the session so user needs to log in
// For other requests loginBean is present but we need to check if user has logged in successfully
if (loginBean == null || !loginBean.isLoggedIn()) {
String contextPath = ((HttpServletRequest) request).getContextPath();
((HttpServletResponse) response).sendRedirect(contextPath + "/login.xhtml");
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
这是 TestBean.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaeetutorial.guessnumber;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.inject.Named;
/**
*
* @author salih
*/
@Named
@SessionScoped
public class TestBean implements Serializable {
private boolean loggedIn;
private String username;
private String password;
private NavigationBean navigationBean = new NavigationBean();
private static final String[] users = {"anna:qazwsx", "kate:123456"};
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isLoggedIn() {
return loggedIn;
}
public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
}
public String doLogin() {
// Get every user from our sample database :)
for (String user : users) {
String dbUsername = user.split(":")[0];
String dbPassword = user.split(":")[1];
// Successful login
if (dbUsername.equals(username) && dbPassword.equals(password)) {
loggedIn = true;
return navigationBean.redirectToWelcome();
}
}
// Set login ERROR
FacesMessage msg = new FacesMessage("Login error!", "ERROR MSG");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, msg);
// To to login page
return navigationBean.toLogin();
}
}
这是登录.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"htth://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Login form</title>
</h:head>
<h:body>
<h3>Login here</h3>
<h:form id="login-form">
<h:outputText value="username:"/>
<h:inputText value="#{testBean.username}" id="username"/>
<br/>
<h:outputText value="password:"/>
<h:inputSecret value="#{testBean.password}" id="password"/>
<br/>
<h:commandButton id="button" value="Login" action="#{testBean.doLogin}"/>
<br/>
<h:commandLink action="#{navigationBean.redirectToInfo}" value="Info page"/>
<br/>
<h:messages />
<br/>
</h:form>
</h:body>
</html>
最佳答案
您正在将 JSF 托管 Bean/概念与 CDI 托管 Bean 混合在一起。
首先,您忽略了指定容器,但我将为普通 servlet 容器编写一个答案,并为支持 CDI 的 Java 应用程序服务器(例如 TomEE/Wildfly/Glassfish)编写一个答案。
使用@Named
和@SessionScoped
但请确保您拥有正确的@SessionScoped。 javax.enterprise.context 是包名称。如果您使用 JSF 中的 SessionScoped,该 bean 将具有依赖作用域,这是错误的。
现在删除过滤器并使用测试页进行快速测试。使用 <h:inputText />
设置字段和<h:commandButton>
(都在 <h:form>
中)。确保 F5 之后该值仍然存在,从而成功 @SessionScoped.
如果它不起作用,请确保您使用的是 CDI 1.1 或者您有根据文档提供的 beans.xml。
最后使用
@Inject
private TestBean testBean;
或者,如果您有像 Tomcat 或 Jetty 这样的普通容器,您需要使用 Deltaspike Core,然后:TestBean testBean = BeanProvider.getContextualReference(TestBean.class, false);
祝你好运
关于java - 尝试在 JSF 中使用 Servlet Filter 登录时出现无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21137561/
我有一个关于 Java Servlet 的问题。 假设我在 servlet 网页“somePage”上。我想登录(使用另一个 servlet,“登录”servlet)。所以我点击“somePage”上
如何将变量数组从一个 servlet 传递到另一个 servlet? 最佳答案 如果您要将当前请求传递给另一个servlet,则只需将其设置为请求属性即可。 request.setAttribute(
什么可能导致此错误? Caused by: jakarta.servlet.UnavailableException: Servlet class org.restlet.ext.servle
我的maven依赖树是这样的 我想问我maven如何解决这个冲突,有两个servlet-api.jar?提前谢谢你。 最佳答案 如果您想从 Velocity 工具中删除 servlet-api,您可以
config ProcessReg ProcessReg text HelloWorld1 public class config implements Serv
您好,我有一个关于 servlet 调用另一个 servlet 的问题 我有一个名为 Relay 的主 servlet,它将负责控制其他 servlet 用户将点击并将转发到 Relay servle
在我的 REST API 项目中,我已将 /* 映射到 RESTServlet,并且需要在同一 WAR 中托管静态内容。我更愿意将 /static/* 映射到 WAS liberty 提供的默认 se
响应映射在 Servlet 中如何工作? 每个响应如何知道清除特定 HTML 或 Handlebars 上的输出? 最佳答案 有一个 ember-java带有 Jersey REST 服务 的 git
有一个 @WebServlet(urlPatterns = "/myServlet/") .如果用户转到 myapp/myServlet/other ,我仍然希望我的 servlet 能够捕获。也就是
我正在使用 Filter 在我的所有页面中插入反点击劫持 header - 这工作正常,除了 JBoss EAP 6.3 容器管理的登录页面,这是更重要的页面之一拥有它。 登录页面根本不调用过滤器,登
我正在尝试使用 RequestDispatcher 将数据从一个 servlet 传递到另一个 servlet。这是我的调度程序代码。 String address; address = "/Java
我刚刚开始使用 Servlet,并设法让一些 Servlet 充当单独的 URL,用于填充数据库以进行一些虚拟测试。某种形式: public class Populate_ServletName ex
我是否需要同时配置app.servlet.version 和 grails.servlet.version? 前者在application.properties中,后者在BuildConfig.gro
在Myeclipse中我创建了一个名为web1的Web项目,并添加了一个名为servlet1的servlet,web.xml如下: servlet1 servlet1
这个问题在这里已经有了答案: How to run a background task in a servlet based web application? (5 个回答) 6年前关闭。 是否可以在
在我的 ManagedBean 中,如果我将范围从 @RequestScoped 更改为 @ViewScoped,我将收到以下错误堆栈。我该如何解决这个问题?不过,当我运行应用程序时,我可以在页面中看
这个问题已经有答案了: How do I execute multiple servlets in sequence? (2 个回答) 已关闭 3 年前。 我已经构建了jdbc-Servlet的代码,
我一直在徒劳地尝试实现 tomcat 9 的 jakarta servlet,而不是以前的 javax.servlet 实现(因为我的理解是 jakarta 包是前进的方向)。问题是,当我将浏览器指向
我是 Spring 新手,正在尝试使用 Maven 部署和运行我的第一个 Spring Web 应用程序。有关更多详细信息,请参阅 here (我昨天发布的一个问题): 现在,我遇到的问题是:当我尝试
我正在尝试学习如何使用 JSP。我遇到了一些毫无意义的事情,至少在我看来是这样: 当我尝试运行时: response.getWriter().println(m.getDb().printAll())
我是一名优秀的程序员,十分优秀!