2017-08-30 55 views
2

我對Spring MVC比較陌生,目前我需要爲Web服務包含一個身份驗證攔截器。如果身份驗證失敗,我需要攔截器來引發AccessForbiddenException,如果通過則返回true。我還創建了一個AuthenticationExceptionController來捕獲異常並返回一個帶有HttpStatus的ResponseEntity。但是,當我遇到內部錯誤500,我懷疑這是由於AuthenticationExceptionController無法捕獲異常。以下是我的代碼。有關我如何解決它的任何建議?控制器建議無法捕捉異常

AuthenticationInterceptor.java

package path.controller; 

public class AuthenticationInterceptor implements HandlerInterceptor { 
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { 
     // handle the authentication check 

     if(authentication fails) { 
      throw new AccessForbiddenException("access forbidden"); 
     } 
     return true; 
    } 

    @Override 
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 
    } 

    @Override 
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) 
     throws Exception { 
    } 
} 

AccessForbiddenException.java

package path.controller; 

public class AccessForbiddenException extends RunTimeException{ 
    public AccessForbiddenException(String message) { 
     super(message); 
    } 
} 

AuthenticationExceptionController.java

package path.controller; 

@ControllerAdvice 
public class AuthenticationExceptionController {  

    @ExceptionHandler(AccessForbiddenException.class) 
    public ResponseEntity<?> handleException(AccessForbiddenException e) { 
     return new ResponseEntity<String>(e.getMessage(), HttpStatus.FORBIDDEN); 
    } 
} 

根的context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context.xsd 
         http://www.springframework.org/schema/tx 
         http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <context:annotation-config /> 
    <bean id="contextApplicationContextProvider" class="path.context.provider.ApplicationContextProvider"></bean> 

    <context:property-placeholder location="classpath:application.properties" /> 

    <context:component-scan base-package="path.**" /> 
</beans> 

servlet的context.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

    <context:property-placeholder location="classpath:application.properties" /> 

    <context:annotation-config /> 

    <annotation-driven /> 

    <view-controller path="" view-name="index.html" /> 

    <resources mapping="**" location="/" /> 

    <beans:bean 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/" /> 
     <beans:property name="suffix" value="" /> 
    </beans:bean> 

    <interceptors> 
     <interceptor> 
      <mapping path="/**" /> 
      <exclude-mapping path="/login"/> 
      <exclude-mapping path="/authenticate"/> 
      <beans:bean class="path.controller.AuthenticationInterceptor" /> 
     </interceptor> 
    </interceptors> 

    <context:component-scan base-package="path" use-default-filters="false"> 
     <context:include-filter expression="path.Controller" type="annotation" /> 
    </context:component-scan> 
</beans:beans> 
+2

1.如果你是新手,不要從Spring XML開始;世界已經移動。 2.發佈500錯誤。 3.發佈堆棧跟蹤,其中有一個。 4.發佈請求。 –

回答

0

@ControllerAdvice捕捉僅在控制器級別產生的異常。

所以如果在控制器調用之前在auth級別拋出異常,這是徒勞的。

相反,您可以嘗試在auth之前添加過濾器並捕獲過濾器中的異常。