Java登陸攔截器

來源:互聯網
上載者:User

標籤:boolean   mod   dha   void   java   .json   eth   cep   equals   

package com.beidou.warehouseerp.interceptor;import com.alibaba.fastjson.JSON;import com.beidou.warehouseerp.dto.BaseResult;import com.beidou.warehouseerp.util.Const;import org.springframework.stereotype.Component;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.PrintWriter;/** * 許可權判斷攔截器 * * @author daxiong *         date: 2017/04/10 16:26 */@Componentpublic class SecurityInterceptor implements HandlerInterceptor {    /**     * 攔截前處理     */    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {        System.out.println("SecurityInterceptor preHandle:" + request.getContextPath() + "," + request.getRequestURI() + "," + request.getMethod());        HttpSession session = request.getSession();        if (session.getAttribute(Const.SESSION_USER) == null) {            System.out.println("AuthorizationException:未登入!" + request.getMethod());            if ("POST".equalsIgnoreCase(request.getMethod())) {                response.setContentType("application/json;charset=utf-8");                PrintWriter out = response.getWriter();                out.write(JSON.toJSONString(new BaseResult(false, "未登入!")));                out.flush();                out.close();            } else {                String xx = request.getContextPath();                response.sendRedirect(xx + "/login");            }            return false;        }        return true;    }    /**     * 攔截後處理     */    @Override    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {    }    /**     * 全部完成後處理     */    @Override    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {    }}

設定檔配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    <!-- 避免IE執行AJAX時,返回JSON出現下載檔案 -->    <bean id="mappingJacksonHttpMessageConverter"          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">        <property name="supportedMediaTypes">            <list>                <value>text/html;charset=UTF-8</value>            </list>        </property>    </bean>    <!-- 登入攔截器 -->    <mvc:interceptors>        <mvc:interceptor>            <mvc:mapping path="/*"/>  <!-- 攔截/  /test  /login  等等單層結構的請求  -->            <mvc:mapping path="/**/*.do"/><!-- 攔截尾碼為 .do的請求 -->            <mvc:mapping path="/**/*"/>            <mvc:exclude-mapping path="/login"/>            <mvc:exclude-mapping path="/logout"/>            <mvc:exclude-mapping path="/doLogin"/>            <mvc:exclude-mapping path="/resource/**"/>            <bean class="com.beidou.warehouseerp.interceptor.SecurityInterceptor">            </bean>        </mvc:interceptor>    </mvc:interceptors>    <!--配置springMVC-->    <!--1:開啟springMVC註解模式-->    <!--簡化配置    (1)自動註冊DefautAnnotationHandlerMapping,AnnotationMethodHandlerAdapter     (2) 提供一系列:資料繫結,數字和日期的format @NumberFormat,@DataTimeFormat,         xml,json預設讀寫支援    -->    <mvc:annotation-driven>        <mvc:message-converters register-defaults="false">            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>            <bean class="org.springframework.http.converter.FormHttpMessageConverter"/>            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                <property name="supportedMediaTypes">                    <list>                        <value>application/json;charset=utf-8</value>                    </list>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>    <!--<mvc:resources location="/image/" mapping="/image/**"/>-->    <!--2:靜態資源預設servlet配置           1:加入對靜態資源的處理:js,gif,png           2:允許使用"/"做整體映射    -->    <mvc:default-servlet-handler/>    <!-- 3.掃描包下所有使用註解的類型,相關的bean -->    <context:component-scan base-package="com.beidou.warehouseerp.web"/>    <!-- 4.配置SpringMVC的視圖解析器,顯示viewResolver -->    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->        <property name="prefix" value="/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>

 

Java登陸攔截器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.