攔截器與過濾器

來源:互聯網
上載者:User

標籤:put   tips   adapter   class   intercept   service   deposit   gets   val   

 springMVC.xml

<mvc:interceptors>        <!-- 多個攔截器,順序執行,不需要登陸的介面都需要在此配置!!!!!!!!! -->        <mvc:interceptor>            <!-- <mvc:mapping path="/front/**" /> --><!-- 如果不配置或/*,將攔截所有的Controller -->            <mvc:mapping path="/**"/>            <bean name="titleInterceptor" class="cn.zsmy.interceptor.TitleInterceptor">                <!-- 不攔截的url -->                <property name="uncheckUrls">                      <list>                         <value>/login</value>                         <value>/doLogin</value>                         <value>/mobile2/login.json</value>                         <value>/mobile2/register.json</value>                         <value>/mobile2/regCheckCode.json</value>                         <value>/mobile2/register_sms.json</value>                         <value>/mobile2/reset_pwd_sms.json</value>                         <value>/mobile2/editpassword.json</value>                         <value>/mobile2/resetpwd_check_code.json</value>                         <value>/mobile2/version/check_version.json</value>                         <value>/mobile2/home_tips.json</value>                         <value>/mobile2/sys/uploadlog.json</value>                         <value>/mobile2/activity/list_recomme.json</value>                        <value>/mobile2/medicine/medicines.json</value>                         <value>/mobile2/medicine/list.json</value>                         <value>/mobile2/inspectionItem/inspection_item_detail.json</value>                         <value>/mobile2/symptom/symptoms.json</value>                         <value>/mobile2/deptmedicine/dept_medicine.json</value>                         <value>/mobile2/symptommedicine/symptom_medicine.json</value>                         <value>/mobile2/inspectionItem/inspection_items.json</value>                         <value>/mobile2/idict/professional_title.json</value>                         <!-- <value>/mobile2/area/getProvinceList.json</value>  -->                        <value>/mobile2/area/getCityList.json</value>                         <value>/mobile2/activity/list.json</value>                         <value>/mobile2/activity/deposits.json</value>                         <value>/mobile2/forum_plate/list.json</value>                       </list>                </property>             </bean>        </mvc:interceptor>    </mvc:interceptors>

 

TitleInterceptor.java

import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import cn.zsmy.constant.Constant;import cn.zsmy.utils.GetIP;public class TitleInterceptor extends HandlerInterceptorAdapter {        /*@Autowired    private UserService userService;*/    private List<String> uncheckUrls; //免登入 免檢查地址         public List<String> getUncheckUrls() {        return uncheckUrls;    }    public void setUncheckUrls(List<String> uncheckUrls) {        this.uncheckUrls = uncheckUrls;    }        /*@Autowired     public RedisUtil redisUtil;*/          @Override    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {         //未配置單裝置登入控制或這配置為1,就攔截         String requestUrl = request.getRequestURI();          Constant.MY_LOG.info("===> url:" + requestUrl+"=====請求ip" + GetIP.getIpAddr(request));                 //介面訪問頻率檢查限制        /* String key = "req_limit_".concat(requestUrl).concat(request.getLocalAddr());         long count = redisUtil.increment(key, 1);         if (count == 1) {             redisUtil.expire(key, 3, TimeUnit.SECONDS);         }         if (count > 5) {             Constant.MY_LOG.debug("超過了限定的次數[" + 5 + "]");             throw new BusinessException("req_limit", "指定時間內請求超過了限定的次數");             //throw new BusinessException(ReturnCode.Other.OTHER_DEVICE_LOGIN, ReturnCode.Other.OTHER_DEVICE_LOGIN_MSG);         }*/                  //單一裝置登入檢查控制        /*if(StringUtils.isEmpty(DictInit.dictMap.get(Constant.Dict.SINGLE_DEVICE_LOGIN)) || "1".equals(DictInit.dictMap.get(Constant.Dict.SINGLE_DEVICE_LOGIN))){            if(uncheckUrls.contains(requestUrl)) {                 return true;             } else {                 Subject subject = SecurityUtils.getSubject();                User account = (User) subject.getPrincipal();                if(account != null){                    User user = userService.findById(account.getId());                    if(user != null){                        String sessionId = getCookieByName(request, "JSESSIONID").getValue();                        if(!StringUtil.isEmpty(user.getSessionId()) && !StringUtil.isEmpty(sessionId) && !user.getSessionId().equals(sessionId)){                            throw new BusinessException(ReturnCode.Other.OTHER_DEVICE_LOGIN, ReturnCode.Other.OTHER_DEVICE_LOGIN_MSG);                        }                    }                }            }        }*/        return true;    }    @Override    public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {        Constant.MY_LOG.debug("===> HandlerInterceptor postHandle");    }    @Override    public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object handler, Exception ex) throws Exception {        Constant.MY_LOG.debug("===> HandlerInterceptor afterCompletion");    }    /**     * 根據名字擷取cookie     * @param request     * @param name cookie名字     * @return     */    public static Cookie getCookieByName(HttpServletRequest request,String name){        Map<String,Cookie> cookieMap = ReadCookieMap(request);        if(cookieMap.containsKey(name)){            Cookie cookie = (Cookie)cookieMap.get(name);            return cookie;        }else{            return null;        }       }        /**     * 將cookie封裝到Map裡面     * @param request     * @return     */    private static Map<String,Cookie> ReadCookieMap(HttpServletRequest request){          Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();        Cookie[] cookies = request.getCookies();        if(null!=cookies){            for(Cookie cookie : cookies){                cookieMap.put(cookie.getName(), cookie);            }        }        return cookieMap;    }    }

 

攔截器與過濾器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.