• 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>
  • 相关阅读:
    shell笔记
    全引用与部分引用
    R语言 EFA(探索性因子分析)
    Markdown 基本使用
    Oracle截取字符串和查找字符串
    ggplot画基本图形类型
    ggplot画图笔记
    用R语言 画条形图(基于ggplot2包)
    Git基本操作
    R语言为数据框添加列名或行名
  • 原文地址:https://www.cnblogs.com/daxiongblog/p/7066924.html
Copyright © 2020-2023  润新知