2014-11-01 82 views
2

設置如下:嵌入式Jetty 9.3.0.M0運行GuiceServlet 3.0,它反過來使用Jersey 1.18.1進行映射以及它可以執行的所有其他漂亮的事情。 Shiro 1.2.3被添加到此設置以提供安全性。在進行一般Shiro和ShiroWebModule中的filterChains的配置時,設置完美無瑕。爲什麼向@RequiresUser的請求沒有重定向到登錄頁面?

當使用Shiro的AOP功能註釋我的方法Shiro's annotations時,對註釋方法的URL的請求不會被重定向到登錄頁面。相反,org.apache.shiro.authz.UnauthenticatedException異常由新澤西州的ContainerResponse拋出:

ERROR com.sun.jersey.spi.container.ContainerResponse - 
The RuntimeException could not be mapped to a response, 
re-throwing to the HTTP container 
org.apache.shiro.authz.UnauthenticatedException: Attempting to perform a user-only operation. 
The current Subject is not a user (they haven't been authenticated or remembered from a previous login). Access denied. 
at org.apache.shiro.authz.aop.UserAnnotationHandler.assertAuthorized(UserAnnotationHandler.java:61) 
at org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor.assertAuthorized(AuthorizingAnnotationMethodInterceptor.java:84) 
at org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor.invoke(AuthorizingAnnotationMethodInterceptor.java:67) 
at org.apache.shiro.guice.aop.AopAllianceMethodInterceptorAdapter.invoke(AopAllianceMethodInterceptorAdapter.java:36) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60) 
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185) 
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) 
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302) 
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409) 
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263) 
at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178) 
at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62) 
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:112) 
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) 
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) 
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) 
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) 
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) 
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) 
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) 
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) 
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) 
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118) 
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113) 
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650) 
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:583) 
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221) 
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1126) 
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) 
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) 
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1060) 
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 
at org.eclipse.jetty.server.Server.handle(Server.java:498) 
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:265) 
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:243) 
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) 
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610) 
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539) 
at java.lang.Thread.run(Thread.java:744) 
Caused by: org.apache.shiro.authz.AuthorizationException: Not authorized to invoke method: public java.lang.String org.example.product.ExampleApp.controller.Index.secured() 
at org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor.assertAuthorized(AuthorizingAnnotationMethodInterceptor.java:90) 

瀏覽器顯示一個javax.servlet.ServletException: Filtered request failed.

然而,根據新澤西州ExceptionMapper實現捆綁:

/* other imports omitted for readability */ 
import org.apache.shiro.authz.UnauthenticatedException; 

public class UnauthenticatedExceptionHandler 
       implements ExceptionMapper<UnauthenticatedException>{ 

    public Response toResponse(UnauthenticatedException exception) {  
    return Response.status(Status.FORBIDDEN).type(MediaType.TEXT_PLAIN) 
      .entity("Authorization required").build(); 
    } 
} 

因此即使四郎不將請求重定向到登錄頁面,則ExceptionMapper應該觸發,它沒有。爲NotFoundException等工作的綁定ExceptionMappers。

必要MethodInterceptors定義和約束的模塊,其被安裝在我的「主」模塊中:

/* imports ommitted for readability */ 
public class ShiroMethodInterceptorModule extends AbstractModule { 

@Override 
protected void configure() { 
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(RequiresRoles.class), 
      new ShiroMethodInterceptor(new RoleAnnotationMethodInterceptor())); 
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(RequiresUser.class), 
      new ShiroMethodInterceptor(new UserAnnotationMethodInterceptor())); 
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(RequiresPermissions.class), 
      new ShiroMethodInterceptor(new PermissionAnnotationMethodInterceptor())); 
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(RequiresGuest.class), 
      new ShiroMethodInterceptor(new GuestAnnotationMethodInterceptor())); 
    bindInterceptor(Matchers.any(), Matchers.annotatedWith(RequiresAuthentication.class), 
      new ShiroMethodInterceptor(new AuthenticatedAnnotationMethodInterceptor())); 
    } 

} 

ShiroMethodInterceptor(從@pabiagioli 's "shiro-guice-jersey-bootstrap" project無恥複製)的實施如下:

public class ShiroMethodInterceptor implements MethodInterceptor{ 

static final Logger logger = Logger.getLogger(ShiroMethodInterceptor.class.getName()); 

private org.apache.shiro.aop.MethodInterceptor methodInterceptor; 

public ShiroMethodInterceptor(org.apache.shiro.aop.MethodInterceptor methodInterceptor) { 
    this.methodInterceptor = methodInterceptor; 
} 


    public Object invoke(MethodInvocation methodInvocation) throws Throwable { 
    return methodInterceptor.invoke(new ShiroMethodInvocation(methodInvocation)); 
    } 

    private static class ShiroMethodInvocation implements org.apache.shiro.aop.MethodInvocation { 

    private final MethodInvocation methodInvocation; 

    public ShiroMethodInvocation(MethodInvocation methodInvocation) { 
     this.methodInvocation = methodInvocation; 
    } 

    public Object proceed() throws Throwable { 
     return methodInvocation.proceed(); 
    } 

    public Method getMethod() { 
     return methodInvocation.getMethod(); 
    } 

    public Object[] getArguments() { 
     return methodInvocation.getArguments(); 
    } 

    public Object getThis() { 
     return methodInvocation.getThis(); 
    } 
    } 
} 

該項目是用Maven構建,並確定了相關的依賴關係是:

  • Jersey的吉斯1.18.1
    • 吉斯3.0
    • 吉斯-的servlet 3.0
    • 球衣-的servlet 1.18.1
  • 四郎核1.2.3
  • 四郎的Web 1.2。 3
  • 四郎-吉斯1.2.3
  • 四郎-的AspectJ 1.2.3

編輯:爲是完整的:境界是由根據提供者創建的ShiroWebModule的配置方法如下:

protected void configureShiroWeb() { 

    bindConstant().annotatedWith(Names.named("shiro.loginUrl")) 
      .to("/login"); 

    addFilterChain("/", ANON); 

    addFilterChain("/login", AUTHC); 
    addFilterChain("/logout", LOGOUT); 

    /* The following rule is activated when not using AOP 
    * annotations 
    * addFilterChain("/**", AUTHC); 
    */ 

} 

我真的很感激任何指針。請不要猶豫,要求進一步的信息。

編輯2:這可能是不相關的,但爲了完整性,我在此添加它。進一步調查此問題我注意到,即使我完全在ShiroWebModule中配置Shiro並完全禁用註釋和AOP,但如果出現錯誤或空證書,就我可以看到運行時異常異常被拋出,這是由運動衫打包並打包變成WebApplicationException:

javax.ws.rs.WebApplicationException 
at com.sun.jersey.server.impl.uri.rules.TerminatingRule.accept(TerminatingRule.java:66) 
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409) 
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263) 
at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178) 
at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62) 
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:112) 
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) 
at org.apache.shiro.guice.web.SimpleFilterChain.doFilter(SimpleFilterChain.java:44) 
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) 
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) 
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) 
at org.apache.shiro.guice.web.SimpleFilterChain.doFilter(SimpleFilterChain.java:41) 
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) 
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) 
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) 
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) 
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) 
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) 
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) 
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) 
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118) 
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113) 
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650) 
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:583) 
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221) 
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1126) 
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) 
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) 
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1060) 
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 
at org.eclipse.jetty.server.Server.handle(Server.java:498) 
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:265) 
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:243) 
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) 
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610) 
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539) 
at java.lang.Thread.run(Thread.java:744) 

javax.ws.rs.WebApplicationException 
at com.sun.jersey.server.impl.uri.rules.TerminatingRule.accept(TerminatingRule.java:66) 
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409) 
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263) 
at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178) 
at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62) 
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:112) 
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) 
at org.apache.shiro.guice.web.SimpleFilterChain.doFilter(SimpleFilterChain.java:44) 
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) 
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) 
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) 
at org.apache.shiro.guice.web.SimpleFilterChain.doFilter(SimpleFilterChain.java:41) 
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) 
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) 
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) 
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) 
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) 
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) 
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) 
at com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:163) 
at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:58) 
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118) 
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113) 
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650) 
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:583) 
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221) 
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1126) 
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) 
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) 
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1060) 
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 
at org.eclipse.jetty.server.Server.handle(Server.java:498) 
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:265) 
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:243) 
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) 
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610) 
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539) 
at java.lang.Thread.run(Thread.java:744) 
+0

還未回答這個問題嗎? – Gideon 2018-01-15 09:54:12

回答

1

這實際上是authc過濾器常見的錯誤。它需要配置爲過濾所有的URL,否則它們不可用。試試這個:

addFilterChain("/logout", LOGOUT); 
addFilterChain("/**", AUTHC); 
+0

非常感謝您的回答。我嘗試了它,區別在於所有請求(甚至是用@ @ RequiresGuest註釋的請求)都被重定向到了登錄頁面。不過,註釋並沒有被忽略,因爲我嘗試了'@ RequiresRoles'。但不是顯示配置的'roles.unauthorizedUrl'(綁定爲ShiroWebModule中的常量),而是拋出異常:'javax.servlet.ServletException:org.apache.shiro.authz.UnauthorizedException:主體沒有角色[admin ]' - 雖然我有一個UnauthorizedExceptionMapper實現並綁定。我不確定這裏發生了什麼事。 – 2014-11-01 19:32:35

+0

好吧,我的壞。除了通過ShiroWebModule安裝的過濾器之外,我還通過GuiceShiroFilter過濾了所有內容。現在,RequiresGuest註釋工作(將必須實現一個匿名註釋;))。但是,針對非匹配授權的ServletExceptions仍然被拋出。 – 2014-11-01 19:48:04

0

基於註釋的授權斷言邏輯當前不是web感知的。你有兩個選擇:

  1. 配置你選擇的web框架,以捕獲異常,因爲他們泡了和重定向如預期,或
  2. 使用四郎的Servlet過濾型的斷言框架(如你似乎已經配置) 。

爲了使這是一個更加「開箱即用」的工作體驗,需要修改Shiro servlet過濾器實現以捕獲已知的Shiro異常,因爲它們會起泡並將最終用戶重定向到已知位置 - 請打開一個Jira問題來捕獲這項工作。 HTH!

相關問題