我正在开发一个安全的web应用程序。我必须开发一个servlet来自定义控制它的访问。
例如,我的home.jsp非常开放,这是一个依赖于content.jsp模式的content.jsp。
如果它是content.jsp,则应该可以大范围地显示内容列表。如果它是content.jsp?chapter=1&detail=true,它应该检查用户是否登录,只有当用户有正确的访问权限时,它才会授予权限,否则,将其重定向到登录页面。
我对Java中的JSESSIONID管理感到困惑。默认情况下,如果没有人,任何JSP都会自动创建会话。我知道我可以使用@page session="false“覆盖它的默认行为。
所以,我想逻辑会是这样的
我只是困惑于
When should I call getSession(true) and getSession(false)? I assume per user session, I should call getSession(true) once.
Which session id should I use (trust) to identify if the user is truly logged on? 谢谢。
发布于 2012-06-09 11:32:46
这个很好用
home.jsp
session = request.getSession(false);
//false: new session will not be created
if(session == null){
//not in a session
//allow to view the home.jsp page and to login
//set an attribute into session
session.setAttribute("authorized","yes");
}
else{//in case a default session automatically establishes
if(session.getAttribute("authorized")==null){
//not in a valid session
//allow to login
}
else{//already in session, so cann't view the login page
//redirect to the home page by "RequestDispatcher"
} contact.jsp
在这里做同样的事情
1-如果不在会话中,则重定向到home.jsp
2-如果在会话中但是session.getAttribute("authorized")给出了null,那么也可以重定向到home.jsp
3- (如果在会话中)和session.getAttribute("authorized")给出值"yes",则
允许contact.jsp
退出时
session.removeAttribute("authorized");
session.invalidate(); 但你将面临一个问题。当您在contact.jsp并单击浏览器的后退按钮时,您将
请参阅home.jsp,即登录页或,当您注销时,在logout.jsp中单击后退
按钮的浏览器,您将看到contact.jsp。为了避免出现这种情况,您必须在每一页中放置这些3行
想要访问control.in的地方-- home.jsp和contact.jsp --你的情况
<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires", -1);
%>发布于 2014-06-11 06:46:24
使用SERVLET创建一个使用会话的登录应用程序
https://stackoverflow.com/questions/10922104
复制相似问题