|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
package com.yipin.liuwanr.filter; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.yipin.liuwanr.entity.UserIdEnum; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate; |
|
|
|
|
import org.springframework.data.redis.core.ValueOperations; |
|
|
|
@ -21,6 +22,7 @@ import java.util.concurrent.TimeUnit; |
|
|
|
|
**/ |
|
|
|
|
public class AuthInterceptor implements HandlerInterceptor { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
StringRedisTemplate stringRedisTemplate; |
|
|
|
|
|
|
|
|
@ -28,24 +30,29 @@ public class AuthInterceptor implements HandlerInterceptor { |
|
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
|
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
|
response.setContentType("text/html;charset=utf-8"); |
|
|
|
|
String token = request.getHeader("token"); |
|
|
|
|
if (StringUtils.isEmpty(token)) { |
|
|
|
|
response.getWriter().print("0");//用户未登录,请登录后操作!
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); |
|
|
|
|
Object loginStatus = ops.get(token); |
|
|
|
|
//userid
|
|
|
|
|
String userId = request.getParameter("userId"); |
|
|
|
|
if( Objects.isNull(loginStatus)){ |
|
|
|
|
response.getWriter().print("1");//token错误
|
|
|
|
|
return false; |
|
|
|
|
if (UserIdEnum.getUserId(Integer.parseInt(userId))!=0){ |
|
|
|
|
return true; |
|
|
|
|
}else { |
|
|
|
|
if (!StringUtils.isEmpty(userId)){ |
|
|
|
|
return userId.equals(loginStatus); |
|
|
|
|
|
|
|
|
|
String token = request.getHeader("token"); |
|
|
|
|
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); |
|
|
|
|
Object loginStatus = ops.get(token); |
|
|
|
|
if (StringUtils.isEmpty(token)) { |
|
|
|
|
response.getWriter().print("0");//用户未登录,请登录后操作!
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if( Objects.isNull(loginStatus)){ |
|
|
|
|
response.getWriter().print("1");//token错误
|
|
|
|
|
return false; |
|
|
|
|
}else { |
|
|
|
|
return true; |
|
|
|
|
if (!StringUtils.isEmpty(userId)){ |
|
|
|
|
return userId.equals(loginStatus); |
|
|
|
|
}else { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|