配置拦截器
@Configuration public class InterceptorConfig implements WebMvcConfigurer { @Autowired private ResponseInteceptor appInteceptor; @Autowired private PersonInterceptorConfig personInteceptor; /** * 拦截器,在这里加拦截路径 */ @Override public void addInterceptors(InterceptorRegistry registry) { System.out.println("加载 InterceptorConfig..."); // 全局拦截器 registry.addInterceptor(appInteceptor).addPathPatterns("/**"); //个人端登录页面拦截器,拦截特定的请求 registry.addInterceptor(personInteceptor).addPathPatterns("/**/person/**"); System.out.println("已开启拦截器!"); } }
处理拦截页面
@Component public class PersonInterceptorConfig extends HandlerInterceptorAdapter { @Resource private RedisCache<Object> redisCache; // 在控制器执行前调用 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 获取当前登录账户信息 String struuid = null; Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { if (cookie.getName().equalsIgnoreCase(Constants.CookieKey)) { struuid = cookie.getValue(); } } if (struuid == null) { System.out.println("验证不通过"); return false; } String userName = redisCache.get(struuid).toString(); if (userName == null) { System.out.println("验证不通过"); return false; } else { System.out.println("***********************************************"); return true; } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { super.postHandle(request, response, handler, modelAndView); } // 在后端控制器执行后调用 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { super.afterCompletion(request, response, handler, ex); } }
public interface Constants { //自定义cookie的key static String CookieKey="userid"; static String RedisUserKey="userid"; }
登录页面处理
@Resource private RedisCache<Object> redisCache; /** * 个人端登录页面 * * @param */ @RequestMapping(value = "/personLoginById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public JSONResponse personLoginById(@PathVariable("name") String name,@PathVariable("id") String id, HttpServletRequest request,HttpServletResponse response) { try { // @PathVariable("code") String code,@PathVariable("phoneCode") String phoneCode, String uuid = UUIDUtils.getUUID(); //Map<String,Object> map = new HashMap<String,Object>(); Cookie cookie = new Cookie(Constants.CookieKey, uuid); response.addCookie(cookie);//返回客户端 redisCache.set(uuid, name); return ResultUtil.success(); } catch (Exception e) { logger.error(e.getMessage()); return ResultUtil.error(e.getMessage()); } }
redisCache类
public class RedisCache<T> { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private RedisTemplate<String, T> redisTemplate; public static final String KEY_SET_PREFIX = "_set:"; public static final String KEY_LIST_PREFIX = "_list:"; public T get(String key) { logger.debug("get key [{}]", key); try { if (key == null) { return null; } else { return redisTemplate.opsForValue().get(key); } } catch (Throwable t) { logger.error("get key [{}] exception!", key, t); throw new CacheException(t); } } public T set(String key, T value) { logger.debug("set key [{}]", key); try { redisTemplate.opsForValue().set(key, value); return value; } catch (Throwable t) { logger.error("set key [{}] exception!", key, t); throw new CacheException(t); } } public T set(String key, T value, long timeout) { logger.debug("set key [{}]", key); try { redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); return value; } catch (Throwable t) { logger.error("set key [{}] exception!", key, t); throw new CacheException(t); } } public void delete(String key) { logger.debug("delete key [{}]", key); try { redisTemplate.delete(key); } catch (Throwable t) { logger.error("delete key [{}] exception!", key, t); throw new CacheException(t); } } @SuppressWarnings("unchecked") public void setSet(String k, T value, long time) { String key = KEY_SET_PREFIX + k; logger.debug("setSet key [{}]", key); try { SetOperations<String, T> valueOps = redisTemplate.opsForSet(); valueOps.add(key, value); if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS); } catch (Throwable t) { logger.error("setSet key [{}] exception!", key, t); throw new CacheException(t); } } public void setSet(String k, T value) { setSet(k, value, -1); } @SuppressWarnings("unchecked") public void setSet(String k, Set<T> v, long time) { String key = KEY_SET_PREFIX + k; logger.debug("setSet key [{}]", key); try { SetOperations<String, T> setOps = redisTemplate.opsForSet(); setOps.add(key, (T[]) v.toArray()); if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS); } catch (Throwable t) { logger.error("setSet key [{}] exception!", key, t); throw new CacheException(t); } } public void setSet(String k, Set<T> v) { setSet(k, v, -1); } public Set<T> getSet(String k) { String key = KEY_SET_PREFIX + k; logger.debug("getSet key [{}]", key); try { SetOperations<String, T> setOps = redisTemplate.opsForSet(); return setOps.members(key); } catch (Throwable t) { logger.error("getSet key [{}] exception!", key, t); throw new CacheException(t); } } public void setList(String k, T v, long time) { String key = KEY_LIST_PREFIX + k; logger.debug("setList key [{}]", key); try { ListOperations<String, T> listOps = redisTemplate.opsForList(); listOps.rightPush(key, v); if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS); } catch (Throwable t) { logger.error("setList key [{}] exception!", key, t); throw new CacheException(t); } } public void setList(String k, List<T> v, long time) { String key = KEY_LIST_PREFIX + k; logger.debug("setList key [{}]", key); try { ListOperations<String, T> listOps = redisTemplate.opsForList(); listOps.rightPushAll(key, v); if (time > 0) redisTemplate.expire(key, time, TimeUnit.SECONDS); } catch (Throwable t) { logger.error("setList key [{}] exception!", key, t); throw new CacheException(t); } } public void setList(String k, List<T> v) { setList(k, v, -1); } public List<T> getList(String k, long start, long end) { String key = KEY_LIST_PREFIX + k; logger.debug("setList key [{}]", key); try { ListOperations<String, T> listOps = redisTemplate.opsForList(); return listOps.range(key, start, end); } catch (Throwable t) { logger.error("getList key [{}] exception!", key, t); throw new CacheException(t); } } public long getListSize(String k) { String key = KEY_LIST_PREFIX + k; logger.debug("setList key [{}]", key); try { ListOperations<String, T> listOps = redisTemplate.opsForList(); return listOps.size(key); } catch (Throwable t) { logger.error("getListSize key [{}] exception!", key, t); throw new CacheException(t); } } public long getListSize(ListOperations<String, String> listOps, String k) { String key = KEY_LIST_PREFIX + k; logger.debug("getListSize key [{}]", key); try { return listOps.size(key); } catch (Throwable t) { logger.error("getListSize key [{}] exception!", key, t); throw new CacheException(t); } } public void setMap(String key, String mapkey, T mapValue) { HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash(); hashOperations.putIfAbsent(key, mapkey, mapValue); } public void deleteMap(String key, String mapkey) { HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash(); hashOperations.delete(key, mapkey); } public T getMap(String key, String mapkey) { HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash(); return hashOperations.get(key, mapkey); } public List<T> getMapValues(String key) { HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash(); return hashOperations.values(key); } public void setRedisTemplate(RedisTemplate<String, T> redisTemplate) { this.redisTemplate = redisTemplate; } }