首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“特殊”AOP UseCase

“特殊”AOP UseCase
EN

Stack Overflow用户
提问于 2014-05-08 09:29:40
回答 1查看 104关注 0票数 1

我有一些编译好的库,它包含这样的方法:

代码语言:javascript
复制
public boolean foo(String userID) {
    Class<?> ntSystemClass = Thread.currentThread().getContextClassLoader()
        .loadClass("com.sun.security.auth.module.NTSystem");
    Method getNameMethod = ntSystemClass.getMethod("getName", null);
    Object ntSystem = ntSystemClass.newInstance();
    String name = (String)getNameMethod.invoke(ntSystem, null);
    boolean same=userID.equalsIgnoreCase(name);
    if (same) {
       // more work done here
    } else {
       // more work done here
    }
}

对于一些非常特殊的用例,我需要确保布尔same,始终是 true。

我的第一种方法是扩展类并重写方法foo(),但这是不可实现的,因为在方法中,需要对其他库的私有内容进行许多引用。

因此,下一个方法是使用AOP。我用AspectJ尝试了一些东西,但没有找到解决方案。有人能帮我们吗?据我所知,我不能直接修改布尔same。是否有可能在库中以任何方式处理String name = (String)getNameMethod.invoke(ntSystem, null);

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-08 14:10:22

让我们在这里直截了当地说:从今以后,我假设您的“特殊用例”是您想要调整/攻击用户身份验证,希望是以合法的方式进行测试或其他什么的。

需要解决的主要问题是:

  • 只需操作foo方法的结果就很容易了(我已将其重命名为isCurrentUser(String userID),以澄清其意图)。但是如果我正确理解,这个方法有副作用,即它调用其他方法,而您希望保留这些副作用。所以我们必须更加小心,使用手术刀,而不是斧头。
  • 局部变量的更改没有切入点,因此必须拦截方法执行或调用,这将更改局部变量的值。
  • AspectJ通常不能拦截JDK方法execution() (除非您想先编织JDK,这是可能的,但超出了这里的范围)。因此,您必须从您自己的代码中拦截call()。我假设有可能编织到代码中,即使它包含在一个JAR中,而且您没有源代码。您可以使用LTW作为目标类,也可以为JAR使用二进制编织,从而创建一个新的编织版本。
  • NTSystem.getName()的方法调用不是以正常的方式进行的,而是通过反射API完成的。因此,您不能只使用像call(NTSystem.getName())这样的切入点,因为它永远不会被触发。你必须拦截call(public Object Method.invoke(Object, Object...))
  • 从理论上讲,其他反射方法调用可能是在isCurrentUser(..)内部进行的,因此我们必须细化切入点,以便只在调用真正的NTSystem.getName()时才匹配,而不是任何其他方法。
  • 作为一种方便的功能,我们希望能够动态地打开和关闭hackingMode

下面是一个完整的、可编译的代码示例(显然只在Windows上工作,就像您自己的代码片段一样):

要操作的方法和演示用途的主要方法:

代码语言:javascript
复制
package de.scrum_master.app;

import java.lang.reflect.Method;

import de.scrum_master.aspect.TweakAuthenticationAspect;

public class UserAuthentication {
    private static final String USER_NAME_GOOD = "alexander"; // Add your own user name here
    private static final String USER_NAME_BAD = "hacker";

    public static boolean isCurrentUser(String userID) throws Exception {
        Class<?> ntSystemClass = Thread.currentThread().getContextClassLoader()
                .loadClass("com.sun.security.auth.module.NTSystem");
        Method getNameMethod = ntSystemClass.getMethod("getName");
        Object ntSystem = ntSystemClass.newInstance();
        String currentUserID = (String) getNameMethod.invoke(ntSystem);
        boolean same = userID.equalsIgnoreCase(currentUserID);
        if (same) {
            System.out.println("Do something (same == true)");
        } else {
            System.out.println("Do something (same == false)");
        }
        return same;
    }

    public static void main(String[] args) throws Exception {
        testAuthentication(false);
        testAuthentication(true);
    }

    private static void testAuthentication(boolean hackingMode) throws Exception {
        TweakAuthenticationAspect.hackingMode = hackingMode;
        System.out.println("Testing authentication for hackingMode == " + hackingMode);
        System.out.println("Authentication result for " + USER_NAME_GOOD + ": "
                + isCurrentUser(USER_NAME_GOOD));
        System.out.println("Authentication result for " + USER_NAME_BAD + ": "
                + isCurrentUser(USER_NAME_BAD));
        System.out.println();
    }
}

如您所见,testAuthentication(boolean hackingMode)被调用了两次,一次是禁用了黑客代码,然后是启用的。在这两种情况下,它测试一个好的/正确的用户名(请编辑!)先是坏的(“黑客”)。

方面操作身份验证方法:

代码语言:javascript
复制
package de.scrum_master.aspect;

import com.sun.security.auth.module.NTSystem;
import de.scrum_master.app.UserAuthentication;
import java.lang.reflect.Method;

public aspect TweakAuthenticationAspect {
    public static boolean hackingMode = false;

    pointcut reflectiveCall_NTSystem_getName(NTSystem ntSystem, Method method) :
        call(public Object Method.invoke(Object, Object...)) &&
        args(ntSystem, *) &&
        target(method) &&
        if(method.getName().equals("getName"));

    pointcut cflow_isCurrentUser(String userID) :
        cflow(
            execution(* UserAuthentication.isCurrentUser(String)) &&
            args(userID)
        );

    Object around(NTSystem ntSystem, Method method, String userID) :
        reflectiveCall_NTSystem_getName(ntSystem, method) &&
        cflow_isCurrentUser(userID) &&
        if(hackingMode)
    {
        System.out.println("Join point: " + thisJoinPoint);
        System.out.println("Given user ID: " + userID);
        System.out.println("Reflectively called method: " + method);
        return userID;
    }
}

这里有几句解释:

  • 切入点reflectiveCall_NTSystem_getName拦截对Method.invoke(..)的调用,将第一个参数限制为NTSystem类型,从而消除了对其他类的反射调用。它还检查目标方法是否实际上是getName。也就是说,切入点检查是否真的要调用NTSystem.getName()`。
  • 切入点cflow_isCurrentUser捕获方法UserAuthentication.isCurrentUser(..)的控制流中的连接点,公开其参数userID供以后使用。
  • around(NTSystem ntSystem, Method method, String userID)建议将两个切入点和&&结合在一起,并且可以访问签名中的三个命名对象。在它的方法主体中,我们可以对这些对象做我们喜欢做的任何事情,例如将它们打印到控制台。我们也可以改变他们的状态,这在这种情况下是不必要的。通知通过if(hackingMode)动态激活。如果你不需要这个,你可以移除它,这只是为了方便。因为我们在这里使用一个around()通知,所以我们可以返回任何东西,而不是原始的方法结果。在这种情况下,我们总是返回userID,就好像给定的用户是当前登录到Windows的用户一样。这实际上导致本地same变量成为true,因为对equalsIgnoreCase(..)的调用也总是返回true。
  • 我也可以直接操作equalsIgnoreCase(..)的结果,但是局部变量currentUserID不等于userID。根据你想要的副作用种类,你可以根据你的喜好改变这种行为。

示例输出:

代码语言:javascript
复制
Testing authentication for hackingMode == false
Do something (same == true)
Authentication result for alexander: true
Do something (same == false)
Authentication result for hacker: false

Testing authentication for hackingMode == true
Join point: call(Object java.lang.reflect.Method.invoke(Object, Object[]))
Given user ID: alexander
Reflectively called method: public java.lang.String com.sun.security.auth.module.NTSystem.getName()
Do something (same == true)
Authentication result for alexander: true
Join point: call(Object java.lang.reflect.Method.invoke(Object, Object[]))
Given user ID: hacker
Reflectively called method: public java.lang.String com.sun.security.auth.module.NTSystem.getName()
Do something (same == true)
Authentication result for hacker: true

您可以在上面的部分看到,如果是hackingMode == false,身份验证会像往常一样工作,但是如果hackingMode == true,则始终会对任何给定的用户名进行肯定的身份验证。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23537854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档