天天看点

spring boot项目中,在其中一个方法中向Session中存入对象,在另一个方法中取出为null的问题

出问题时:此时,我想先把注册功能的验证码,先存入,Session域对象中,等用户点击注册按钮时,再取出Session中存入的验证码与浏览器的请求中传入的验证码,作比较,来验证,用户输入的验证是否正确,但在此两次请求中的方法的执行过程,先前存入Session对象中值,在第二次请求中,从Session中取时,却为null。

第一次请求,调用的方法

@RequestMapping("/sendMail")
public String sendMail(User user,HttpSession session) {
	String codes="123456789";
	StringBuffer sb=new StringBuffer("");
	
	Random random=new Random();
	
	for(int i=0;i<6;i++) {
		int index=random.nextInt(codes.length());
		char c=codes.charAt(index);
		sb.append(c);
	}
	String content=sb.toString();
	
	session.setAttribute("vericode",content);
	
	System.out.println(session.getAttribute("vericode")+"--刚要存session");
	
	mailService.sendSimpleMail(user.getEmail(),"贝壳找二手房注册验证码", content);
	return "发送完毕";
}
           

第二次的,注册时,调用的方法

@RequestMapping("/regist")
	public String regist(User user,MultipartFile imgFile,HttpSession session) {
		
		String vericode=(String) session.getAttribute("vericode");//自动生成的验证码
		
	   
		
		System.out.println(vericode+"--session");   //
		
		if(user.getVericode().equals(vericode)) {//用户输入的验证码与系统自动生成验证码进行比较
			if(imgFile!=null) {
				String originalFilename=imgFile.getOriginalFilename();
				String  suffix=originalFilename.substring(originalFilename.lastIndexOf("."));//获取文件后缀    
				String newName=UUID.randomUUID().toString().replace("-","")+suffix; //新文件名
				
				try {
					imgFile.transferTo(new File(props.getUploadPath()+newName)); //上传文件
					user.setImage(newName);
					user.setRegtime(new Date());  //注册时间
					user.setStatus(0);   //用户类型
					
					userService.save(user); //将用户信息保存至数据库
					return "ok";
					
				} catch (IllegalStateException|IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return "fail";
				
			}
		}
		
		return "fail";
		
	
	}
	
           

造成这种错误的原因,从原理上是因为,在这样次的方法中Session对象,分别是不同的两个对象,服务器,把两次请求的客户端,当成了两个不同客户端的请求,而服务器,怎样识别请求的来源是不是自己认识的客户端呢?,这就是Cookie会发生的作用。Cookie里会存储这,配对自己客户端的Session对象的一个Id号,这个Id号是独一无二的,由此来判断,一个请求的来源性。

Cookie在默认的情况下,会在请求发出时,存在于请求头中,一起发送到服务器中。

所以,这个错误的原因,应该是发出的请求中没有Cookie的数据,

vue不会自动帮我们保存后端传来的cookie,

需要在main.js中加入 一下代码,

后台,还要加入,跨域的代码

@Configuration
public class WebMVCConfiger implements WebMvcConfigurer {
	static final String ORIGINS[]=new String[] {"GET","POST","PUT","DELETE"};
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
		.allowedOriginPatterns("*")
		.allowCredentials(true)
		.allowedMethods(ORIGINS)
		.maxAge(3600);
	}
}
           

参考博客

https://blog.csdn.net/qq_38128179/article/details/84956552

https://blog.csdn.net/qq_39611230/article/details/108090828

跨域资源共享 CORS 详解

http://www.ruanyifeng.com/blog/2016/04/cors.html

jQuery 简单使用ajax 的跨域,携带Cookie

https://blog.csdn.net/crippty/article/details/80179356?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

不要再问我跨域的问题了

https://segmentfault.com/a/1190000015597029###