getAuthentication() when error-page 404 is shown in the Spring.

in web.xml.  
<error-page>   
  <error-code>404</error-code>   
  <location>/WEB-INF/pages/errors/error404.jsp</location>    
</error-page

In error404.jsp, don't get user message by using SecurityContextHolder.getContext().getAuthentication().

Solution:

<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> 
        <dispatcher>REQUEST</dispatcher> 
        <dispatcher>ERROR</dispatcher> 
    </filter-mapping> 



The dispatcher has four legal values: FORWARD, REQUEST, INCLUDE, and ERROR. A value of FORWARD means the Filter will be appliedunder RequestDispatcher.forward() calls. A value of REQUEST means the Filter will be applied under ordinary client calls to the path or servlet. A value of INCLUDE means the Filter will be applied under RequestDispatcher.include() calls. A value of ERROR means the Filter will be applied under the error page mechanism. The absence of any dispatcher elements in a filter-mapping indicates a default of applying filters only under ordinary client calls to the path or servlet.

creating a helloworld from demo
1.jar
2.web.xml
3.xxx-servlet.xml in WEB-INF
4.Controller
5.view

1.Reference jar files
WEB-INF/lib/*.jar, this folder will be scanned and loaded by web application.
2.Setting web.xml, adding the servlet DispatcherServlet
Web.xml
    <servlet>
        <servlet-name>mydemo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>  // loaded by Tomcat
    </servlet>
   
    <servlet-mapping>
        <servlet-name>mydemo</servlet-name>
        <url-pattern>*.lt</url-pattern>       
    </servlet-mapping>

3. xxx-servlet.xml
3.1Creating xxx-servlet.xml, xxx is the name of the servlet DispatcherServlet.
WEB-INF/mydemo-servlet.xml
mydemo-servlet.xml default is for mydemo servlet.

. In xxx-servlet.xml
 3.2 for loading controller
<context:component-scan base-package="com.mydemo.controller"/>
3.3 for view
<bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/views/"></property>
          <property name="suffix" value=".jsp"></property>       
 </bean>

4.Controller class
4.1 Creating Controller 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;   
@Controller
@RequestMapping(value="test")
public class TestController{
  @RequestMapping("one")
  public String test(string userName,Model model){
    model.addatribute("msg",userName);
    return "test";
  }
}
4.2 Calling Controller from a page at the browser.
<from action="test/one.lt"> <input type="text" name="userName">
<input type="submit" value="submit">
</from>

5./WEB-INF/views/test.jsp
 ${msg}

6. The "get" method encode in Tomcat  server.xml
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

    <servlet>
        <servlet-name>mydemo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/mydemo/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
   

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE