2015-09-04 33 views

回答

0

我由http節點下添加以下內容到我的春季安全XML解決了這個問題:

<security:access-denied-handler error-page="/#/login" /> 
<security:session-management invalid-session-url="/#/login" session-fixation-protection="changeSessionId" /> 
0

您可以指定你的應用程序通過web.xml中的錯誤頁面元素將其指定

如處理異常或HTTP狀態代碼:web.xml中

<error-page> 
    <error-code>401</error-code> 
    <location>/login.html</location> 
</error-page> 

相同的方式,處理其他HTTP狀態碼404代表頁面未找到頁面。

+2

有沒有辦法使用基於java/annotaion的配置來做到這一點? –

0

對於spring-security應用基於spring-boot

定義處理豆:

@Component 
public class CommenceEntryPoint implements AuthenticationEntryPoint, Serializable { 
    private static final long serialVersionUID = 565662170056829238L; 

    // invoked when user tries to access a secured REST resource without supplying any credentials, 
    @Override 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { 
     // send a json object, with http code 401, 
     // response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 

     // redirect to login page, for non-ajax request, 
     response.sendRedirect("/login.html"); 
    } 
} 

在安全配置類(例如WebSecurityConfig):

的Autowire豆:

@Autowired 
private CommenceEntryPoint unauthorizedHandler; // handle unauthorized request, 

指定的處理程序:

.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() // excepion handler, 

提示:

  • 這僅適用於非Ajax請求,
  • 對於Ajax請求,更好的回報401代碼,並讓前端處理。
    如果要使用401響應,請在CommenceEntryPoint.commence()中使用response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");而不是response.sendRedirect()
相關問題