如何围绕实现 java.security.Principal 的类中的方法创建切面?

发布于 2024-11-19 11:32:19 字数 686 浏览 2 评论 0原文

有谁知道如何在实现接口 java.security.Principal 的类中围绕方法“getName()”创建方面?

我正在使用 spring,下面是我的类的相关部分:

@Aspect
public class MyPrincipalAspect {
    @PointCut("(execution(* java.security.Principal.getName(..)))")
    private void getNamePC(){}

    @Around("getNamePC()")
    public Object getNameJP(ProceedingJoinPoint pjp) throws Throwable {
        Object retVal = pjp.proceed();
        return retVal;
    }
}

我实际上想用“retVal”做一些事情,但是,我只是使用上面的内容作为一个简单的例子。我的应用程序中有其他方面,它们都工作得很好。

我在 AspectJ 网站上读到一些关于无法从 java 包加载时编织类的内容,但他们说这仍然是可能的(但是,他们没有给出任何示例)。

我还尝试过非 Spring 方法(使用 aop.xml 文件和在我的应用程序服务器配置参数中定义的 javaagent)。

非常感谢任何帮助。

谢谢。

Does anyone know how one would go about creating an aspect around the method "getName()" in a class that implements the interface java.security.Principal?

I am using spring and below is the pertinent parts of my Class:

@Aspect
public class MyPrincipalAspect {
    @PointCut("(execution(* java.security.Principal.getName(..)))")
    private void getNamePC(){}

    @Around("getNamePC()")
    public Object getNameJP(ProceedingJoinPoint pjp) throws Throwable {
        Object retVal = pjp.proceed();
        return retVal;
    }
}

I actually want to do something with "retVal", however, I'm just using the above as a trivial example. I have other Aspects within my application and they all work just fine.

I read something on the AspectJ site regarding not being able to load-time weave classes from the java package, but they say it's still possible (but, they don't give any examples).

I've also tried going through a non-Spring approach (using an aop.xml file with javaagent defined in my app-server config params).

Any help is much appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹短裙飘 2024-11-26 11:32:19

您有多种选择:

  • 如果您可以控制调用 getName 的客户端类,则可以使用 call 切入点而不是 execution。这样你就不需要编织任何 JDK 类。
  • 如果你想要拦截的Principal子类不是JDK,而是第三方类(你没有提到),你可以在编译或加载时轻松编织它们。
  • 如果您确实需要编织 JDK/JRE 类,请在现有类文件中使用编译时编织,并在输出目录中创建新的编织文件。然后用它们替换原来的 JDK/JRE 类,将它们复制到默认位置。只要您可以控制运行时环境,这对于调试来说应该足够了,甚至对于生产使用来说也应该足够了。
  • 也许(我不知道,因为我没有尝试过)有一种方法可以编写一个类加载器,它会首先加载运行时编织器类,但它听起来不太可能或不像一个简单的选项。我必须自己进行网络搜索或进行一些编码实验才能找到答案。

更新:哦,顺便说一句,在你的切入点中,你可能想要使用 Principal+ 而不仅仅是 Principal ,以便也拦截重写的方法。

You have several options:

  • If you can control the client classes calling getName, you can use a call pointcut instead of execution. This way you do not need to weave any JDK classes.
  • If the Principal subclasses you want to intercept are not JDK, but third-party classes (you did not mention), you can weave them easily at compile or load time.
  • If you really need to weave JDK/JRE classes, use compile time weaving in the existing class files, creating new woven ones in your output directory. Then use those to replace the original JDK/JRE classes, copying them to the default location. That should be good enough for debugging and maybe even for productive use, as long as you have the runtime environment under your control.
  • Maybe (I do not know because I have not tried) there is a way to write a class loader which will load the runtime weaver classes first, but it does not sound likely or like a simple option. I would have to do a web search or some coding experiments by myself in order to find out.

Update: Oh by the way, in your pointcut you might want to use Principal+ instead of just Principal in order to also intercept overridden methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文