gpt4 book ai didi

java - Spring AOP @Before 服务中 - Before Advice 检查安全性

转载 作者:行者123 更新时间:2023-12-01 12:28:25 27 4
gpt4 key购买 nike

文件applicationContext.xml

<bean id="employee" class="com.my.Employee">
</bean>
<bean id="observer" class="com.my.Observer" />

员工.java

public Employee() {
System.out.println("Employee constructor executed");
}

private String name;
private String company;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public String getCompany() {
return company;
}

public void setCompany(String company) {
this.company = company;
}

观察者.java

@Before("execution(* com.my.*.get*()) ")
public void beforeAdvice(JoinPoint jp) {
Employee emp = (Employee) jp.getTarget();
if("admin".equals(emp.getName())){
// access method get*()
}
else {
// can't access method get*()
}
}

AOPTest.java

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Employee employee = (Employee) context.getBean("employee");
employee.setName("admin");
employee.setCompany("IBM");
System.out.println("1 = "+employee.getName());
System.out.println("2 = " + employee.getCompany());
}

如果我改变

employee.setName("user");

// can't access method get*(). But method getName() and getCompany() is still running. How can i control this. If setName("user") >>> can't access to method getName and getCompany(). Help me ! plz

最佳答案

beforeAdvice 中抛出 Exception 怎么样?

else {
throw new NotAuthorizedException("Can not access to method get");
}

关于java - Spring AOP @Before 服务中 - Before Advice 检查安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com