Spring自定义授权不能使用@PreAuthorize

时间:2014-04-04 07:15:01

标签: spring-mvc spring-security authorization

我正在尝试在我的spring(3.2.2)mvc app中实现自定义@PreAuthorize方法。但每当我点击链接时,我都会把我带到实现@PreAuthorize的控制器方法,它会给我这个错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause 

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
    org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:339)
    org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:198)
    org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:60)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
    com.nav.qanda.admin.question.controller.AdminQuestionController$$EnhancerByCGLIB$$5bcda356_2.handleRequest(<generated>)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是我的app-servlet.xml

  <security:global-method-security pre-post-annotations="enabled">
    <security:expression-handler ref="expressionHandler" />
  </security:global-method-security>

  <bean id="expressionHandler" class="com.nav.panda.security.PandaMethodSecurityExpressionHandler" />

这些是覆盖

的java文件
public class PandaMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler{
      @Override
      protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication,
              MethodInvocation invocation){
        PandaMethodSecurityExpressionRoot root = new PandaMethodSecurityExpressionRoot(authentication);
        root.setThis(invocation.getThis());
        return root;
      }



public class PandaMethodSecurityExpressionRoot implements MethodSecurityExpressionOperations {


    private Object filterObject;
    private Object returnObject;
    private Object target;


    public  boolean adminOnly() {
        System.out.println("Checking admin authority");
        return true;
//     return  this.hasAuthority("ADMIN");
    } ... other methods

控制器看起来像这样:

@RequestMapping(value = "/createPandaPage", method = RequestMethod.GET)
      @PreAuthorize("adminOnly()")
      public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
           return new ModelAndView("admin/createPanda");
       }

web.xml:

<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>

 <filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping> 

我的应用程序中的登录将从我的应用程序填充User对象(具有从db读取的用户权限)。如何传递此用户对象以检查权限?而且,我还需要做些什么来实现这个目标呢?

1 个答案:

答案 0 :(得分:3)

导致调用此方法的请求需要通过Spring Security过滤器链,否则在检查权限时将没有可用的安全上下文。

在没有看到堆栈跟踪的其余部分的情况下,我不能说100%,但看起来这就是这里发生的事情,因为您看到了异常(如果您搜索错误消息,您应该找到类似的问题)。 / p>

因此,您需要确保安全过滤器链处理您想要保护的所有请求,并确保您的过滤器链已正确配置(如果您正在使用命名空间,则应该自动配置)。