本文也是結合Shiro的應用,session經過shiro的subject得到html
一、後臺session賦值java
Session session=subject.getSession(); session.setAttribute("user", user);
二、後臺判斷是否已經登陸,若是已經登陸跳轉到主頁,不然跳轉到登陸頁git
@RequestMapping("/") public String main(){ Subject subject=SecurityUtils.getSubject(); Session session=subject.getSession(); if(session.getAttribute("user")!=null&&session.getAttribute("user")!=""){ return "system/index/main"; } return "login"; }
三、前臺使用sessionspring
${sessionScope.user.username}或者${user.username}
四、註銷登陸,移除sessionsession
/** * 用戶註銷 */ @RequestMapping(value="/logout") public String logout(){ //shiro銷燬登陸 Subject subject = SecurityUtils.getSubject(); Session session=subject.getSession(); session.removeAttribute("user"); subject.logout(); return "login"; }
完整代碼:app
http://git.oschina.net/smilease/spring-example/tree/v0.2.3.net