【发布时间】:2012-04-10 15:36:05
【问题描述】:
在我的网络应用程序中,我有 3 个主要部分 1. 客户 2. 供应商 3. 管理员
我正在使用 java 会话过滤器来检查用户会话并允许访问网站的特定部分。 因此客户只能访问客户部分,供应商可以访问供应商部分,管理员可以访问管理部分。
客户的会话过滤器已经实现并且工作正常。它检查客户身份验证并提供对客户子文件夹的访问权限,我有几个 jsp。
如果我希望过滤器检查供应商和管理部分的身份验证,并根据他们的用户级别允许他们访问。
我是否需要再创建 2 个过滤器 - 管理员和供应商?
目前这是我为客户实现的:
public class SessionFilter implements Filter {
private FilterConfig config;
/** Creates new SessionFilter */
public SessionFilter() {
}
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Instance created of " + getClass().getName());
this.config = filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws java.io.IOException, ServletException {
HttpSession session = ((HttpServletRequest) request).getSession();
ServletContext context = config.getServletContext();
/*
* use the ServletContext.log method to log filter messages
*/
context.log("doFilter called in: " + config.getFilterName() + " on "
+ (new java.util.Date()));
// log the session ID
context.log("session ID: " + session.getId());
// Find out whether the logged-in session attribute is set
Object u= session.getAttribute("users");
if (u != null){
chain.doFilter(request, response);
}
else{
//request.getRequestDispatcher("../index.jsp").forward(request, response);
((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response).encodeRedirectURL("../index.jsp?error=userpriv"));
}
}
public void destroy() {
}
}
这是我的 web.xml
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>controller.SessionFilter</filter-class>
<init-param>
<param-name>avoid-urls</param-name>
<param-value>index.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/users/*</url-pattern>
</filter-mapping>
【问题讨论】:
标签: java session servlets servlet-filters