springboot后端获取session,前端页面 session存取,退出登录时清空session中的数据,详情见项目 enroll-online-edu-sys(入学预约登记)

1、退出清空session

  @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpSession session){
        Integer userid = (Integer)session.getAttribute("adminId");
        Object username = session.getAttribute("admin");
        if(StringUtils.isEmpty(userid) && StringUtils.isEmpty(username)){
            return "redirect:/login.html";
        }
        request.getSession().removeAttribute("adminId");
        request.getSession().removeAttribute("admin");
        request.getSession().getServletContext().removeAttribute("user_token");
        return "redirect:/login.html";
    }
引用:https://blog.csdn.net/weixin_43863007/article/details/88406486

2 、springboot后端获取 session 页面存取session

springBoot session存取
存:request.getServletContext().setAttribute(mobile,code);
取: String oldCode = (String) request.getServletContext().getAttribute("code");

前端页面 session存取
components.js 后端 token验证
frontcommon.js 前端 token验证
存: localStorage.setItem("token", r.token);
取:var studentId = localStorage.getItem('studentId');

页面跳转parent.location.href ='/sys/frontH5/myenroll.html';
/**
	 * @description:
	 * 学生登录  loginType 0:密码登录 1:验证码登录
	 * @author: JACKPAN
	 * @date: 2023/7/19 11:22
	 * @param: [telephone, password, captcha, loginType, verifycode, request]
	 * @return: java.util.Map
	 **/
	@RequestMapping(value = "/sys/studentLogin", method = RequestMethod.POST)
	public Map<String, Object> studentLogin(
			@RequestParam(value = "telephone",required = false)  String telephone,
			@RequestParam(value = "password",required = false) String password,
			@RequestParam(value = "loginType",required = false) String loginType,
			@RequestParam(value = "verifycode",required = false) String verifycode, HttpServletRequest request) throws Exception {
		//用户信息
		Student student = studentService.studentLogin(telephone);
		//账号不存在、密码错误
		if (student == null ) {
			return R.error("账号或密码不正确");
		}
		//1、手机验证码登录
		if (!StringUtils.isEmpty(loginType)&&loginType.equals("1")){
			String codeInSession = (String) request.getServletContext().getAttribute("code");
			logger.info("=====================短信验证码==================="+codeInSession);
			if (verifycode.equals(codeInSession)){
				R r = R.ok().put("studentId",student.getStudentId());
				return r;
			}else {
				return R.error("验证码发送异常!");
			}
		}
		//2、密码登录
		//账号不存在、密码错误
		if (student == null || !student.getBackupField2().equals(MD5Util.MD5Encode(password))) {
			return R.error("账号或密码不正确");
		}
		//生成token
		String token = jwtUtils.generateToken(student.getStudentId().longValue());
		request.getServletContext().setAttribute("student", student);
		request.getServletContext().setAttribute("studentId", student.getStudentId());
		request.getServletContext().setAttribute("user_token",token);
		R r = R.ok().put("fronttoken", token).put("studentId",student.getStudentId()).put("stuName",student.getStuName());
		return r;
	}

你可能感兴趣的:(Java,spring,boot,后端,java)