[英]Spring AOP: Accessing return value of advice
我想增加我對AOP的了解,這目前還不是很好。 我有以下程序。 總的來說,我正在創建一些User對象並打印出它們的用戶名。 就在創建用戶之前,我有一個安全方面,該方面可以運行並在發現危險字時顯示錯誤(檢查SQL注入)。 它可以工作,但是即使安全性發現錯誤,它也始終會創建用戶並打印出用戶名。
安全檢查返回一個布爾值。 如果check返回true,是否可以只運行其余部分? 為此,我需要訪問建議的返回值。 還是需要通過更多的AOP和返回后的操作來完成? 如果是這樣,有人可以解釋一下嗎? 我現在只知道之前和之后的用法。
謝謝你的幫助。 下面是我的代碼:
public class App
{
public static void main( String[] args ){
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
List<String> names = new ArrayList<String>();
List<User> users = new ArrayList<User>();
names.add("Chris");
names.add("Dave");
names.add(";DROP table");
names.add("Bob");
User user = null;
for(String name : names){
user = context.getBean("user", User.class);
user.setUsername(name);
users.add(user);
}
for(User u : users){
System.out.println(u.getUsername());
}
}
}
這是安全性:
public class Security {
private List<String> words = new ArrayList<String>();
{
words.add("drop");
words.add("delete");
words.add("truncate");
words.add("remove");
}
public boolean check(String input){
for(String word: words){
if(input.toLowerCase().contains(word)){
System.err.println("Unsafe word " + word + " found!!!!!");
return false;
}
}
return true;
}
}
這是我在context.xml中的內容
<bean id="user" class="com.company.springAOPExample.User" scope="prototype" />
<bean id="security" class="com.company.springAOPExample.Security" />
<aop:config>
<aop:aspect ref="security">
<aop:pointcut id="setUsername" expression="execution(* com.company.springAOPExample.User.setUsername(java.lang.String)) and args(username)" />
<aop:before pointcut-ref="setUsername" method="check" arg-names="username" />
</aop:aspect>
</aop:config>
首先,我認為您必須在users.add(user);
上添加AOP users.add(user);
不是user.setUsername(name);
那么您需要使用“周圍建議”並在為true的情況下調用繼續,在為false的情況下不調用,因為:
public class Security {
private List<String> words = new ArrayList<String>();
{
words.add("drop");
words.add("delete");
words.add("truncate");
words.add("remove");
}
public Void check(User user, ProceedingJoinPoint pjp){
for(String word: words){
if(!user.getUsername().toLowerCase().contains(word)){
return pjp.proceed();
}
return;
}
}
}
好的,我已經解決了這個問題,這要歸功於先前的回答,它使我指向“周圍”,並且對本教程非常有用( http://www.compiletimeerror.com/2013/05/spring-aop-around-advice -example.html#.UzMAJvkRAmM )。 這是主要方法:
public static void main( String[] args ){
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
List<String> names = new ArrayList<String>();
List<User> users = new ArrayList<User>();
names.add("Chris");
names.add("Dave");
names.add(";DROP table");
names.add("Bob");
User user = null;
for(String name : names){
user = context.getBean("user", User.class);
user.setUsername(name);
if(user.getUsername()!=null){
users.add(user);
}
}
for(User u : users){
System.out.println(u.getUsername());
}
}
這是“安全性”中的檢查方法:
public void check(ProceedingJoinPoint pjp) throws Throwable {
boolean match = false;
Object o[] = pjp.getArgs();
for(String word: words){
if(o[0].toString().toLowerCase().contains(word)){
System.err.println("Unsafe word " + word + " found!!!!!");
match = true;
}
}
if(!match){
pjp.proceed();
return;
}
}
這是context.xml:
<bean id="user" class="com.company.springAOPExample.User" scope="prototype" />
<bean id="security" class="com.company.springAOPExample.Security" />
<aop:config>
<aop:aspect ref="security">
<aop:pointcut id="setUsername" expression="execution(* com.company.springAOPExample.User.setUsername(java.lang.String))" />
<aop:around pointcut-ref="setUsername" method="check" />
</aop:aspect>
</aop:config>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.