dan*_*nik 2 java spring aspectj spring-mvc java-ee
我正在测试Spring AOP框架并提出以下问题.
我有以下代码:
package danny.test.controllers;
@Controller
public class MyController{
@Autowired
private DaoService service;
@RequestMapping(value="/save",method = RequestMethod.POST)
public String addUser(@Valid MyClass myClass, BindingResult result){
service.save(myClass);
return "Ok";
}
Run Code Online (Sandbox Code Playgroud)
我想在Advice方面之前创建以检查用户会话中的用户安全性.
@Aspect
public class Profiler {
@Pointcut("execution(* danny.test.services.DaoServices.*.*(..))")
public void methods(){}
@Before("methods()")
public void checkSecurity() throws Throwable{
//check session if user is authenticated....
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做是取消执行DaoServices.save方法,如果用户未经过身份验证并导致控制器返回任何其他值而不是"ok".
我可以做吗?有人能指出我这样的例子吗?我可以使用@Around建议进行此类操作吗?
是的,我认为您应该使用@Around建议,ProceedingJoinPoint.proceed()
如果身份验证失败,则不要调用该方法.
更新:
要返回其他内容,您的方法应该如下所示:
@Before("methods()")
public Object checkSecurity(ProceedingJoinPoint pjp) throws Throwable{
if (/*user is authenticated*/) {
return pjp.proceed();
} else {
return "NOT OK";
}
}
Run Code Online (Sandbox Code Playgroud)
请注意该方法返回一个对象.另请参阅Spring 文档的这一部分.