2011-10-26 50 views
1

我需要知道如何覆蓋shiro未經授權的頁面。即當用戶對受保護的URL沒有某種權限時,shiro返回頁面401.我們如何使用戶轉向預先定義的未授權頁面?覆蓋shiro未經授權的頁面

謝謝...

回答

0

:)我需要的其他方式,我想獲得一個401而不是重定向(302)。你目前的設置是什麼?你能從shiro.ini文件配置這個嗎?

0

不確定您正在使用哪種技術堆棧,但是在Java Web應用程序中,您可以在標記中的web.xml中配置它。

<error-page> 
    <error-code>401</error-code> 
    <location>{path to custom page}</location> 
</error-page> 
3

我發現只有一條路,如何改變重定向到的login.jsp以響應與401

你應該創建自己的過濾器,這將延長org.apache.shiro.web.filter。 authc.FormAuthenticationFilter,並覆蓋saveRequestAndRedirectToLogin()方法。

public class SecurityAuthenticationFilter extends FormAuthenticationFilter { 

    @Override 
    protected void saveRequestAndRedirectToLogin(ServletRequest request, ServletResponse response) throws IOException { 
     saveRequest(request); 
     sendChallenge(response); 
    } 

    protected void sendChallenge(ServletResponse response) { 
     HttpServletResponse httpResponse = WebUtils.toHttp(response); 
     httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); 
    } 

} 

我使用Guice + Shiro集成。因此,此過濾器的添加方式與org.apache.shiro.guice.web.ShiroWebModule中的相同。

public class SecurityModule extends ShiroWebModule { 

    public static final Key<SecurityAuthenticationFilter> AUTHC_REST = Key.get(SecurityAuthenticationFilter.class); 

    ... 

    @Override 
    protected void configureShiroWeb() { 
     ... 

     // Add as filter 
     addFilterChain("/rest/login", ANON); 
     addFilterChain("/rest/**", AUTHC_REST); 

     ... 
    } 
} 

對於shiro.ini文件應該在以下方式添加:

[main] 
authc = package.path.to.SecurityAuthenticationFilter 

[urls] 
/rest/login = anon 
/rest/** = authc 

這應該工作:)

+0

感謝豬頭...這個解決我的問題。 +1 – Alexandre

相關問題