在Spring 3.0中将拦截器集成到Web请求 [英] Integrating interceptor to web requests in spring 3.0

查看:92
本文介绍了在Spring 3.0中将拦截器集成到Web请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在研究一个Web项目,并将spring 3.0与带有注释基础的控制器一起使用.

Currently I am working on a web project and using spring 3.0 with annotation bases controllers.

  1. 我正在尝试使用拦截进行登录.
  2. 只能使用拦截器从登录名中直接敲击URL.

我能够编写一个拦截器,该拦截器不会在没有提供登录所需参数的情况下让任何人进入网站.但是问题在于,其余页面也可以直接访问.

I am able to write an interceptor which won't let pass anyone to move into website without giving required parameters for login. But problem is that, remaining pages can also be accessed directly.

我正在与您共享我的servlet.xml

I am sharing with you my servlet.xml

我还想知道为什么必须定义URL映射才能使拦截器正常工作,否则它将陷入无限循环.

I am also wondering why I have to defien URL mappings to get interceptor working, otherwise it goes into a unlimited loop.

请帮助我解决此问题,如果您有满足此要求的可行示例,请也分享一下.

please help me out in solving this issue, if you have any working example for this requirement, then please share it too.

谢谢.

springmvc-servlet.xml

springmvc-servlet.xml

  <?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan
        base-package="com.waqas.app.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>



        <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
        <!--  -->   
            <entry key="contacts.htm"><ref bean="contactController" /></entry>
                    <entry key="users.htm"><ref bean="loginController" /></entry>

                            <entry key="hello.htm"><ref bean="helloController" /></entry>


            </map>
        </property>
    </bean>


    <bean name="contactController" class="com.waqas.app.controller.ContactController" />

        <bean name="helloController" class="com.waqas.app.controller.HelloWorldController" />


   <bean name="loginController" class="com.waqas.app.controller.LoginController" />



        <bean id="handlerMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="loginInterceptor"/>
            </list>
        </property>
          <property name="mappings">
            <value>

            *.htm=contactController

            </value>
        </property>

    </bean>



    <bean id="loginInterceptor"
          class="com.waqas.app.interceptor.LoginInterceptor">

    </bean>






</beans>

index.html

index.html

<%@ include file="/WEB-INF/jsp/include.jsp" %>
<jsp:forward page="contacts.htm"></jsp:forward>

我也想将此拦截器应用于单独的URL/URL模式.

i also want to apply this interceptor on sepcific URLs/url pattern.

期待您的答复.

亲切的问候, qasibeat

Kind Regards, qasibeat

推荐答案

这是一个非常普遍的问题. Scott Murphy创建了一个不错的spring插件,该插件可以根据URL在控制器上指定拦截器.您可以从 springplugins 下载.

This is quite a common problem. Scott Murphy has created a nice spring plugin that allows to specify interceptor on a controller based on URLs. You can download it from springplugins.

另请参见基于Spring框架注释的控制器拦截器配置

对于3.1+版本,请参阅我对问题控制对控制器的访问的答案根据URL模式配置拦截器.

For version 3.1+, see my answer to the question Control Access to Controllers to configure interceptor based on URL patterns.

这篇关于在Spring 3.0中将拦截器集成到Web请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆