【发布时间】:2019-07-19 07:15:57
【问题描述】:
我编写了一个带有自定义登录系统的应用程序。然后为它编写了我自己的安全过滤器来设置可以访问的区域。然而,我总是被重定向到登录页面,然后是登录主页的索引页面。我发现会话 ID 与我登录时和尝试使用受限制的东西时不同。这是我的代码:
public class securtityFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
//To change body of implemented methods use File | Settings | File Templates.
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
// if there is no userBean, then they have not gone through
// the login, so kick them to the login page
if(null==req.getSession().getAttribute("username"))
{
((HttpServletResponse)servletResponse).sendRedirect("../Login.jsp");
System.out.println("Redirected - No session");
}
// otherwise, let them go to the page/resource they want
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("Gone through Filter");
// System.out.println("In Filter Servlet: "+ req.getSession().getId());
}
public void destroy() {
//To change body of implemented methods use File | Settings | File Templates.
}
}
这是我的 web.xml 文件:
<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>filters.securtityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>/add/*</url-pattern>
</filter-mapping>
【问题讨论】:
-
您确定登录页面设置了用户名属性吗?
-
在登录 servlet 中是:while(rs.next()) { HttpSession session = request.getSession(true);字符串 tmp = rs.getString(1); System.out.println(tmp); session.setAttribute("用户名",tmp); /*角色 = rs.getInt("级别"); session.setAttribute("role",role);*/ count++; }
标签: java session authentication servlets