①DAO层
/**
* 登录校验
*
@return
*/
User loginCheck(
@Param
(
“userId”
)Integer userId,
@Param
(
“userPassword”
)String userPassword);
②service层
@Override
public
ServerResponse<User> loginCheck(String userId, String password) {
User user =
userDao
.loginCheck(Integer.
valueOf
(userId),password);
if
(user!=
null
){
return
ServerResponse.
createBySuccess
(
“登录成功”
,user);
}
return
ServerResponse.
createByError
(
“登录失败”
);
}
③controller层
@RequestMapping
(value =
“/loginCheck”
,method = RequestMethod.
GET
)
@ResponseBody
public
ServerResponse<User> loginCheck(HttpSession session,
@RequestParam
(
“userId”
) String userId,
@RequestParam
(
“userPassword”
)String userPassword){
ServerResponse<User> serverResponse =
userService
.loginCheck(userId,userPassword);
if
(serverResponse.checkIsSuccess()){
//设置session
session.setAttribute(
“user”
,serverResponse.getData());
return
serverResponse;
}
return
ServerResponse.
createByError
(
“账号或密码错误!”
);
}
④操作其它功能前需要登录校验,以session为传参,定义一个通用方法
public class
CheckRole {
/**
* 判断是否登录,是否为管理员
*
@param
session
*
@return
*/
public static
ServerResponse checkIsLoginAndAdmin(HttpSession session){
User user = (User) session.getAttribute(
“user”
);
if
(user ==
null
){
return
ServerResponse.
createByNeedLogin
();
}
if
(user.getUserRole()!=
1
){
return
ServerResponse.
createByError
(
“非管理员,无此权限!”
);
}
return
ServerResponse.
createByCheckSuccess
();
}
}
⑤调用实例
参考我的博客:SSM框架中封装后端返回的Json数据
版权声明:本文为weixin_42228338原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。