filter

本文介绍了一个具体的Servlet过滤器实现案例,该过滤器通过拦截请求来检查用户的登录状态,并根据预设的排除路径列表来决定是否放行请求。此外,还实现了P3P头部信息的添加以支持跨域资源共享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**t
* 过滤请求
* @param request 请求
* @param response 响应
* @param filterChain 过滤器链
* @throws IOException IOException
* @throws ServletException ServletException
*/
//stop check
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response;
String requestAction = httpRequest.getRequestURI()
.substring(httpRequest.getContextPath().length());
String basePath = httpRequest.getScheme() + "://"
+ request.getLocalAddr() + ":" + request.getLocalPort()
+ httpRequest.getContextPath();
httpResponse.addHeader("P3P", "CP=CAO PSA OUR");
if (null != excludeList && !excludeList.isEmpty())
{
for (String temp : excludeList)
{
if (!temp.startsWith("/"))
{
temp = (new StringBuilder()).append("/")
.append(temp)
.toString();
}
if (requestAction.startsWith(temp))
{
filterChain.doFilter(request, response);
return;
}
}
}
Agent agent = null;
agent = (Agent)DataContextHolder.getDataContext()
.getContext(Constants.LOGIN_AGENT, DataContext.SCOPE_SESSION);

if (agent == null)
{
httpResponse.setStatus(Constants.NUM_1000);
httpResponse.sendRedirect(basePath + "/timeOut.jsp");
return;
}

filterChain.doFilter(request, response);
}

/**
* 初始化过滤器
* @param config 过滤器配置
* @throws ServletException ServletException
*/
//stop check
public void init(FilterConfig config) throws ServletException
{
String excludeActions = config.getInitParameter("exclude.actions");
if (excludeActions != null)
{
String temp[] = excludeActions.split(".action");
if (temp != null && temp.length > 0)
{
for (int i = 0; i < temp.length; i++)
{
if (null != temp[i] && !"".equals(temp[i]))
{
int beginIndex = temp[i].indexOf("/");
String str = (new StringBuilder()).append(temp[i].substring(beginIndex))
.append(".action")
.toString();
excludeList.add(str);
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值