在我的web應用程序,我有3個主要部分 1.客戶 2.供應商 3.管理員Java會話過濾器
我使用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>
能否請您闡述一下我是如何實現servlet身份驗證或任何有用的鏈接。 是否與檢查每個頁面上的用戶會話相同? – Parvesh 2012-03-27 06:04:52
我想你的意思是這樣的: http://www.informit.com/articles/article.aspx?p=24253 我試過使用基於表格 - 使用jdbc .. realms。無法配置它,這就是爲什麼使用會話篩選器 – Parvesh 2012-03-27 06:09:54
是的,您可以在容器中配置基於數據庫的Realm(這是特定容器)並配置上面定義的安全約束。 – 2012-03-27 06:28:26