java这个登录过滤器实现安全吗?
我有一个登录筛选器,如下所示:
@WebFilter("*.xhtml")
public class LoginFiltre implements Filter {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) arg0;
HttpServletResponse res = (HttpServletResponse) arg1;
System.out.println(req.getRequestURI());
System.out.println(req.getContextPath());
Credentials credentials = (Credentials) req.getSession().getAttribute(
"credentials");
if (req.getRequestURI().contains("login")) {
System.out
.println("login olmak istiyor faces servlet e yönlendirilecek");
chain.doFilter(arg0, arg1);
} else if (credentials != null
&& credentials.getUsername().length() != 0
&& credentials.isIsloggedin()) {
System.out.println("login olmus faces servlet e yönlendiriliyor");
chain.doFilter(arg0, arg1);
} else {
System.out.println("login olmamis yönlendirilecek");
res.sendRedirect(req.getContextPath() + "/login.xhtml");
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
我希望此筛选器检查除登录页之外的所有请求的身份验证。将所有页面放在名为secure(login.xhtml除外)的文件夹中,并为此筛选器使用/secure/*之类的前缀映射如何
# 1 楼答案
这张支票
他很虚弱。如果你有一个
logins.xhtml
列表列出所有登录的用户怎么办?您允许每个URL只包含字符“login”。您应该执行精确的URL匹配并在重定向URL中重复使用:
与具体问题无关,该过滤器也会阻塞CSS/JS资源。所以所有的
<h:outputStylesheet>
和<h:outputScript>
资源都停止工作。你也许也想允许他们这样做。此外,.xhtml
扩展和您的问题历史表明您正在使用JSF。当会话过期或用户在另一个选项卡上注销时,这个过滤器将失败,并且没有对JSF ajax请求的反馈。相反,您应该返回一个特殊的XML响应,以在JavaScript端触发重定向。你可以在这个答案中找到另一个例子,它涵盖了这一切:Authorization redirect on session expiration does not work on submitting a JSF form, page stays the same# 2 楼答案
我知道这不是你想要的答案,但安全是最好留给专业人士的事情之一。除非你有一套非常独特的需求,否则你应该试着找到一个合适的开源库,比如Spring Security,Apache Shiro,等等。他们花了很多时间来确保他们的实现是安全的,并且他们被广泛部署,所以他们对他们有很多关注。当您使用自己的安全性时,您可能会遇到引入漏洞的风险,这些漏洞可能会在以后导致漏洞