Grails Spring Security Core 插件 - 将 flash 消息添加到登录页面

发布于 2024-10-02 22:25:48 字数 364 浏览 0 评论 0 原文

我正在使用 Grails Spring Security Core Plugin 使用以下注释来保护特定操作。

@Secured(['IS_AUTHENTICATED_REMEMBERED'])

如果用户未登录,这会导致操作重定向到我的登录页面。登录后,他们将被重定向到原始操作。

在这种情况下,如何将 Flash 消息添加到登录页面,即

flash.message = "You must be logged in to submit a news story"

为了澄清,我只想在用户尝试访问此特定操作后重定向到登录页面时显示该消息。其他操作不会触发该消息。

I am using the Grails Spring Security Core Plugin to secure a certain action using the following annotation.

@Secured(['IS_AUTHENTICATED_REMEMBERED'])

This causes the action to redirect to my login page if the user is not logged in. Once they are logged in they are then redirected to the original action.

How can I add a flash message to the login page in this scenario, i.e.

flash.message = "You must be logged in to submit a news story"

To clarify, I would only want the message to be displayed if the user was redirected to the login page after attempting to access this specific action. Other actions would not trigger the message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来世叙缘 2024-10-09 22:25:48

我已经解决了这个问题如下。

  1. 从操作中远程注释。
  2. 在操作的开头添加以下代码(news 和 Submit 分别是相关的控制器和操作)。

    if (!springSecurityService.isLoggedIn()) {
        flash.message = "您必须登录才能提交新闻报道。"
        重定向(控制器:“登录”,操作:“身份验证”,参数:[“spring-security-redirect”:“/新闻/提交”])
    }
    
  3. 将以下内容添加到登录表单。

    ;
    

I have solved this as follows.

  1. Remote the annotation from the action.
  2. Add the following code at the beginning of the action (news and submit being the relevant controller and action respectively).

    if (!springSecurityService.isLoggedIn()) {
        flash.message = "You must be logged in to submit a news story."
        redirect(controller:"login", action: "auth", params:["spring-security-redirect" : "/news/submit"])
    }
    
  3. Add the following to the login form.

    <input type='hidden' name='spring-security-redirect' value='${params['spring-security-redirect']}'/>
    
苍暮颜 2024-10-09 22:25:48

例如,将其添加到您的登录视图中:

<sec:noAccess url="/admin/listUsers">
    You must be logged in to view the list of users.
</sec:noAccess>

请参阅 安全标签库文档

Add, for example, this to your login view:

<sec:noAccess url="/admin/listUsers">
    You must be logged in to view the list of users.
</sec:noAccess>

See the security taglib's documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文