gpt4 book ai didi

grails - 如何动态生成grails.plugins.springsecurity使用的URL?

转载 作者:行者123 更新时间:2023-12-02 14:37:51 25 4
gpt4 key购买 nike

这不是简单的问题之一。所以让我详细解释一下...

背景:
在我的grails应用程序中,我具有以下映射:

class UrlMappings {
static mappings = {
"/$storeId/$controller/$action?/$id?"
...
}
}

这意味着我的应用程序将处理的所有url都具有前面的url参数“storeId”。所有 Controller 都将使用此参数来呈现内容并执行其他操作...

我也有带注释的 Controller
@Secured(['IS_AUTHENTICATED_REMEMBERED'])
class SomeController {
def index = {
// ...
}
}

假设用户正在尝试访问此页面:
/555/some/index

如果这是未经身份验证的用户,则安全性插件会将其重定向到
/login/auth

问题是:
当spring-security重定向到此页面时,用户将看到404。发生这种情况是因为登录 Controller 将仅处理以下网址
/555/login/auth

问题是:
我应该怎么做才能动态建立这个网址?


事实证明:
  • 有用于的配置参数
    Spring 安全,称为
    “auth.loginFormUrl”。但这只是
    静态文字。我需要建立这个
    基于影响此内容的网址
    重定向
  • 重定向已完成(我不是100%
    确定)在org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint#commence中。
    但是如何覆盖呢?
  • 最佳答案

    通常,这是一个静态值,但它是从LoginUrlAuthenticationEntryPoint.getLoginFormUrl()调用的,因此您可以将其子类化,并在覆盖的方法中对URL进行动态计算。该插件已经使用org.codehaus.groovy.grails.plugins.springsecurity.AjaxAwareAuthenticationEntryPoint对其进行了子类化,因此您应该对其进行扩展。

    要注册您的实现而不是插件,请在grails-app/conf/spring/resources.groovy中添加一个bean覆盖:

    import com.yourcompany.yourapp.YourAuthenticationEntryPoint
    beans = {
    authenticationEntryPoint(YourAuthenticationEntryPoint) {
    loginFormUrl = '/login/auth' // has to be specified even though it's ignored
    ajaxLoginFormUrl = '/login/authAjax'
    portMapper = ref('portMapper')
    portResolver = ref('portResolver')
    }
    }

    您没有通过此方法直接访问该请求,但是可以在线程本地调用 org.codehaus.groovy.grails.plugins.springsecurity.SecurityRequestHolder.getRequest()中使用它。

    关于grails - 如何动态生成grails.plugins.springsecurity使用的URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6381739/

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