发生场景
在spring boot项目中我在postHandle(登录拦截)中,通过ThreadLocal判断其中有没有存储user信息来判断是否将user信息给到前端。
但是在我退出登录之后(通过setUser(null))来清除存储的user信息之后。
连续请求首页,请求首页时调用了我的ThreadLocal.getUser(),方法在连续请求10几次之后,突然能请求到一次用户信息。
2022-05-02 22:51:30.845 INFO 26368 --- [nio-8001-exec-7] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:30.845 INFO 26368 --- [nio-8001-exec-7] com.hwx.goodsSystem.Component.goodsPand : user: user(id=3, userName=10086@qq.com, userPassword=null, sex=null, age=null, salt=null, petName=null, imageUrl=/img/hwx.png, dwell=null, signature=null, createTime=Sat Apr 30 22:52:52 CST 2022, updateTime=Sat Apr 30 22:52:52 CST 2022)
2022-05-02 22:51:31.014 INFO 26368 --- [nio-8001-exec-9] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:31.014 INFO 26368 --- [nio-8001-exec-9] com.hwx.goodsSystem.Component.goodsPand : user: null
2022-05-02 22:51:31.168 INFO 26368 --- [nio-8001-exec-8] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:31.168 INFO 26368 --- [nio-8001-exec-8] com.hwx.goodsSystem.Component.goodsPand : user: null
2022-05-02 22:51:31.353 INFO 26368 --- [nio-8001-exec-1] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:31.354 INFO 26368 --- [nio-8001-exec-1] com.hwx.goodsSystem.Component.goodsPand : user: null
2022-05-02 22:51:31.525 INFO 26368 --- [nio-8001-exec-4] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:31.526 INFO 26368 --- [nio-8001-exec-4] com.hwx.goodsSystem.Component.goodsPand : user: null
2022-05-02 22:51:31.684 INFO 26368 --- [nio-8001-exec-3] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:31.684 INFO 26368 --- [nio-8001-exec-3] com.hwx.goodsSystem.Component.goodsPand : user: null
2022-05-02 22:51:31.849 INFO 26368 --- [nio-8001-exec-2] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:31.850 INFO 26368 --- [nio-8001-exec-2] com.hwx.goodsSystem.Component.goodsPand : user: user(id=3, userName=10086@qq.com, userPassword=null, sex=null, age=null, salt=null, petName=null, imageUrl=/img/hwx.png, dwell=null, signature=null, createTime=Sat Apr 30 22:52:52 CST 2022, updateTime=Sat Apr 30 22:52:52 CST 2022)
2022-05-02 22:51:32.008 INFO 26368 --- [io-8001-exec-10] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:32.009 INFO 26368 --- [io-8001-exec-10] com.hwx.goodsSystem.Component.goodsPand : user: null
2022-05-02 22:51:32.187 INFO 26368 --- [nio-8001-exec-7] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:32.188 INFO 26368 --- [nio-8001-exec-7] com.hwx.goodsSystem.Component.goodsPand : user: user(id=3, userName=10086@qq.com, userPassword=null, sex=null, age=null, salt=null, petName=null, imageUrl=/img/hwx.png, dwell=null, signature=null, createTime=Sat Apr 30 22:52:52 CST 2022, updateTime=Sat Apr 30 22:52:52 CST 2022)
2022-05-02 22:51:32.358 INFO 26368 --- [nio-8001-exec-9] com.hwx.goodsSystem.Component.goodsPand : session: null
2022-05-02 22:51:32.359 INFO 26368 --- [nio-8001-exec-9] com.hwx.goodsSystem.Component.goodsPand : user: null
具体原因
所以及时调用ThreadUser.remove();方法清除