Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringMVC中,如何在servlet过滤器中的请求到达登录jsp之前首先拦截该请求?_Java_Jsp_Spring Mvc_Servlets_Servlet Filters - Fatal编程技术网

Java 在SpringMVC中,如何在servlet过滤器中的请求到达登录jsp之前首先拦截该请求?

Java 在SpringMVC中,如何在servlet过滤器中的请求到达登录jsp之前首先拦截该请求?,java,jsp,spring-mvc,servlets,servlet-filters,Java,Jsp,Spring Mvc,Servlets,Servlet Filters,我正在接管一个现有的Spring MVC项目,需要修改它的行为,我添加了一个servlet过滤器来拦截所有传入的请求,该应用程序的一些URL具有以下格式: 我想在其他任何事情发生之前获取令牌值,但由于此应用程序有一个安全登录页面,如果您未登录,并且您尝试访问上述URL,它将引导您访问登录页面: 因此,我将无法截获传入请求url[]和令牌值,我的应用程序的web.xml如下所示: <?xml version="1.0" encoding="UTF-8"?> <web-app ve

我正在接管一个现有的Spring MVC项目,需要修改它的行为,我添加了一个servlet过滤器来拦截所有传入的请求,该应用程序的一些URL具有以下格式:

我想在其他任何事情发生之前获取令牌值,但由于此应用程序有一个安全登录页面,如果您未登录,并且您尝试访问上述URL,它将引导您访问登录页面:

因此,我将无法截获传入请求url[]和令牌值,我的应用程序的web.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>
 <filter>
  <filter-name>Site_Filter</filter-name>
  <filter-class>com.builders.support.center.ServletFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>Site_Filter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <servlet>
  <servlet-name>supportCenter</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>supportCenter</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>

 <servlet-mapping>
  <servlet-name>supportCenter</servlet-name>
  <url-pattern>/login</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>supportCenter</servlet-name>
  <url-pattern>/newlogin</url-pattern>
 </servlet-mapping>
 ...
 <login-config>
  <auth-method>FORM</auth-method>
  <form-login-config>
   <form-login-page>/login.html</form-login-page>
   <form-error-page>/fail_login.html</form-error-page>
  </form-login-config>
 </login-config>
 <security-role>
  <role-name>1</role-name>
 </security-role>
 <security-role>
  <role-name>4</role-name>
 </security-role>
<?xml version="1.0" encoding="UTF-8"?>
    <!--    - Application context definition for "SupportCenter" DispatcherServlet. -->
<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-2.5.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="com.XYZ.support.center.weblayer.springmvc.controllers"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <bean id="supportCenterPropertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>/WEB-INF/properties/web-site.properties</value>
        </property>
    </bean>

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

    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/main.html">mainFormController</prop>

                <prop key="/login">mainFormController</prop>
                <prop key="/newlogin">mainFormController</prop>
                <prop key="/fail_login.html">failLoginViewController</prop>
                <prop key="/login.html">loginFormController</prop>

                <prop key="/changePassword.html">changePasswordFormController</prop>
                <prop key="/forgotUsername.html">forgotUsernameFormController</prop>
                <prop key="/forgotPassword.html">forgotPasswordFormController</prop>
...
            </props>
        </property>
    </bean>
...
    <bean id="mainFormController"
          class="com.XYZ.support.center.weblayer.springmvc.controllers.MainFormController">
        <property name="sessionForm">
            <value>true</value>
        </property>
        <property name="commandName">
            <value>commandBean</value>
        </property>
        <property name="commandClass">
            <value>com.XYZ.support.center.weblayer.springmvc.command.beans.MainCommandBean</value>
        </property>
        <property name="formView">
            <value>main</value>
        </property>
        <property name="successView">
            <value>main.html</value>
        </property>
        <property name="bindOnNewForm">
            <value>true</value>
        </property>
        <property name="equotesLink">
            <value>${equotes.link}</value>
        </property>
        <property name="hotchkissAgencyBillPayLink">
            <value>${payment.pay.site.hotchkiss}</value>
        </property>
    </bean>
...
    <bean id="loginFormController"
          class="com.XYZ.support.center.weblayer.springmvc.controllers.LoginFormController">
        <property name="sessionForm">
            <value>true</value>
        </property>
        <property name="commandName">
            <value>commandBean</value>
        </property>
        <property name="commandClass">
            <value>com.XYZ.support.center.weblayer.springmvc.command.beans.LoginCommandBean</value>
        </property>
        <property name="formView">
            <value>login</value>
       </property>
        <property name="successView">
            <value>j_security_check</value>
        </property>
        <property name="bindOnNewForm">
            <value>true</value>
        </property>
    </bean>
...
</beans>
public class ServletFilter implements Filter
{
  String requestURI="",url="",queryString="",Token="";

  public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain) throws IOException,ServletException
  {
    if (servletRequest instanceof HttpServletRequest)
    {
      requestURI=(HttpServletRequest)servletRequest.getRequestURI();
      url=((HttpServletRequest)servletRequest).getRequestURL().toString();
      queryString=((HttpServletRequest)servletRequest).getQueryString();
    }
    System.out.println("  [ Intercepted In ServletFilter = 0 ]  requestURI = "+requestURI+"  ,  url = "+url+"  ,  queryString = "+queryString);
...
   }
}
Aug 13, 2014 6:26:05 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 54325 ms
  [ JSP = 1 ] 1407968732269
  [ JSP = 2 ] 1407968732677
  [ Intercepted In ServletFilter = 0 ]  requestURI = /SupportCenter/styles/loginForm.css  ,  url = http://10.xx.5.2/SupportCenter/styles/loginForm.css  ,  queryString = null
  [ Intercepted In ServletFilter = 1 ]  Token = null  ,  url = http://10.xx.5.2/SupportCenter/styles/loginForm.css
  [ queryString = null ]
  [ Intercepted In ServletFilter = 0 ]  requestURI = /SupportCenter/javascript/login.js  ,  url = http://10.xx.5.2/SupportCenter/javascript/login.js  ,  queryString = null
  [ Intercepted In ServletFilter = 1 ]  Token = null  ,  url = http://10.xx.5.2/SupportCenter/javascript/login.js
  [ queryString = null ]
  [ Intercepted In ServletFilter = 0 ]  requestURI = /SupportCenter/styles/header_ms.css  ,  url = http://10.xx.5.2/SupportCenter/styles/header_ms.css  ,  queryString = null
  [ Intercepted In ServletFilter = 1 ]  Token = null  ,  url = http://10.xx.5.2/SupportCenter/styles/header_ms.css
  [ queryString = null ]
尝试达到[/SupportCenter/abc.html?token=some\u token\u value]的输出如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>
 <filter>
  <filter-name>Site_Filter</filter-name>
  <filter-class>com.builders.support.center.ServletFilter</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>Site_Filter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <servlet>
  <servlet-name>supportCenter</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>supportCenter</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>

 <servlet-mapping>
  <servlet-name>supportCenter</servlet-name>
  <url-pattern>/login</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>supportCenter</servlet-name>
  <url-pattern>/newlogin</url-pattern>
 </servlet-mapping>
 ...
 <login-config>
  <auth-method>FORM</auth-method>
  <form-login-config>
   <form-login-page>/login.html</form-login-page>
   <form-error-page>/fail_login.html</form-error-page>
  </form-login-config>
 </login-config>
 <security-role>
  <role-name>1</role-name>
 </security-role>
 <security-role>
  <role-name>4</role-name>
 </security-role>
<?xml version="1.0" encoding="UTF-8"?>
    <!--    - Application context definition for "SupportCenter" DispatcherServlet. -->
<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-2.5.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:component-scan base-package="com.XYZ.support.center.weblayer.springmvc.controllers"/>

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <bean id="supportCenterPropertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>/WEB-INF/properties/web-site.properties</value>
        </property>
    </bean>

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

    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/main.html">mainFormController</prop>

                <prop key="/login">mainFormController</prop>
                <prop key="/newlogin">mainFormController</prop>
                <prop key="/fail_login.html">failLoginViewController</prop>
                <prop key="/login.html">loginFormController</prop>

                <prop key="/changePassword.html">changePasswordFormController</prop>
                <prop key="/forgotUsername.html">forgotUsernameFormController</prop>
                <prop key="/forgotPassword.html">forgotPasswordFormController</prop>
...
            </props>
        </property>
    </bean>
...
    <bean id="mainFormController"
          class="com.XYZ.support.center.weblayer.springmvc.controllers.MainFormController">
        <property name="sessionForm">
            <value>true</value>
        </property>
        <property name="commandName">
            <value>commandBean</value>
        </property>
        <property name="commandClass">
            <value>com.XYZ.support.center.weblayer.springmvc.command.beans.MainCommandBean</value>
        </property>
        <property name="formView">
            <value>main</value>
        </property>
        <property name="successView">
            <value>main.html</value>
        </property>
        <property name="bindOnNewForm">
            <value>true</value>
        </property>
        <property name="equotesLink">
            <value>${equotes.link}</value>
        </property>
        <property name="hotchkissAgencyBillPayLink">
            <value>${payment.pay.site.hotchkiss}</value>
        </property>
    </bean>
...
    <bean id="loginFormController"
          class="com.XYZ.support.center.weblayer.springmvc.controllers.LoginFormController">
        <property name="sessionForm">
            <value>true</value>
        </property>
        <property name="commandName">
            <value>commandBean</value>
        </property>
        <property name="commandClass">
            <value>com.XYZ.support.center.weblayer.springmvc.command.beans.LoginCommandBean</value>
        </property>
        <property name="formView">
            <value>login</value>
       </property>
        <property name="successView">
            <value>j_security_check</value>
        </property>
        <property name="bindOnNewForm">
            <value>true</value>
        </property>
    </bean>
...
</beans>
public class ServletFilter implements Filter
{
  String requestURI="",url="",queryString="",Token="";

  public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain) throws IOException,ServletException
  {
    if (servletRequest instanceof HttpServletRequest)
    {
      requestURI=(HttpServletRequest)servletRequest.getRequestURI();
      url=((HttpServletRequest)servletRequest).getRequestURL().toString();
      queryString=((HttpServletRequest)servletRequest).getQueryString();
    }
    System.out.println("  [ Intercepted In ServletFilter = 0 ]  requestURI = "+requestURI+"  ,  url = "+url+"  ,  queryString = "+queryString);
...
   }
}
Aug 13, 2014 6:26:05 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 54325 ms
  [ JSP = 1 ] 1407968732269
  [ JSP = 2 ] 1407968732677
  [ Intercepted In ServletFilter = 0 ]  requestURI = /SupportCenter/styles/loginForm.css  ,  url = http://10.xx.5.2/SupportCenter/styles/loginForm.css  ,  queryString = null
  [ Intercepted In ServletFilter = 1 ]  Token = null  ,  url = http://10.xx.5.2/SupportCenter/styles/loginForm.css
  [ queryString = null ]
  [ Intercepted In ServletFilter = 0 ]  requestURI = /SupportCenter/javascript/login.js  ,  url = http://10.xx.5.2/SupportCenter/javascript/login.js  ,  queryString = null
  [ Intercepted In ServletFilter = 1 ]  Token = null  ,  url = http://10.xx.5.2/SupportCenter/javascript/login.js
  [ queryString = null ]
  [ Intercepted In ServletFilter = 0 ]  requestURI = /SupportCenter/styles/header_ms.css  ,  url = http://10.xx.5.2/SupportCenter/styles/header_ms.css  ,  queryString = null
  [ Intercepted In ServletFilter = 1 ]  Token = null  ,  url = http://10.xx.5.2/SupportCenter/styles/header_ms.css
  [ queryString = null ]
如您所见,它首先到达JSP页面。我该怎么做才能让它首先命中servlet过滤器?即使它无法首先到达过滤器,当它重定向到login.jsp时,如何获取令牌值


我的第二个问题是:如何一起绕过login.jsp?

这里没有足够的代码/信息来回答您的问题。您需要自己做更多的工作来了解应用程序的工作方式、URL如何映射到各种控制器和/或JSP页面等,以及安全性是如何连接的(以便禁用登录等)。我认为这无助于发布SpringMVC应用程序的全部内容。一旦你把它缩小到一个更具体的问题,然后在这里再试一次。看起来这个过滤器只在JS/CSS文件上被触发,而不是在你的实际JSP页面上。。。过滤器应该总是在JSP或控制器代码之前被触发,所以我认为你说它“首先到达JSP页面”是错误的。。。它根本没有到达过滤器。