假设我有一个方面
public aspect Hack {
pointcut authHack(String user, String pass): call(* Authenticator.authenticate(String,String)) && args(user,pass);
boolean around(String user, String pass): authHack(user,pass) {
out("$$$ " + user + ":" + pass + " $$$");
return false;
}
}
Authenticator.authenticate 方法很重要。黑客拦截对此方法的调用。
是否可以编写第二个方面来取消/禁用 Hack 方面的 authHack 建议?
我可以捕捉到 around authHack 建议的执行,但是如果我想继续身份验证,我需要再次调用 Authenticator.authenticate,这会创建一个无限循环..
最佳答案
为了模拟您的情况,我编写了以下 Authenticator 代码:
public class Authenticator {
public boolean authenticate(String user, String pass) {
System.out.println("User: '" + user + "', pass: '" + pass + "'");
return true;
}
}
这是我的主类:
public class Main {
public static void main(String[] args) {
Authenticator authenticator = new Authenticator();
boolean status = authenticator.authenticate("Yaneeve", "12345");
System.out.println("Status: '" + status + "'");
}
}
输出是:
User: 'Yaneeve', pass: '12345'
Status: 'true'
我添加了你的 Hack 方面:
public aspect Hack {
pointcut authHack(String user, String pass): call(* Authenticator.authenticate(String,String)) && args(user,pass);
boolean around(String user, String pass): authHack(user,pass) {
System.out.println("$$$ " + user + ":" + pass + " $$$");
return false;
}
}
现在的输出是:
$$$ Yaneeve:12345 $$$
Status: 'false'
解决方案:
我创建了以下 HackTheHack 方面:
public aspect HackTheHack {
declare precedence: "HackTheHack", "Hack";
pointcut authHack(String user, String pass): call(* Authenticator.authenticate(String,String)) && args(user,pass);
boolean around(String user, String pass): authHack(user,pass) {
boolean status = false;
try {
Class<?> klass = Class.forName("Authenticator");
Object newInstance = klass.newInstance();
Method authMethod = klass.getDeclaredMethod("authenticate", String.class, String.class);
status = (Boolean) authMethod.invoke(newInstance, user, pass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return status;
}
}
输出又是:
User: 'Yaneeve', pass: '12345'
Status: 'true'
这仅在 Hack 方面的原始切入点是“调用”而不是“执行”时有效,因为执行实际上捕获了反射。
解释:
我使用方面优先级在 Hack 之前调用 HackTheHack:
declare precedence: "HackTheHack", "Hack";
然后我使用反射(注意可以而且应该进行优化以减少方法的重复查找)来简单地调用原始方法,而无需 Hack around 建议。由于两件事,这成为可能:
切入点 authHack(String user, String pass): call(* Authenticator.authenticate(String,String)) && args(user,pass); 使用(在两个方面) call() 而不是 execution()proceed()我想向您推荐Manning's AspectJ in Action, Second Edition这让我走上了正确的轨道:
6.3.1 Ordering of advice
As you’ve just seen, with multiple aspects present in a system, pieces of advice in the different aspects can often apply to a single join point. When this happens, AspectJ uses the following precedence rules to determine the order in which the advice is applied. Later, you’ll see how to control precedence:
1 The aspect with higher precedence executes its before advice on a join point before the aspect with lower precedence.
2 The aspect with higher precedence executes its after advice on a join point after the aspect with lower precedence.
3 The around advice in the higher-precedence aspect encloses the around advice in the lower-precedence aspect. This kind of arrangement allows the higher- precedence aspect to control whether the lower-precedence advice will run by controlling the call to proceed(). If the higher-precedence aspect doesn’t call proceed() in its advice body, not only will the lower-precedence aspects not execute, but the advised join point also won’t execute.
关于java - 在 AspectJ 中禁用/避免建议执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10194951/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/