Spring MVC +在建议之前检查安全性 [英] Spring MVC + Before Advice check security

查看:184
本文介绍了Spring MVC +在建议之前检查安全性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试Spring AOP框架并提出以下问题。

I'm testing Spring AOP framework and have the following question.

我有以下代码:

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";
}

我想在Advice方面之前创建以检查用户会话中的用户安全性。

I would like to create before Advice aspect to check user security in user session.

@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....
    }

}

我不知道该怎么办是取消DaoServices.save方法的执行如果用户未经过身份验证并导致控制器返回任何其他值而不是好。

What I don't know what to do is to cancel execution of DaoServices.save method if the user is not authenticated and cause controller to return any other value instead of "ok".

我可以这样做吗?
有人能指出我这样的例子吗?
我可以使用@Around建议进行此类操作吗?

Can i do it? Can someone point me to such example? Can I use @Around advice for such actions?

推荐答案

是的,我认为你应该使用@Around建议如果验证失败,则不要调用 ProceedingJoinPoint.proceed()方法。

Yes, I think you should use the @Around advice and just not call the ProceedingJoinPoint.proceed() method if the authentication fails.

更新:

要返回其他内容,您的方法应如下所示:

To return something else your method should look something like this:

@Before("methods()")
public Object checkSecurity(ProceedingJoinPoint pjp) throws Throwable{
    if (/*user is authenticated*/) {
        return pjp.proceed();
    } else {
        return "NOT OK";
    }
}

请注意该方法返回一个对象。另见Spring的这一部分文档

Please notice that the method returns an object. See also this part of the Spring documentation.

这篇关于Spring MVC +在建议之前检查安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆