发表于: 2017-10-03 21:48:28
1 761
今天完成的事情:
1.写出拦截器,完成拦截器免登录
2.对/u的未登录用户进行拦截
3.完成被拦截页面的登陆后跳转
明天计划的事情:
1.写出注册页面
2.了解过滤器
3.完善代码,尝试写一个过滤器
遇到的问题:
1. 拦截器没有生效
原因:
<mvc:mapping path="/a/u/*"/>
应该有两个*才能拦截
2. session的一次性
原因:在拦截器里不论是否拦截后跳转,拦截器对符合条件的url都在生效,当拦截登陆后自动跳转到被拦截页面,拦截器也在生效,没有进行条件限制,把第二次跳转到被拦截网页的url存入了session。所以在登陆页将session放空无效。
收获:
1. 写出拦截器,完成拦截器免登录
String lastUrl = request.getRequestURL().toString();
HttpSession session = request.getSession();
session.setAttribute("lastUrl",lastUrl);
String DES_KEY = "12345678";
Cookie[] cookies = request.getCookies();
for(int i = 0;i<cookies.length;i++)
//找到名为token的cookie
if ("token".equals(cookies[i].getName()))
loggerLI.info("Cookie-name: " + cookies[i].getName() +
" Cookie-MaxAge: " + cookies[i].getMaxAge() +
" Cookie-Value: " + cookies[i].getValue()+ "\n");
//将value进行分割
String[] results = cookies[i].getValue().split(":");
//对value进行base64逆编码
byte[] str = Base64.decodeBase64(results[0]);
//将str用DES进行解密
DesUtils desUtils = new DesUtils();
Integer id = Integer.valueOf(new String(desUtils.decrypt(str,DES_KEY)));
loggerLI.info("拦截器中cookie的id: "+ id);
try{
int selectId = studyService.studentSelect(id).getId();
if(selectId == id){
session.setAttribute("lastUrl",null);
return true;
}
}catch (Exception e){
e.printStackTrace();
loggerLI.info("e.getMessage(): " + e.getMessage());
loggerLI.info("没有名为: "+ id +"的id");
response.sendRedirect(request.getContextPath()+"/a/login");
return false;
}
}
}
loggerLI.info("没用名为token的cookie");
response.sendRedirect(request.getContextPath()+"/a/login");
return false;
2.对/u的未登录用户进行拦截
<mvc:interceptors>
<!-- 直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->
<mvc:interceptor>
<!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->
<mvc:mapping path="/a/u/**"/>
<!--<mvc:mapping path="/a/login"/>-->
<bean class="task.jnshu.interceptor.LoginInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors
3.完成被拦截页面的登陆后跳转
if(request.getSession().getAttribute("lastUrl") != null)
{
//获取名为lastUrl的session的value
String lastUrl = (String)request.getSession().getAttribute("lastUrl");
loggerController.info("lastUrl: " + lastUrl);
session.setAttribute("lastUrl",null);
loggerController.info(request.getSession().getAttribute("lastUrl"));
response.sendRedirect(lastUrl);
}
进度:
任务开始时间:8.14
预计完成时间:10.6
是否有延期风险:有,可能要加上过滤器
禅道:http://task.ptteng.com/zentao/project-task-264.html
参考资料:
刘继林师兄的git:https://github.com/sweetalin
PS:国庆第三天,明天就中秋了!!!
评论