java - Spring Security 应用程序中自定义错误页面未由 Sitemesh 修饰

标签 java jsp spring-mvc spring-security sitemesh

在带有 Spring Security (3.2.0.RC2) 和 Sitemesh (2.4.2) 的 Spring MVC (3.2.4) 应用程序中,web.xml 文件具有以下条目:

<error-page>
    <error-code>403</error-code>
    <location>/error?code=403</location>
</error-page>

映射到ErrorController:

@RequestMapping("error")
public String displayErrorPage(
    @RequestParam(value = "code", defaultValue = "0") int code,
    Model model, final HttpServletRequest request, Principal principal) {
    // ...
    return "errorPage";
}

它通过InternalResourceViewResolver显示errorPage.jsp(应用程序中没有其他 View 解析器)。

安全性工作正常,当未经授权的用户尝试访问 protected 页面时,会显示 errorPage.jsp,但该页面未经过修饰。应用程序中的所有其他页面都经过修饰,没有任何问题,并且 errorPage.jsp 与其他经过修饰的 JSP 位于同一目录中,没有任何问题。此应用程序使用 Servlet 3.0 规范。

最佳答案

这似乎是一个 Sitemesh 错误(请参阅: http://forum.spring.io/forum/spring-projects/security/37742-sitemesh-decoration-problem ),可以通过重定向解决。由于各种原因,我不想从 JSP 页面内进行重定向,因此我更改了 Controller :

@RequestMapping("error")
    public String displayErrorPage(
    @RequestParam(value = "code", defaultValue = "0") int code,
    RedirectAttributes redirectAttributes, final HttpServletRequest request,
    Principal principal) {
    // ...
    redirectAttributes.addFlashAttribute("myAttribute", myAttribute);
    return "redirect:/displayError";
}

@RequestMapping("displayError")
public String displayError() {
    return "errorPage";
}

关于java - Spring Security 应用程序中自定义错误页面未由 Sitemesh 修饰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19907706/

相关文章:

java - 如何从 ExternalContext 获取 webapp 的绝对 URL?

java - Swing:自定义组件上的监听器执行顺序

java - 如何将字符串中出现的每个特殊字符后的小写字符转换为大写字符?

java - 使用 Eclipse 创建 Servlet 后,Tomcat 7 服务器无法启动

java - 空白index.jsp重定向到某个servlet

java - 将 spring boot 应用程序拆分为多个 Web 应用程序

java - 可滑动的线性/相对布局(第 2 部分)

java - request.getMethod().equalsIgnoreCase ("post") 在 (pre-Spring-3.0) Spring 博客应用程序中是个坏主意吗?

java - <form :input > to map with object attribute in spring 中路径的动态值