|
@@ -0,0 +1,70 @@
|
|
|
+package cn.efunbox.audio.aop;
|
|
|
+
|
|
|
+import cn.efunbox.audio.entity.Device;
|
|
|
+import cn.efunbox.audio.impl.DeviceServiceImpl;
|
|
|
+import cn.efunbox.audio.service.DeviceService;
|
|
|
+import cn.efunbox.audio.util.ApiCode;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户验证拦截器
|
|
|
+ * Created by yao on 17-9-29.
|
|
|
+ */
|
|
|
+
|
|
|
+public class AuthInterceptor implements HandlerInterceptor {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DeviceService deviceService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean preHandle(HttpServletRequest request,
|
|
|
+ HttpServletResponse response, Object handler) throws Exception {
|
|
|
+ Map map = new HashMap<>();
|
|
|
+ String id = request.getParameter("id");
|
|
|
+ String token = request.getParameter("token");
|
|
|
+ System.out.println("handle");
|
|
|
+ if(id==null && token==null){
|
|
|
+// map.put("code", ApiCode.PARAMETER_ERROR.getCode());
|
|
|
+// map.put("msg", ApiCode.PARAMETER_ERROR.getMessage());
|
|
|
+// return map;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Device device = deviceService.GetOne(Long.valueOf(id));
|
|
|
+ if(device==null ||
|
|
|
+ ((device.getTokenNew()==null || false == device.getTokenNew().equalsIgnoreCase(token))
|
|
|
+ && (device.getTokenOld()==null || false == device.getTokenOld().equalsIgnoreCase(token)))){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if(device.getTokenOld()!=null && device.getTokenOld().equalsIgnoreCase(token)){
|
|
|
+ device.setTokenNew(device.getTokenOld());
|
|
|
+ device.setTokenOld("");
|
|
|
+ deviceService.Update(device);
|
|
|
+ }else if(device.getTokenOld()!=null && false==device.getTokenOld().equalsIgnoreCase(token)){
|
|
|
+ device.setTokenOld("");
|
|
|
+ deviceService.Update(device);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void postHandle(HttpServletRequest request,
|
|
|
+ HttpServletResponse response, Object handler,
|
|
|
+ ModelAndView modelAndView) throws Exception {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterCompletion(HttpServletRequest request,
|
|
|
+ HttpServletResponse response, Object handler, Exception ex)
|
|
|
+ throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|