1 web.xml里添加filter配置信息。
<filter>
<filter-name>Authentication</filter-name>
<filter-class>
com.nova.colimas.web.filters.AuthenticationFilter
</filter-class>
<init-param>
<param-name>onError</param-name>
<param-value>/pages/index.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Authentication</filter-name>
<url-pattern>/protect/*</url-pattern>
</filter-mapping>
<filter-class>定义使用com.nova.colimas.web.filters.AuthenticationFilter类执行过滤Action。
<init-param>当验证失败后Forward到/pages/index.jsp
<filter-mapping>只有当地址包括/protect/时运行filter类
2 实现com.nova.colimas.web.filters.AuthenticationFilter
package com.nova.colimas.web.filters;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import com.nova.colimas.web.bean.UserBean;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.*;
import com.nova.colimas.web.constants.Constants;
/**
* Filter implementation for user authentication。必须实现Filter接口
* @author tyrone
* @version $Id: AuthenticationFilter.java,v 1.0
*/
public class AuthenticationFilter implements Filter
{
/**
* filterConfig will store the reference of FilterConfig
*/
private FilterConfig filterConfig;
private String onErrorUrl;
/**
* store the reference of the FilterConfig.
* @param config FilterConfig object
* @throws ServletException
*/
public void init(FilterConfig config) throws ServletException
{
//获得验证失败forward地址
filterConfig = config;
onErrorUrl=filterConfig.getInitParameter("onError");
if (onErrorUrl==null || "".equals(onErrorUrl)){
onErrorUrl="/pages/index.jsp";
}
}
/**
* User Authentication is done. If User is authenticated successful then
* control is transferred to logon URI
* @param ServletRequest Request
* @param ServletRequest Response
* @param FilterChain Filter Chain
* @throws ServletException,IOException
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
throws IOException, ServletException
{
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse=(HttpServletResponse)response;
// Current session
HttpSession httpSession = httpRequest.getSession();
//Session里是否有用户信息。
if (httpSession.getAttribute(Constants.USER_KEY) == null)
{
ActionErrors errors=new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error authentication"));
httpRequest.setAttribute(Globals.ERROR_KEY,errors);
//没有,验证失败forward到/pages/index.jsp httpRequest.getRequestDispatcher(onErrorUrl).forward(httpRequest,httpResponse);
}else
//成果过滤Action结束
next.doFilter(request,response);
}
/**
* destroy() method is called by the servlet container
*/
public void destroy()
{
}
}
分享到:
相关推荐
Struts2是一款强大的Java Web框架,它为开发者提供了一种优雅的方式来实现MVC(Model-View-Controller)架构,使得应用程序的开发更为简洁、高效。本教程将详细讲解如何手动搭建一个完整的Struts2环境,包括引入所有...
在探讨Struts2.5中的`web.xml`配置时,我们首先需要理解`web.xml`在Java Web应用中的角色以及它如何与Struts框架交互。`web.xml`是Java Web应用的核心配置文件之一,用于定义应用程序级别的配置信息,如过滤器、监听...
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping> `...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping>...
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping> `...
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping> ``` ...
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping> ``` ...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping>...
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping> ``` ...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <param-name>actionPackages <param-value>com.mycompany.myapp.actions</param-value> </filter> ...
本文档将详细介绍如何在Java Web项目中搭建EXTJS和STRUTS2框架,并通过一个简单的实例来展示如何使这两个技术协同工作。EXTJS是一个用于构建交互式前端界面的JavaScript库,而STRUTS2则是Java后端开发中的流行框架之...
2. 让这个类继承自`org.apache.struts2.dispatcher.ng.ExecuteOperations`和`org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter`,并实现`execute()`方法。 3. 添加一个返回值,比如"success...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> ``` **解析**:这两个过滤器都是用于处理Struts2框架的请求。`FilterDispatcher`是旧版本Struts...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping>...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/* </filter-mapping>...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>Struts2</filter-name> <url-pattern>/* <dispatcher>REQUEST...
Struts框架是Java Web开发中的一个关键组件,它属于MVC(Model-View-Controller)设计模式的一种实现,主要用于构建可维护性和可扩展性高的Web应用程序。本教程将引导你首次接触并使用Struts框架,让你理解其核心...
以上配置文件内容提供了Struts2框架的基本配置,通过这些配置可以实现Struts2框架的基础功能,并为后续的功能扩展打下坚实的基础。 综上所述,徒手配置Struts2虽然可能会遇到不少挑战,但只要按照正确的步骤进行...