有时候需要在全局范围内保存用户的一些信息:
$("#loginBtn")
.click(
function() {
$
.ajax({
type : "post",
url : "/passport/login.do",
data : {
username : $(
"#username")
.val(),
password : $(
"#password")
.val()
},
dataType : "json",
success : function(data) {
if (data.success) {
location.href = "/admin/goAdmin.do";
} else {
alert(data.message);
}
},
error : function(
XMLHttpRequest,
textStatus,
errorThrown) {
var obj = $
.parseJSON(XMLHttpRequest.responseText);
if (obj != null) {
alert(obj.message);
}
}
});
})
@RequestMapping(value="/login.do")
@ResponseBody
public Map<String,Object> login(
UserVO vo,
HttpServletRequest request,HttpServletResponse response){
if(StringUtils.isBlank(vo.getUsername())||StringUtils.isBlank(vo.getPassword())){
throw new ClientException(Code.CODE_PASSPORT_USERNAMEPASSWORD_NULL);
}
userService.login(vo);
ResponseResult result= new ResponseResult();
result.setSuccess(true);
return result.returnResult();
}
public int login(UserVO vo) throws ClientException {
// TODO Auto-generated method stub
vo.setPassword(MD5Utils.md5(vo.getPassword()));
User user = getBaseDao().queryForObject("User.selectUserByNameAndPassword",vo);
if (user==null) {
throw new ClientException(Code.CODE_PASSPORT_USERNAMEPASSWORD_WRONG);
}
if(user.getStatus()==Constants.USER_STATUS_DISABLED){
throw new ClientException(Code.CODE_PASSPORT_USER_DISABLED);
}
if(user.getStatus()==Constants.USER_STATUS_LOCKED){
throw new ClientException(Code.CODE_PASSPORT_USER_LOCKED);
}
if(user.getStatus()==Constants.USER_STATUS_EXPIRED){
throw new ClientException(Code.CODE_PASSPORT_USER_EXPIRED);
}
HttpSession session = RequestUtil.getRequest().getSession();
session.setAttribute(Constants.SESSION_KEY, user);
return user.getId();
}
将用户User类放入session中。