Announcement Announcement Module
Collapse
No announcement yet.
Problems redirecting to error pages on error Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems redirecting to error pages on error

    Hi,

    I'm trying to use Spring MVC and Spring Security to implement my site and would like to direct all errors--in both servlets and JSP page code--to a single simple error page. To test this I have a simple controller which deliberately throws an exception in

    its handleRequestInternal method. A SimpleMappingExceptionResolver forwards an java.lang.Exceptions to a custom error page. Everything works fine until... I try to put a custom login page in front of it.

    That is, I go to my servlet which is mapped to the root http://localhost/, I'm directed to my custom login.jsp page at http://localhost/login.jsp;jsessioni...A09F50258F8068, login correctly and end up getting a 404 error instead of my custom error page (with http://localhost/favicon.ico in the address bar).

    It's strange because the error page mapping works by itself, since without the login it takes me to the custom error page. The login also works by itself because when I remove the thrown exception I can log in and go to the page just fine. When I add the two together though the wires get crossed somehow.

    Some code is below:

    WEB-INF/web.xml
    Code:
    <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">
      <display-name>Test Website</display-name>
      <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
      </listener>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          /WEB-INF/test-context.xml
          /WEB-INF/test-security.xml
        </param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
      </listener>
      <servlet>
        <servlet-name>main</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>main</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>
    WEB-INF/test-servlet.xml
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="mainController" class="com.midsee.web.controller.MainController" />
        
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/">mainController</prop>
                </props>
            </property>
        </bean>
        
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
                    <prop key="java.lang.Exception">error</prop>
                </props>
            </property>
        </bean>
    </beans>
    WEB-INF/test-context.xml
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    
        
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
        
    </beans>
    
    WEB-INF/test-security.xml
    <beans:beans xmlns="http://www.springframework.org/schema/security"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation=" http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                http://www.springframework.org/schema/security 
                http://www.springframework.org/schema/security/spring-security-3.0.xsd">
        
        <http auto-config="true">
            <intercept-url pattern="/login.jsp" filters="none" />
            <intercept-url pattern="/" access="ROLE_USER" />
            <intercept-url pattern="/**" access="ROLE_USER" />
            <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" />
            <logout logout-success-url="/logout.jsp" />
        </http>
    
        <authentication-manager>
            <authentication-provider>
            <user-service>
                <user name="user" password="password" authorities="ROLE_USER" />
            </user-service>
            </authentication-provider>
        </authentication-manager>
    </beans:beans>
    /src/main/java/com/test/controller/MainController.java
    Code:
    package com.test.controller;
    
    import java.util.Date;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;
    
    public class MainController extends AbstractController {
    
        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest request,
    	    HttpServletResponse response) throws Exception {
    	if(true) throw new Exception("something went wrong.");
    	return new ModelAndView("main", "today", new Date());
        }
    }
    /login.jsp
    HTML Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>    
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Login</title>
    </head>
    
    <body>
        <c:if test="${not empty param.error}">
            <font color="red">
                Login error.<br/>
                Reason : ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
            </font>
        </c:if>
        <form method="POST" action="<c:url value="/j_spring_security_check" />">
        <table>
            <tr>
                <td align="right">Username</td>
                <td><input type="text" name="j_username" /></td>
            </tr>
            <tr>
                <td align="right">Password</td>
                <td><input type="password" name="j_password" /></td>
            </tr>
            <tr>
                <td align="right">Remember Me</td>
                <td><input type="checkbox" name="_spring_security_remember_me" /></td>
            </tr>
            <tr>
                <td colspan="2" align="right">
                    <input type="submit" value="Login" />
                    <input type="reset" value="Reset" />
                </td>
            </tr>
        </table>
        </form>
    </body>
    </html>
    WEB-INF/jsp/main.jsp
    HTML Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Main</title>
    </head>
    <body>
    Today is <fmt:formatDate value="${today}" pattern="MM-dd-yyyy" />. <a href="<c:url value="/j_spring_security_logout"/>">Logout</a>
    </body>
    </html>
    WEB-INF/jsp/error.jsp
    HTML Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Error</title>
    </head>
    <body>
    Error
    </body>
    </html>
    Any help or alternative method to solving this problem would be greatly appreciated.

    Thanks,
    Moshe

  • #2
    I'm not sure if there is a better way to solve the problem but I was able to at least work around it with the following in my security configuration:

    <http auto-config="true">
    ...
    <intercept-url pattern="/favicon.ico" filters="none" />
    ...
    </http>

    Comment

    Working...
    X