Java登陆拦截器

Posted 大雄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java登陆拦截器相关的知识,希望对你有一定的参考价值。

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登陆拦截器的主要内容,如果未能解决你的问题,请参考以下文章

java ssh2登陆,session的问题

webserive学习记录5-拦截器完成登陆校验

JSP 设计教师与学生不同登陆界面(带验证码)

SpringBoot实现登陆拦截

用java实现一个简单的单用户登陆功能的思路

为什么你写的拦截器注入不了 Java bean?