2011-11-22 36 views
2

如何當用戶不能看到一個頁面(在沒有特定的權限)獲得HTTP 500後顯示自定義錯誤頁 -自定義錯誤頁+春季安全3 +調用hasPermission

org.springframework.security.access .AccessDeniedException:訪問被拒絕

自定義hasPermission()方法的異常。

您可以查看我以前的問題 here以查看我的代碼。

回答

3

在你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

我的建議是不要把所有的異常,因爲原始一個更好的創造自己的異常包裝並在他們的包裹這些例外它會幫助你組織你的代碼以更好的方式

希望這會幫助你。

+0

感謝它爲我工作。 –

+0

歡迎您:) –