实体类
- 用户实体类
import java.io.Serializable;
public class PUser implements Serializable {
private String PUserName;
private String Password;
}
清.登录 Copy
- Postman调用
- 逻辑实现
@PostMapping("/loginPost")
public void loginPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
//设置请求编码格式
request.setCharacterEncoding("UTF-8");
//设置响应编码格式
response.setCharacterEncoding("UTF-8");
//设置数据返回的类型
response.setContentType("text/json");
//TODO:获取请求信息
//账号
String PUserNumber = request.getParameter("PUserNumber");
//密码
String Password = request.getParameter("password");
//记住密码
String remember = request.getParameter("remember");
String str;
//通过账号查询用户密码
PUser user = findByPUserNumber(PUserNumber);
//判断账号是否存在
if (user != null) {
//判断密码是否正确
if (user.getPassword().equals(Password)) {
str = "success";
//判断记住密码是否选中
if (remember != null) {
//创建Cookie对象
Cookie c1 = new Cookie("PUserName", PUserNumber);
Cookie c2 = new Cookie("Password", Password);
//设置Cookie的有效期(3天)
c1.setMaxAge(3 * 24 * 3600);
c2.setMaxAge(3 * 24 * 3600);
//响应Cookie信息
response.addCookie(c1);
response.addCookie(c2);
}
} else {
str = "密码错误,请重新输入";
}
} else {
str = "该用户不存在";
}
PrintWriter out = response.getWriter();
out.write(JSON.toJSONString(str));
//刷新该流的缓冲
out.flush();
//关闭流
out.close();
}
清.获取Cookie
- Postman调用
- 逻辑实现
@PostMapping("/loginPost")
public void loginPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
//设置请求编码格式
request.setCharacterEncoding("UTF-8");
//设置响应编码格式
response.setCharacterEncoding("UTF-8");
//设置数据返回的类型
response.setContentType("text/json");
//TODO:获取请求信息
//账号
String PUserNumber = request.getParameter("PUserNumber");
//密码
String Password = request.getParameter("password");
//记住密码
String remember = request.getParameter("remember");
String str;
//通过账号查询用户密码
PUser user = findByPUserNumber(PUserNumber);
//判断账号是否存在
if (user != null) {
//判断密码是否正确
if (user.getPassword().equals(Password)) {
str = "success";
//判断记住密码是否选中
if (remember != null) {
//创建Cookie对象
Cookie c1 = new Cookie("PUserName", PUserNumber);
Cookie c2 = new Cookie("Password", Password);
//设置Cookie的有效期(3天)
c1.setMaxAge(3 * 24 * 3600);
c2.setMaxAge(3 * 24 * 3600);
//响应Cookie信息
response.addCookie(c1);
response.addCookie(c2);
}
} else {
str = "密码错误,请重新输入";
}
} else {
str = "该用户不存在";
}
PrintWriter out = response.getWriter();
out.write(JSON.toJSONString(str));
//刷新该流的缓冲
out.flush();
//关闭流
out.close();
}
清.清除Cookie
- Postman调用
- 逻辑实现
@GetMapping("/cleanCookie")
public void cleanCookie(HttpServletRequest request, HttpServletResponse response) throws IOException {
//设置数据返回的类型
response.setContentType("text/json");
//设置请求编码格式
request.setCharacterEncoding("UTF-8");
//设置响应编码格式
response.setCharacterEncoding("UTF-8");
//设置返回值
JSONObject jsonObject = new JSONObject();
//清除用户名
Cookie pUserName = new Cookie("PUserName", "");
if (0 != pUserName.getMaxAge()) {
pUserName.setMaxAge(0); //设置立即删除
response.addCookie(pUserName);
}
//清除密码
Cookie password = new Cookie("Password", "");
password.setMaxAge(0); //设置立即删除
response.addCookie(password);
PrintWriter out = response.getWriter();
jsonObject.put("success", "清除成功!");
out.write(JSON.toJSONString(jsonObject));
//刷新该流的缓冲
out.flush();
//关闭流
out.close();
}
工具类
- 模拟Service返回数据
/**
* 模拟 Service 返回数据
*
* @param PUserName
* @return PUser
*/
private static PUser findByPUserNumber(String PUserName) {
if ("靳露".equals(PUserName)) {
return new PUser(PUserName, "12");
}
return null;
}