发表于: 2017-05-03 23:40:27

1 1091


今天完成的事:

1、设置t10页面不需登录可访问,t11需要登录才可访问(此处只在请求t11的控制器中判断cookie中是否包含name和pwd且能且能在数据库查找到对象,如果cookie=null或不能通过name和,pwd查找到对象则需要登录,登录后将name和pwd存入cookie中)(代码很烂)

    @RequestMapping("/getvocation")
public String getVocation(@RequestParam("id") int id,
                             Model model,
                             HttpServletRequest request,
                             HttpServletResponse response){
log.info("getVocation 方法被调用");

       List<Vocation_msg> listVocation=new ArrayList<Vocation_msg>();
       Vocation_msg vocation_msg=new Vocation_msg();
       String msg="传入的参数有误";
       String name="";
       String pwd="";
       Cookie[] cookies=request.getCookies();
       for (Cookie cookie:cookies){
log.info(" "+cookie.getValue());
           if(cookie.getName().equals("name")){
name=cookie.getValue();
               log.info("name="+name);
           }
if(cookie.getName().equals("password")){
pwd=cookie.getValue();
               log.info("pwd="+pwd);
           }
}
List<Student> stulist=studentService.getByNameAndPwd(name,pwd);
       if(!(stulist.size()>0)){
log.info("cookie的内容不对"+studentService.getByNameAndPwd(name,pwd));
           return "redirect:/login";
//            try {
//                response.sendRedirect("/login");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
       }
switch (id){
case 0:
listVocation= vocationService.getAllVocation();
               model.addAttribute("list",listVocation);
               log.info("取得的全部职业信息为:"+listVocation);
               return "t11";
           case 1:
vocation_msg=vocationService.getVocationById(id);
               model.addAttribute("",vocation_msg);
               return "home";
           case 2:
vocation_msg=vocationService.getVocationById(id);
               model.addAttribute("",vocation_msg);
               return "home";


       }
//        model.addAttribute("",msg);
       return "test01";
   }
@RequestMapping("/loginer")
public String login(
HttpServletRequest request,
       HttpServletResponse response,
       @RequestParam String name,
       @RequestParam String password
){
log.info("login方法被调用!name="+name+",password="+password);
   if(studentService.getByNameAndPwd(name,password)!=null){
Cookie name1=new Cookie("name",name);
       name1.setMaxAge(60*60*24);
       response.addCookie(name1);
       Cookie pwd=new Cookie("password",password);
       pwd.setMaxAge(60*60*24);
       response.addCookie(pwd);
       return "redirect:/home";
   }else {
return "login";
   }
}

2、同门那里传来一个例子(代码非常规范,应该是出自于大师之手,来教我们入门的,看了一个下午,不能完全看明白。)

    //主页
   @RequestMapping(value = "/index.html",method = RequestMethod.GET)
public String index(Model model,HttpServletRequest request)
{
List<Photos> photosList=photoService.select();
       List<Cooperate> cooperateList=cooperateService.select();
       List<Links> linksList=linksService.select();
       Statistics statistics=statisticsService.select();
       List<Student> studentList=studentService.select();
       String contextpath;
       contextpath = request.getScheme() +"://" + request.getServerName() + ":" +request.getServerPort() +request.getContextPath();
       model.addAttribute("contextpath",contextpath);
       //list.jsp+mode=ModelAndView
       model.addAttribute("photosList",photosList);
       model.addAttribute("cooperateList",cooperateList);
       model.addAttribute("linksList",linksList);
       model.addAttribute("statistics",statistics);
       model.addAttribute("studentList",studentList);

       return "index.首页.ptteng";
   }
//职业页面
   @RequestMapping(value = "/u/occupation.html",method = RequestMethod.GET)
public String occupation(Model model,HttpServletRequest request)
{
List<Occupation> occupationList=occupationService.select();
       model.addAttribute("occupationList",occupationList);
       String contextpath;
       contextpath = request.getScheme() +"://" + request.getServerName() + ":" +request.getServerPort() +request.getContextPath();
       model.addAttribute("contextpath",contextpath);
       return "occupation.职业.ptteng";
   }
//登录页面
   @RequestMapping(value = "/login.html",method = RequestMethod.GET)
public String login(){
return "login";
   }
//登录失败页面
   @RequestMapping(value = "/no.html",method = RequestMethod.GET)
public String no(){
return "no";
   }
//登录处理
   @RequestMapping(value = "/login.html",method = RequestMethod.POST)
public void loginaction(@RequestParam("username") String username, @RequestParam("password") String password,
                              HttpServletResponse httpServletResponse) {
String md5= MD5Util.stringToMD5(password);
       if(userService.verification(username,md5)){
//待加密内容
           long id=userService.selectToUsername(username).getId();
           long creatDate=new Date().getTime();
           String str = id+"="+creatDate;
           //加密操作
           byte[] result = DesUtil.desCrypto(str,"12345678");
           //把加密的字节转换为16进制
           String resules= TypeUtil.bytesToHexString(result);
           Cookie cookie = new Cookie("token",resules);
           cookie.setMaxAge(60*60*24*7);//保留7
           httpServletResponse.addCookie(cookie);
           try {
httpServletResponse.sendRedirect("index.html");
           } catch (IOException e) {
e.printStackTrace();
           }
}else{
try {
httpServletResponse.sendRedirect("no.html");
           } catch (IOException e) {
e.printStackTrace();
           }
}
}
//注册页面
   @RequestMapping(value = "/register.html",method = RequestMethod.POST)
public String register(@RequestParam("user") String user,@RequestParam("passwd") String passwd){
String md5= MD5Util.stringToMD5(passwd);
           userService.insert(user,md5);
           return "ok";

   }
}

明天计划的事:将任务的加密部分和拦截器完成。

收获:

遇到的问题:今天刚开始时对token的生成不太理解(自己以为token是固定的规范化生成的),网上看的生成token的方法不一有点晕,后来参照同门的例子和思考,才觉得他只是将一些(字符串)字节等加密(加密方法多样)后放入cookie中命名成token。

总结:(今天看到大师出手的例子,把自己怔到了,觉得自己实在是太菜了,并又怀疑了下人生)要好好学习。


返回列表 返回列表
评论

    分享到