zoukankan      html  css  js  c++  java
  • springmvc添加拦截器

    springmvc.xml配置如下:

    除了 sysFile 下的所有接口,以及user下的loginUser接口,其他的所有接口都会经过拦截器UserInterceptor处理

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/sysFile/**" />
            <mvc:exclude-mapping path="/users/loginUser.do" />
            <bean class="com.test.interceptor.UserInterceptor" />
         </mvc:interceptor>
    </mvc:interceptors>
    public class UserInterceptor extends HandlerInterceptorAdapter{
        
        @Autowired
        private IUsersService usersService;
        
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            
            try {
                request.setCharacterEncoding("UTF-8");
            } catch (UnsupportedEncodingException e) {
                return false;
            }
         //获取前台传入userId String userId
    = request.getParameter("userId");
         //获取前台传入的 token String token
    = request.getParameter("token");
         //根据userId从数据库获取user信息 UsersVo usersVo
    = usersService.findUserById(userId); if(usersVo == null){ return false;//拦截 }
         //判断前台传入的token 与 数据获取的token做比较
    if(usersVo.getToken() == null || "".equals(usersVo.getToken()) || !usersVo.getToken().equals(token)){ return false;//拦截 }
         //不拦截
    return true; } }
  • 相关阅读:
    预览上传
    使用 Vagrant 打造跨平台开发环境fffff
    使用 Vagrant 打造跨平台开发环境
    弱智python小游戏猜数字
    Linux设置固定IP
    call_user_func
    mongodb 下载安装 转
    chrome浏览器下的xdebug helper使用方法
    类似NL的update更新
    如何启用并行?
  • 原文地址:https://www.cnblogs.com/hjw-zq/p/8971527.html
Copyright © 2011-2022 走看看