2

我似乎无法让 Spring Pre/Post 方法安全注释起作用。我已阅读有关该主题的所有相关 stackoverflow 问题,主要建议是确保在与您希望保护的 bean 相同的上下文中启用 global-method-security。我的 dispatcher-servlet.xml 有以下内容:

  <context:component-scan base-package="com.package.path" />
  <context:annotation-config />
  <security:global-method-security pre-post-annotations="enabled" />

有问题的 bean 在“com.package.path”中。我知道 Spring 正在正确地创建它们的实例,因为注入工作得很好,并且请求正在由预期的类提供服务。

因此,这是“com.package.path”中的示例服务类:

@Controller
@RequestMapping("/article")
public class ArticleServiceImpl extends GWTController implements ArticleService {
    @Autowired
    public ArticleServiceImpl(DataSource ds) {

    }

    @Override
    @PreAuthorize("hasRole('ROLE_BASIC_USER')")
    public Article save(Article article) {

    }

}

保存方法上的注释不起作用。几个重要的注意事项:

  • 我正在使用 GWT,尽管从我读过的内容来看,这并不重要。
  • 我的方法安全性在另一个类似的项目中运行良好。唯一的区别是在另一个项目中有一个 DAO 层,而在这个项目中没有。正是在这一层,我有注释安全工作。但是,只要 Spring 负责创建 bean,这应该是什么“层”并不重要,对吧?
  • 上面的接口“ArticleService”是一个GWT服务接口。我试过把注释放在那里,但这也不起作用。

如果需要,这是上面引用的我的 GWTController 类:

package com.areahomeschoolers.baconbits.server.spring;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.ServletConfigAware;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.areahomeschoolers.baconbits.server.util.ServerContext;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Spring controller class that handles all requests and passes them on to GWT. Also initializes server context.
 */
public class GWTController extends RemoteServiceServlet implements ServletConfigAware, ServletContextAware, Controller, RemoteService {

    private static final long serialVersionUID = 1L;

    protected ServletContext servletContext;

    @Override
    public ServletContext getServletContext() {
        return servletContext;
    }

    // Call GWT's RemoteService doPost() method and return null.
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        // load our ServerContext with current request, response, session, user, appContext, etc.
        ServerContext.loadContext(request, response, servletContext);
        try {
            doPost(request, response);
        } finally {
            ServerContext.unloadContext();
        }
        return null; // response handled by GWT RPC over XmlHttpRequest
    }

    @Override
    public void setServletConfig(ServletConfig conf) {
        try {
            super.init(conf);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @Override
    protected void checkPermutationStrongName() throws SecurityException {
        return;
    }

    @Override
    protected void doUnexpectedFailure(Throwable e) {
        e.printStackTrace();
        super.doUnexpectedFailure(e);
    }

}
4

1 回答 1

1

Spring Security 提供的安全方面继承了 Spring Framework 基于代理的 AOP 支持的所有限制。特别是,方面不适用于发生在对象“内部”的调用(除非您使用 AspectJ 编织),请参阅7.6.1 了解 AOP 代理

因此,如果您想以这种方式使用安全方面,则需要使用从外部调用您的服务的 GWT 集成机制,即不需要您的服务扩展的机制RemoteServiceServlet

spring4gwt 之类的东西应该可以正常工作。

于 2012-05-21T19:17:26.210 回答