[英]Filter on j_security_check not working. Glassfish V3 Netbeans 6.8
我正在嘗試在j_security_check上執行過濾器,以執行一些登錄后操作,例如更改重定向URL等。但是問題是我的過濾器從未執行過。 我可以申請任何拼布嗎? 任何幫助,將不勝感激。 我真的受夠了容器管理的安全性。
提前致謝。
您無法以編程方式鈎上/j_security_check
。 這是安全限制。
最好的選擇是通過手動檢查HttpSession
的用戶主體來確定首次登錄,如果不存在則將其放置在其中,然后執行操作。 在此之前,我已經發布了類似的答案。 這是過濾器代碼的一部分,您只需要將過濾器映射到覆蓋安全頁面的所需url-pattern
。
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
UserPrincipal user = httpRequest.getUserPrincipal();
HttpSession session = httpRequest.getSession();
if (user != null && session.getAttribute("user") == null) {
session.setAttribute("user", user);
// First-time login. You can do your intercepting thing here.
}
chain.doFilter(request, response);
}
恕我直言,您不應嘗試攔截容器的身份驗證系統; 在您的情況下,可以在web.xml中以聲明方式設置重定向URL。
如果您想執行一些認證后的操作,建議您設置一個虛擬的認證后servlet / jsp,它可以執行您想要的操作,然后重定向到請求的資源。 然后可以將該身份驗證后servlet正確配置為登錄后頁面。
一種便攜式解決方案。
在模式/ *上注冊全局過濾器;
在doFilter()中,嘗試從會話(即用戶工作空間)中獲取自定義對象;
如果object為null,則將新對象放入會話並執行登錄后邏輯。
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException { Principal principal = request.getUserPrincipal(); if(principal != null) { UserWorkspace uwks = (UserWorkspace) getSession(request).getAttribute("com.foo.myproject.userworkspace"); if (uwks == null) { uwks = new UserWorkspace(principal); getSession(request).setAttribute("com.foo.myproject.userworkspace", uwks); // // post-login code here // } } chain.doFilter(request, response); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.