当前位置: 代码迷 >> Web前端 >> Struts 二 - 在 web.xml 中配置拦截器和前后台权限过滤器
  详细解决方案

Struts 二 - 在 web.xml 中配置拦截器和前后台权限过滤器

热度:160   发布时间:2012-10-07 17:28:51.0
Struts 2 - 在 web.xml 中配置拦截器和前后台权限过滤器

现在只是为将来总结提示一下,以后再将运行流程详细解说并将源码添加到附件上,同时加深自己对 Struts 运行机制。

Architects Guide

Struts 2 框架架构图

Figure 1? Struts 2 Framework's Architecture

?

?

AuthorityFilter.java

?

package com.tucue.interceptor;

import java.io.IOException;

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;

/**
 * 文件名称:AuthorityFilter.java
 *
 * 版权信息:TD 实验室
 *
 * 创建日期:2010-6-24
 *
 * 修改历史:
 */

/*
 * 类的功能描述:登录权限过滤器
 * 
 * 作者:Li Huagang
 */
public class AuthorityFilter implements Filter {

	// @see javax.servlet.Filter#destroy()
	@Override
	public void destroy() {
	}

	// @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String uri = request.getRequestURI();
		// 过滤所有的 *.jsp 请求
		if (uri.contains("jsp") || uri.contains("html") || uri.contains("htm")) {
			HttpSession session = request.getSession();
			if (session.getAttribute(AuthorityInterceptor.LOGINED_FLAG) == null) {
				response.sendRedirect(request.getContextPath() + "/admin/jumpLoginPage.action");
				return ;
			}
		}
		chain.doFilter(req, res);

		return ;
	}

	// @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	@Override
	public void init(FilterConfig paramConfig) throws ServletException {
	}

}

?

AuthorityInterceptor.java

package com.tucue.interceptor;

import static com.opensymphony.xwork2.Action.ERROR;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 文件名称:AuthorityInterceptor.java
 *
 * 版权信息:TD 实验室
 *
 * 创建日期:2010-4-22
 *
 * 修改历史:
 */

/*
 * 类的功能描述:登录权限拦截器
 * 
 * 作者:Li Huagang
 */
public class AuthorityInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 1770471187955003755L;
	
	public static String LOGINED_FLAG = "logined";	// 判断用户是否已登录
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		Map<?,?> session = invocation.getInvocationContext().getSession();
		Object logined = session.get(AuthorityInterceptor.LOGINED_FLAG);

		if (logined == null) {
			return ERROR;
		} else {
			return invocation.invoke();
		}
	}
}
?

?

?

  相关解决方案