@RequestMapping(value = "fg/webLogin.html", method = RequestMethod.POST, produces = "text/html; charset=utf-8") public String userLogin(String accountName, String password, HttpServletRequest request) { UsernamePasswordToken token = null; try { // 想要得到Subject对象,访问地址必须在shiro的拦截地址内,不然会报空指针 Subject subject = SecurityUtils.getSubject(); token = new UsernamePasswordToken(accountName, password); subject.login(token); if (subject.isAuthenticated()) { LoginInfoEntity loginInfo = new LoginInfoEntity(); Session session = SecurityUtils.getSubject().getSession(); loginInfo.setUserId(Integer.valueOf(session.getAttribute("userSessionId").toString())); loginInfo.setAccountName(accountName); loginInfo.setLoginIp(session.getHost()); loginInfoService.insert(loginInfo); request.removeAttribute("error"); // 获取登录的bean request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); UserEntity userEntity = (UserEntity) request.getSession().getAttribute("userSession"); }
在登录页面拿到登录的信息放到userSession中 页面可以直接点
<div class="top_wrap"> <div class="top"> <div class="top_left"> 您好,欢迎来到湖里创享在线! <c:if test="${empty userSession}"> <span>请<a href="${ctx }/fg/login.html" title="" class="login">登录</a><a href="${ctx}/fg/personRegister.html" title="" class="mfzc">[免费注册]</a></span> </div> </c:if> <c:if test="${!empty userSession}"> <span>账户名:<a href="${ctx }/ps/person/center.html" title="" class="login">${userSession.accountName}</a><em>|</em><a href="${ctx}/fg/logout.html" title="" class="mfzc">退出</a></span> </div> </c:if> <div class="top_right">0592-5778393</div> </div>
这样网页面的所有登录的都是有值得