1. 程式人生 > >Java登陸攔截器

Java登陸攔截器

gif 下載 drive ima direct source string see throws

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 */ @Component public 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登陸攔截器