2015-12-10 19 views
0

想法:我想用HTTP基本身份驗證保護Spring Web MVC的每個站點,但希望通過錯誤頁面重定向的40 *或500服務器錯誤/歡迎(當然用戶必須進行身份驗證,否則他會看到基本的身份驗證對話框)。保護在web.xml中彈簧安全和錯誤頁面的Spring MVC應用程序

問題:每次嘗試訪問該站點時,都會按預期彈出基本認證對話框。但是,當我取消對話,即取消取消,我登陸受保護的歡迎頁面[並查看所有重要/安全信息] - 沒有基本的身份驗證對話框!

樣品控制器:

@Controller 
public class WelcomeController 
{ 
    // Welcome is a protected site! 
    @RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET) 
    public ModelAndView welcome() 
    { 
     return new ModelAndView("welcome"); 
    } 
} 

安全配置:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter 
{ 
    @Override 
    protected void configure(HttpSecurity security) throws Exception 
    { 
     // Set the security settings 
     security.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().csrf(); 
    } 
} 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
    ... Snipped ... 
    <servlet-mapping> 
     <servlet-name>testapp</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <error-page> 
     <error-code>400</error-code> 
     <location>/welcome</location> 
    </error-page> 
    <error-page> 
     <error-code>401</error-code> 
     <location>/welcome</location> 
    </error-page> 
    <error-page> 
     <error-code>403</error-code> 
     <location>/welcome</location> 
    </error-page> 
    <error-page> 
     <error-code>404</error-code> 
     <location>/welcome</location> 
    </error-page> 
    <error-page> 
     <error-code>500</error-code> 
     <location>/welcome</location> 
    </error-page> 
</web-app> 
+0

你找到一個解決辦法? –

回答

1

我也必須實現爲客戶相同的設置。另外,我們在應用程序服務器前有一個Web應用程序防火牆。

我還不知道Tomcat如何忽略Spring的身份驗證檢查。重要的一點不是搞亂401,402和403 HTTP代碼(我從web.xml中刪除了他們的處理程序)

我最終得到了一個通用的錯誤頁面。這是可以討論的,如果你應該自己處理500個錯誤。如果你的系統拋出一個500錯誤,你可能無法處理它,因爲你的系統剛剛拋出了500錯誤,並且在錯誤處理過程中你將遇到另一個500錯誤 - >使用簡單模型和視圖,靜態HTML站點或重定向到一個錯誤處理程序servlet。

春季安全:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter 
{ 
    @Override 
    protected void configure(HttpSecurity security) throws Exception 
    { 
     // Set the security settings 
     security.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().csrf(); 
    } 
} 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 
     <!-- Snipped --> 
    <error-page> 
     <error-code>404</error-code> 
     <location>/error</location> 
    </error-page> 
</web-app> 

404錯誤動作:

@Controller 
public class ErrorController 
{ 
    @RequestMapping(value = "/error", method = RequestMethod.GET) 
    public ModelAndView addresses() 
    { 
     return new ModelAndView("error"); // Or redirect to /welcome 
    } 
}