【问题标题】:Spring security global-method-security doesn't work in OSGI environmentSpring security global-method-security 在 OSGI 环境中不起作用
【发布时间】:2015-06-25 10:09:07
【问题描述】:

我尝试使用 spring security 来保护我的 osgi 服务和 web 应用程序,intercept-url 工作正常,但全局方法 security 根本不起作用。对于纯 OSGI 包,代理模式不起作用,因为 spring-beans 无法访问 SpringProxy。我尝试了代理模式和aspectj模式,并在aspectj模式下启用了加载时间编织,包被成功加载。但是Preauthorized不起作用(在xml配置中添加了pre-post-annotations="enabled"),并且在接口和实现上都添加了注释,但它仍然不起作用。

我不知道spring security保护bean方法的机制。有没有人可以给我一些提示?谢谢!

【问题讨论】:

  • 我调试了代码,似乎问题是因为每个包都有自己的类负载,并且 PrePostAnnotationSecurityMetadataSource 从它自己的包中检查 Pre post 注释,所以它找不到我的业务类。似乎可能的解决方案是在我自己的包中定义一个自定义 SecurityMetadataSource 。我使用相同的技术来解决类似的问题。

标签: methods spring-security osgi bundle protected


【解决方案1】:

找到原因,默认上下文:component-scan不会为生成的bean生成代理。

  <context:component-scan base-package="org.ops4j.pax.web.samples.spring.service" scoped-proxy="targetClass/interfaces" />

【讨论】:

  • 这个还是不行,问题不是proxy,而是component-scan中创建bean的机制
  • component-scan 中定义搜索类时 spring-osgi-io 出现 bug 真的很遗憾
【解决方案2】:

在组件扫描 OsgiBundleResourcePatternResolver 中定义的搜索类时 spring-osgi-io 中出现错误真的很遗憾。类路径从一开始就被删除了,所以在第 2 步中,它搜索包根文件夹,而不是类路径

// 消除类路径路径 最终字符串路径 = OsgiResourceUtils.stripPrefix(locationPattern);

    final Collection foundPaths = new LinkedHashSet();

    // 1. search the imported packages

    // find folder path matching 
    final String rootDirPath = determineFolderPattern(path);

    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged(new PrivilegedAction() {

            public Object run() {
                for (int i = 0; i < importedBundles.length; i++) {
                    final ImportedBundle importedBundle = importedBundles[i];
                    if (!bundle.equals(importedBundle.getBundle())) {
                        findImportedBundleMatchingResource(importedBundle, rootDirPath, path, foundPaths);
                    }
                }
                return null;
            }
        });
    }
    else {
        for (int i = 0; i < importedBundles.length; i++) {
            final ImportedBundle importedBundle = importedBundles[i];
            if (!bundle.equals(importedBundle.getBundle())) {
                findImportedBundleMatchingResource(importedBundle, rootDirPath, path, foundPaths);
            }
        }
    }

    // 2. search the target bundle
    findSyntheticClassPathMatchingResource(bundle, path, foundPaths);

【讨论】:

  • 很遗憾,2015 年人们仍然认为类路径扫描是一种可行的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多