spring - 自定义错误页面 + spring security 3 + hasPermission

标签 spring struts2 spring-security

当用户在获得 HTTP 500 后无法看到页面(当没有特定权限时)时如何显示自定义错误页面 -

org.springframework.security.access.AccessDeniedException:访问被拒绝

自定义 hasPermission() 方法的异常。

您可以查看我之前的问题 here 查看我的代码。

最佳答案

在您的 struts2 配置文件中,您可以配置全局异常,例如

<global-exception-mappings>
    <exception-mapping exception="org.springframework.security.access.AccessDeniedException" result="securityerror" />
     <exception-mapping exception="java.lang.Exception" result="error" />
   </global-exception-mappings>

  <global-results>
        <result name="securityerror">/securityerror.jsp</result>
    <result name="error">/error.jsp</result>
   </global-results>

这将适用于应用程序级别,如果您发现细粒度的异常处理内容,您可以为操作级别本身定义它
<action name="actionspecificexception" class="org.apache.struts.register.action.Register" method="throwSecurityException">
     <exception-mapping exception="org.springframework.security.access.AccessDeniedException" 
          result="login" />
      <result>/register.jsp</result>
      <result name="login">/login.jsp</result>
   </action>

所以这取决于您选择如何配置它。有关详细信息,请参阅。

Exception handling in Struts2

我的建议是不要将每个异常都作为原始异常抛出,更好地创建自己的异常包装器并将此类异常包装在其中,这将帮助您以更好的方式组织代码

希望这会帮助你。

关于spring - 自定义错误页面 + spring security 3 + hasPermission,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8225805/

相关文章:

spring - 排除 hibernate 创建的特定表?

java - Hibernate 关系映射不是双向的

java - 如何编写查询参数是列表的 JPARepository 查询?

java - struts2中的http post方法

java - 如何在 Struts2 中进行子字符串化?

grails - 未调用自定义UserDetailsS​​ervice-Grails和Spring Security Core

java - 如何构建与数据库通信的 Controller

jquery - 无法使用 jQuery 加载 JSON 上的完整数据

java - 如何从登录表单调用额外参数到 CustomAuthenticationProvider

java - Spring Security 中的最大并发用户数