验证码的失效处理

仅仅做个笔记

public void generate(HttpServletResponse response){
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		String code = drawImg(output);

		Subject currentUser = SecurityUtils.getSubject();
		final Session session = currentUser.getSession();
		session.setAttribute(Const.SESSION_SECURITY_CODE, code);

		System.out.println("验证码已经生成");
		Timer timer = new Timer();// 实例化Timer类
		timer.schedule(new TimerTask() {
			public void run() {
				session.removeAttribute(Const.SESSION_SECURITY_CODE);
				System.out.println("验证码过期");
				this.cancel();
			}
		}, 300000);// 这里百毫秒
		System.out.println("验证码将在5分钟后过期");

		try {
			ServletOutputStream out = response.getOutputStream();
			output.writeTo(out);
			out.close();
		} catch (IOException e) {
			//e.printStackTrace();
		}
	}
主要还是借助了session和定时任务
然后碰到问题了,用户注销后,
因为做了清除session操作
导致重写登录后有报错
	Exception in thread "Timer-1" org.apache.shiro.session.InvalidSessionException: java.lang.IllegalStateException: getAttribute: Session already invalidated
	at org.apache.shiro.web.session.HttpServletSession.removeAttribute(HttpServletSession.java:167)
	at org.apache.shiro.session.ProxiedSession.removeAttribute(ProxiedSession.java:135)
	at com.fh.controller.system.secCode.SecCodeController$1.run(SecCodeController.java:56)
	at java.util.TimerThread.mainLoop(Timer.java:555)
	at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.IllegalStateException: getAttribute: Session already invalidated
	at org.apache.catalina.session.StandardSession.getAttribute(StandardSession.java:1145)
	at org.apache.catalina.session.StandardSessionFacade.getAttribute(StandardSessionFacade.java:103)
	at org.apache.shiro.web.session.HttpServletSession.removeAttribute(HttpServletSession.java:163)
	... 4 more
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值