NutzCN Logo
问答 过滤器怎么中止一个请求?
发布于 3103天前 作者 chineselio 2369 次浏览 复制 上一个帖子 下一个帖子
标签: mvc

我的需求是在View match(ActionContext context)里面,如果判定这个请求不合法,则不做任何处理,就像Servlet中的Filter不执行chain.doFilter()。

文档--> 7.11.2.2. 定制你自己的过滤器
"
根据 HttpRequest 对象,你的过滤器需要决定返回值是:
一个 View : 这个请求有问题,不要继续执行了,马上用这个 View 来渲染 HTTP 输出流吧
null : 恩,这个请求没问题,继续吧。

"

按照文档上说明,不知道怎么能实现?

1 回复

ActionFilter, 对吧? 我直接给你一个本网站的授权ActionFilter, 过滤客户端请求的

package net.wendal.nutzbook.mvc;

import javax.servlet.http.HttpSession;

import net.wendal.nutzbook.module.BaseModule;
import net.wendal.nutzbook.service.yvr.YvrService;

import org.nutz.integration.shiro.NutShiro;
import org.nutz.lang.Strings;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.View;

/**
 * 通过请求参数中的accesstoken进行授权
 * @author wendal
 *
 */
public class AccessTokenFilter implements ActionFilter {
	
	YvrService yvrService;

	public View match(ActionContext ac) {
		String at = ac.getRequest().getParameter("accesstoken");
		if (Strings.isBlank(at)) {
			return BaseModule.HTTP_403; // 呵呵,呵呵,想骗我? 直接403侍候
		}
		HttpSession session = ac.getRequest().getSession();
		if (session.getAttribute(NutShiro.SessionKey + "_at") != null) {
			String tmp = (String) session.getAttribute(NutShiro.SessionKey + "_at");
			if (tmp.equals(at) && session.getAttribute(NutShiro.SessionKey) != null) {
				return null;
			}
		}
		if (yvrService == null)
			yvrService = ac.getIoc().get(YvrService.class);
		int uid = yvrService.getUserByAccessToken(at);
		if (uid < 1) {
			return BaseModule.HTTP_403; // 呵呵,毛线, 根本不存在这个token, 403侍候
		}
		session.setAttribute(NutShiro.SessionKey, uid);
		session.setAttribute(NutShiro.SessionKey + "_at", at);
		return null;
	}

}
添加回复
请先登陆
回到顶部