spring mvc login / logout using session

时间:2014-07-04 10:48:50

标签: spring-mvc

在此登录示例中,我尝试向用户添加会话。基本登录功能正常。当我返回主页时,我无法访问日志成功页面中的页面。我需要再次登录我是春天的新手,请告诉我应该怎么开会。

Logincontoller.java

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("login.html")
    public String toLogin(Model model) {
        Login login = new Login(); 
        model.addAttribute("login",login);
        return "login";
    }

    @RequestMapping(value ="login.html", method = RequestMethod.POST)
    public ModelAndView  doLogin(@Valid @ModelAttribute ("login") Login login, 
            BindingResult bindingresult,HttpSession session ) {
         ModelAndView view = new ModelAndView("login");
        if(!bindingresult.hasErrors()){
            if(!loginService.authenticateUser(login)){
                bindingresult.addError(new ObjectError("invalid", "Invalid Credentials!!!"));
                return new ModelAndView("error");
            }
            else{
                session.setAttribute("login", login);
                view.setViewName("success");
            }
        }

         return view;
}

    @RequestMapping("/logout")
      public String logout(HttpSession session ) {
         session.invalidate();
         return "redirect:/login.html";
      }

}

1 个答案:

答案 0 :(得分:1)

@RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request) {
        System.out.println("logout()");
        HttpSession httpSession = request.getSession();
        httpSession.invalidate();
        return "redirect:loginformRichUI.html";
    }