SpringMVC拦截器盗链接

Posted Taboos_

tags:

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

编译工具:IntelliJ IDEA 2019.3.5

用到的jar包链接

https://download.csdn.net/download/Taboos_/19465556?spm=1001.2014.3001.5503

用到的配置文件

spring_config.xml

 代码---------------------------------------------------------------

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="cn.hp"></context:component-scan>

    <mvc:annotation-driven></mvc:annotation-driven>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--配置一个全局拦截器 拦截所有请求-->
    <mvc:interceptors>
        <!--        <bean id="testInterceptor1" class="cn.hp.interceptor.TestInterceptor1"></bean>-->

        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <bean id="loginInterceptor" class="cn.hp.interceptor.LoginInterceptor"></bean>
            <!--            <bean id="testInterceptor1" class="cn.hp.interceptor.TestInterceptor1"></bean>-->
        </mvc:interceptor>

        <!--        <mvc:interceptor>-->
        <!--            <mvc:mapping path="/test1.do"/>-->
        <!--            <mvc:exclude-mapping path="/test2.do"/>-->
        <!--            <bean id="testInterceptor2" class="cn.hp.interceptor.TestInterceptor2"></bean>-->
        <!--        </mvc:interceptor>-->


    </mvc:interceptors>



    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.hp.dao"></property>
    </bean>





</beans>

-------------------------------------------------------------------------------

web.xml

  代码----------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">


    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 初始化时加载springmvc配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:*.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>fileEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>Encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>fileEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

--------------------------------------------------------------------------------

action层

   代码----------------------------------------------------------------------------

package cn.hp.action;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserAction 
    @RequestMapping(value = "test1.do")
    public String test1()
        System.out.println("正在执行test1 这个的业务逻辑");
        return "index";
    
    @RequestMapping(value = "test2.do")
    public String test2()
        System.out.println("正在执行test2 这个的业务逻辑");
        return "index";
    


    @RequestMapping(value = "login.do")
    public String login(String username, String pwd, Model model, HttpSession session)
        if (username.equals("zsl")&&pwd.equals("123456"))
            session.setAttribute("user",username);
            return "redirect:/main.do";
        else 
            model.addAttribute("msg","用户名密码错误");
            return "login";
        

    
    @RequestMapping(value = "main.do")
    public String main()

        return "main";
    
    @RequestMapping(value = "loginout.do")
    public String loginout(HttpSession session)

        session.invalidate();
        return "login";
    

--------------------------------------------------------------------------------

interceptor层

Testinterceptor1

    代码----------------------------------------------------------------------------

package cn.hp.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestInterceptor1 implements HandlerInterceptor 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception 

        System.out.println("拦截请求之后 业务逻辑之前执行此方法");
        return true;
    

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception 

        System.out.println("业务逻辑之后 视图渲染之前");
    

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception 

        System.out.println("视图渲染之后执行");
    

----------------------------------------------------------------------------

Testinterceptor2

     代码----------------------------------------------------------------------------

package cn.hp.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestInterceptor2 implements HandlerInterceptor 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception 
        System.out.println("拦截2请求之后 业务逻辑之前执行此方法");
        return true;
    

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception 
        System.out.println("业务逻辑2之后 视图渲染之前");
    

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception 
        System.out.println("视图渲染2之后执行");
    

----------------------------------------------------------------------------

盗链接

      代码----------------------------------------------------------------------------

    @RequestMapping(value = "login.do")
    public String login(String username, String pwd, Model model, HttpSession session)
        if (username.equals("zsl")&&pwd.equals("123456"))
            session.setAttribute("user",username);
            return "redirect:/main.do";
        else 
            model.addAttribute("msg","用户名密码错误");
            return "login";
        

    
    @RequestMapping(value = "main.do")
    public String main()

        return "main";
    
    @RequestMapping(value = "loginout.do")
    public String loginout(HttpSession session)

        session.invalidate();
        return "login";
    

----------------------------------------------------------------------------

LoginInterceptor

       代码----------------------------------------------------------------------------

package cn.hp.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception 
        String path = request.getRequestURI();
        if (path.indexOf("login.do")>0)
            return true;
        
        Object obj1 = request.getSession().getAttribute("user");
        if (obj1!=null)

            return true;
        else 
            request.setAttribute("msg","去登陆");
            request.getRequestDispatcher("login.jsp").forward(request,response);

            return false;
        



    

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception 

    

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception 

    

----------------------------------------------------------------------------

前端jsp页面

 

 

 

以上是关于SpringMVC拦截器盗链接的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVC拦截器盗链接

springMVC盗链接

springMVC盗链接

SpringMVC 拦截器(interceptors)对样式(css),JavaScript(js),图片(images)链接的拦截

css拦截器,SpringMVC 拦截器(interceptors)对样式(css),JavaScript(js),图片(images)链接的拦截...

面试问烂的 Spring AOP 原理SpringMVC 过程