转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudespringboothoutailanjieqi60/javascript
以前我们把用户登陆,注册成功的信息都放到redis里面了,若是产品经理有一种场景,就是同一个用户在同一个时间以最后一个登陆为准,那么前一个就须要从新登陆,而且清空前一个用户缓存。这就用到了springboot的缓存机制。源码:https://github.com/limingios/wxProgram.git 中No.15和springboot前端
经过前端传递过来的userToken,和从redis里面获取到的userToken对比,若是不一致,前端传递过来的这个session奖杯提示用户被挤出,直接缓存失效。须要从新登陆。java
package com.idig8.controller.interceptor; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.idig8.utils.JSONResult; import com.idig8.utils.JsonUtils; import com.idig8.utils.RedisOperator; public class MiniInterceptor implements HandlerInterceptor { @Autowired public RedisOperator redis; public static final String USER_REDIS_SESSION = "user-redis-session"; /** * 拦截请求,在controller调用以前 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception { String userId = request.getHeader("headerUserId"); String userToken = request.getHeader("headerUserToken"); if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(userToken)) { String uniqueToken = redis.get(USER_REDIS_SESSION + ":" + userId); if (StringUtils.isEmpty(uniqueToken) && StringUtils.isBlank(uniqueToken)) { System.out.println("请登陆..."); returnErrorResponse(response, new JSONResult().errorTokenMsg("请登陆...")); return false; } else { if (!uniqueToken.equals(userToken)) { System.out.println("帐号被挤出..."); returnErrorResponse(response, new JSONResult().errorTokenMsg("帐号被挤出...")); return false; } } } else { System.out.println("请登陆..."); returnErrorResponse(response, new JSONResult().errorTokenMsg("请登陆...")); return false; } /** * 返回 false:请求被拦截,返回 * 返回 true :请求OK,能够继续执行,放行 */ return true; } public void returnErrorResponse(HttpServletResponse response, JSONResult result) throws IOException, UnsupportedEncodingException { OutputStream out=null; try{ response.setCharacterEncoding("utf-8"); response.setContentType("text/json"); out = response.getOutputStream(); out.write(JsonUtils.objectToJson(result).getBytes("utf-8")); out.flush(); } finally{ if(out!=null){ out.close(); } } } /** * 请求controller以后,渲染视图以前 */ @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { } /** * 请求controller以后,视图渲染以后 */ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { } }
每个拦截器有须要实现HandlerInterceptor接口,这个接口有三个方法,每一个方法会在请求调用的不一样时期完成,由于咱们须要在接口调用以前拦截请求判断是否登录,因此这里须要使用preHandle方法,在里面是验证逻辑,最后返回true或者false,肯定请求是否合法。ios
原来我们在spring mvc的时候都是经过xml配置文件的方法,springboot为了简化,都是经过java来进行配置,刚建立的拦截器须要配置在webconfig里面git
package com.idig8; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.idig8.controller.interceptor.MiniInterceptor; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Value("${server.file.path}") private String fileSpace; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //资源的路径.swagger2的资源.所在的目录, registry.addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/resources/") .addResourceLocations("file:"+fileSpace); } @Bean public MiniInterceptor miniInterceptor() { return new MiniInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(miniInterceptor()).addPathPatterns("/user/**") .addPathPatterns("/video/upload", "/video/uploadCover") .addPathPatterns("/bgm/**"); super.addInterceptors(registry); } }
在经过userId获取用户的信息时,在header中添加用户的userId,userToken,针对登陆后返回502进行提示并清空用户信息缓存。github
“` javascript
// pages/mine/mine.js
const app = getApp()
var videoUtils = require(‘../../utils/videoUtils.js’)
Page({web
/**
* 页面的初始数据
*/
data: {
faceImage: “../../resource/images/noneface.png”,
nickname: “昵称”,
fansCounts: 0,
followCounts: 0,
receiveLikeCounts: 0,
},
/**
* 用户注销
*/
logout: function(e) {
var user = app.getGlobalUserInfo();
wx.showLoading({
title: ‘正在注销中。。。’
});
wx.request({
url: app.serverUrl + “/logout?userId=” + user.id,
method: “POST”,
header: {
‘content-type’: ‘application/json’ // 默认值
},
success: function(res) {
console.log(res.data);
var status = res.data.status;
wx.hideLoading();
if (status == 200) {
wx.showToast({
title: “用户注销成功~!”,
icon: ‘none’,
duration: 3000
})
// app.userInfo = null;
wx.removeStorageSync(“userInfo”);
wx.redirectTo({
url: ‘../userRegister/userRegister’,
})redis
} else if (status == 500) { wx.showToast({ title: res.data.msg, icon: 'none', duration: 3000 }) } } })
},
/**
* 头像上传
*/
uploadFace: function(e) {
// var user = app.userInfo;
var user = app.getGlobalUserInfo();
var me = this;
wx.chooseImage({
count: 1, // 默认9
sizeType: [‘compressed’], // 能够指定是原图仍是压缩图,默认两者都有
sourceType: [‘album’, ‘camera’], // 能够指定来源是相册仍是相机,默认两者都有
success: function(res) {
// 返回选定照片的本地文件路径列表,tempFilePath能够做为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths
if (tempFilePaths.length > 0) {
console.log(tempFilePaths[0]);
wx.uploadFile({
url: app.serverUrl + “/user/uploadFace?userId=” + user.id, //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: ‘file’,
success: function(res) {
var data = JSON.parse(res.data);
console.log(data);
wx.hideLoading();
if (data.status == 200) {
wx.showToast({
title: “用户上传成功~!”,
icon: ‘none’,
duration: 3000
})
me.setData({
faceUrl: app.serverUrl + data.data
})spring
} else if (data.status == 500) { wx.showToast({ title: data.msg, icon: 'none', duration: 3000 }) } } }) } } })
},
/**
* 生命周期函数–监听页面加载
*/
onLoad: function(options) {
var me = this;
var userInfo = app.getGlobalUserInfo();
wx.showLoading({
title: ‘正在获取用户信息。。。’
});
wx.request({
url: app.serverUrl + “/user/queryByUserId?userId=” + userInfo.id,
method: “POST”,
header: {
‘content-type’: ‘application/json’, // 默认值
‘headerUserId’: userInfo.id,
‘headerUserToken’: userInfo.userToken
},
success: function(res) {
console.log(res.data);
var status = res.data.status;apache
if (status == 200) { var userInfo = res.data.data; wx.hideLoading(); var faceImage = me.data.faceUrl; if (userInfo.faceImage != null && userInfo.faceImage != '' && userInfo.faceImage != undefined) { faceImage = app.serverUrl + userInfo.faceImage; } me.setData({ faceImage: faceImage, fansCounts: userInfo.fansCounts, followCounts: userInfo.followCounts, receiveLikeCounts: userInfo.receiveLikeCounts, nickname: userInfo.nickname }) } else if (status == 502){ wx.showToast({ title: res.data.msg, duration:3000, icon:'none', complete:function(){ wx.removeStorageSync("userInfo"); wx.navigateTo({ url: '../userLogin/userLogin', }) } }) } } })
},
uploadVideo: function(e) {
videoUtils.uploadVideo();
},
/**
* 生命周期函数–监听页面初次渲染完成
*/
onReady: function() {
},
/**
* 生命周期函数–监听页面显示
*/
onShow: function() {
},
/**
* 生命周期函数–监听页面隐藏
*/
onHide: function() {
},
/**
* 生命周期函数–监听页面卸载
*/
onUnload: function() {
},
/**
* 页面相关事件处理函数–监听用户下拉动做
*/
onPullDownRefresh: function() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function() {
}
})
““
PS:经过拦截器的方式很好的保护后台的程序正常的运行。