@Security批注的自定义消息 [英] Custom message for @Security annotation

查看:76
本文介绍了@Security批注的自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对路线使用@Security注释.像这样:

I'm trying to use @Security annotations for my routes. Like this:

/**
 * @return Response
 * @Route("/action")
 * @Security("has_role('ROLE_USER')")
 * @Template()
 */
public function someAction()
{
    return array();
}

当安全限制引发异常时,我收到消息Expression "has_role('ROLE_USER')" denied access.

When the security restriction fires an exception, I get the message Expression "has_role('ROLE_USER')" denied access.

这是不可以显示给最终用户的,所以我试图找到一种方法来定制用于注释的消息.

This is not acceptable to be shown to the end user, so I'm trying to find a way to customize the message for annotation.

简单的解决方法是不使用@Secutity注释并编写如下代码:

Simple workaround is to not to use @Secutity annotations and write code like these:

/**
 * @return Response
 * @Route("/action")
 * 
 * @Template()
 */
public function someAction()
{
    if (!$this->get('security.context')->isGranted('ROLE_USER')) {
        throw new AccessDeniedException('You have to be logged in in order to use this feature');
    }

    return array();
}

但这不太方便,也不太可读.

But this is less convenient and less readable.

是否可以将自定义消息写入@Security注释?

Is it possible to write custom message to @Security annotations?

推荐答案

我一意识到这是不可能的,我就做了一个

As soon as I realized that this is not possible, I have made a pull request to the Sensio FrameworkExtra Bundle to make this possible.

此PR允许通过指定消息参数来自定义显示的消息,例如

This PR allows to customize displayed message by specifying the message parameter like

@Security("has_role('ROLE_USER')",message="You have to be logged in")

这篇关于@Security批注的自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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