2012-08-08 47 views
8

拋出處理異常:四郎:由我使用四郎註釋檢查這樣的授權使用註釋

@RequiresPermissions("addresses:list") 
    public ModelAndView getCarrierListPage() { 
     return new ModelAndView("addressList", "viewData", viewData); 
    } 

我的問題是這樣的:如果所要求的註釋的用戶沒有權限,一個異常正在拋出。如果發生異常,我寧願將用戶重定向到其他網址。我怎麼做?

這裏是我的四郎過濾器配置:

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 
    <property name="securityManager" ref="securityManager"/> 
    <property name="loginUrl" value="/showLoginPage"/> 
    <property name="filterChainDefinitions"> 
    </property> 
</bean> 

回答

1

它看起來像你使用Spring。我通過在控制器中提供一個ExceptionHandler來處理SpringMVC中的這個問題。

@ExceptionHandler(TheSpecificException.class) 
    protected ModelAndView handleSpecificException(ApplicationException e, HttpServletRequest request) 
    { 
     // code to handle view/redirect here 
    } 
1

沒有Spring MVC的你也可以使用ExceptionMapper:

@Provider 
@Component 
public class GenericExceptionMapper implements ExceptionMapper<ShiroException> { 

    @Override 
    public Response toResponse(final ShiroException ex) { 
     return Response.status(ex instanceof UnauthenticatedException ? Response.Status.UNAUTHORIZED : Response.Status.FORBIDDEN) 
       .entity(ex.getMessage()) 
       .type(MediaType.TEXT_PLAIN_TYPE) 
       .build(); 
    } 

} 
0

附加配置爲spring-servlet.xml:

<beans:bean 
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
    <beans:property name="exceptionMappings"> 
     <beans:props> 
      <beans:prop key="org.apache.shiro.authz.UnauthorizedException">/403</beans:prop> 
      <beans:prop key="org.apache.shiro.authz.AuthorizationException">/login</beans:prop> 
     </beans:props> 
    </beans:property> 
</beans:bean>