我想像public User getUserFromSession()
,private void setUserInSession(User user)
一样编写方法。我希望当用户登录时,它可以保存他的信息。我尝试但失败了,我使用spring-mvc,我想为getUserFromSession
和setUserInSession
编写方法,当我使用anoher控制器时,我可以调用getUserFromSession()
,我可以得到用户信息。怎么做?
我的登录控制器:
@RequestMapping(value = "/login")
public ModelAndView loginCheck(HttpServletRequest request) {
String userName = request.getParameter("j_username");
String userPwd = request.getParameter("j_password");
boolean isValidUser = populator.loginCheck(userName, userPwd);
if (!isValidUser) {
return new ModelAndView("auth/loginpage", "error", "name or password error!");
} else {
request.setAttribute("j_username", userName);
return new ModelAndView("auth/main");
}
}
我的服务
@Transactional
public boolean loginCheck(String userName, String password) {
boolean rtn = false;
log.debug(userName);
if (!StringUtils.isEmpty(userName)) {
User loginUser = template.lookup(User.class, "login", userName).to(User.class).singleOrNull();
if (loginUser != null) {
String userPassword = loginUser.getPassword();
if (new Md5PasswordEncoder().encodePassword(password, User.SALT).equals(userPassword)) {
rtn = true;
//setUserInSession(loginUser);
//log.debug( "====== loginCheck getUserFromSession ===="+getUserFromSession());
}
}
} else {
log.warn("you must input user name");
}
return rtn;
}
public User getUserFromSession();
private void setUserInSession(User user) ;
答案 0 :(得分:0)
为什么不管理弹簧方式
<bean id="userInfoBean" class="com.fwk.session.UserInfoImpl" scope="session">
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
为该用户保留bean的所有信息并以春天的方式工作。