我实现了一个filter-class来检查用户是否已经登录。
在登录期间,会创建一个cookie和一个数据库条目,并且Filter-Class会检查是否设置了某些会话属性,或者该cookie是否与数据库记录等效。
通常它工作正常但在Tomcat-Restart之后我必须再次登录。我关闭电脑也一样。
我盯着调试我的过滤器类,并发现现有只有一个cookie:
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
res.setDateHeader("Expires", 0); // Proxies.
boolean positive = false;
//is ok
if (req.getSession().getAttribute("Benutzername") != null && req.getSession().getAttribute("BenID") != null) {
positive = true;
}
if (!positive) {
cookies.GetCookie gc = new cookies.GetCookie(req.getCookies());
/**
Checks...
**/
}
try {
fc.doFilter(request, response);
} catch (NullPointerException nupo) {
Logger.getLogger(SessionCheckFilter.class.getName()).log(Level.SEVERE, null, nupo);
if (request == null) {
Logger.getLogger(SessionCheckFilter.class.getName()).log(Level.INFO, "Request is null");
}
if (response == null) {
Logger.getLogger(SessionCheckFilter.class.getName()).log(Level.INFO, "Response is null");
}
}
}
行req.getCookies()
只返回cookie JSESSIONID。
再次登录后,可以再次访问所有cookie ...这怎么可能?
这里也是web.xml,难道我忘记了什么......?我使用Tomcat 8.0.15.0和JDK 1.8作为Java EE 7 Web。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter>
<description>Sollte den Login überprüfen</description>
<filter-name>Loginpruefung</filter-name>
<filter-class>filter.SessionCheckFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Loginpruefung</filter-name>
<url-pattern>/filter/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<res-ref-name>jdbc/sqlserv</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
</web-app>