1

我在 Grails 2.0.3 中使用 Spring Security 插件,如果用户未登录,我会尝试将登录表单添加到每个页面的标题中。当我单击登录按钮时,它会尝试 POST 到控制器没有任何代码来处理请求的操作。这就是我正在做的

博客帖子控制器

class BlogPostController {

    static defaultAction = "home"

    /**
     * Summary of the most recent blog posts.
     */
    def home() {
        // snip
    }

    def show() {
        // snip
    }
}

UrlMappings 有一些额外的条目

"/blog/$year/$month/$day/$title"(controller: "blogPost", action: "show")
"/blog/$action?/$id?"(controller: "blogPost")
"/"(controller:"blogPost")

grails-app/views/layout/main.gsp

<!doctype html>
<html lang="en" >
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><g:layoutTitle default="My Blog"/></title>
        <g:layoutHead/>
        <r:layoutResources />
    </head>
    <body>
        <g:render template="/banner" />
        <g:layoutBody/>
        <r:layoutResources />
    </body>
</html>

grails-app/views/_banner.gsp

<div id="banner">
    <r:img class="logo" dir="images" file="logo.png" />

    <div class="login">

        <sec:ifLoggedIn>
            <sec:username/>
        </sec:ifLoggedIn>

        <sec:ifNotLoggedIn>
        <g:form name="banner-login" method="POST" action="${resource('file': 'j_spring_security_check')}">
            <ul>
                <li>
                    <label for="j_username">Username:</label>
                    <g:textField name="j_username"/>
                </li>
                <li>
                    <label for="j_password">Password:</label>
                    <g:passwordField name="j_password"/>
                </li>
            </ul>
            <div class="button-panel">
                <g:submitButton name="banner-login-button" value="Log in" />
            </div>
        </g:form>
        </sec:ifNotLoggedIn>

    </div>

</div>

配置文件

grails.plugins.springsecurity.auth.loginFormUrl = '/'

当我单击我的登录按钮时,它会尝试发布到“/myblog/blog/%2fmyblog%2fj_spring_security_check”,但失败了。我认为这是因为 BlogPostController 的 home 操作不知道如何处理该请求。

知道我做错了什么吗?

谢谢!

4

2 回答 2

2

必须是/j_spring_security_check,只有这个 url 会被 Spring Security 过滤器处理(当然,如果你没有改变这个设置)。似乎resource生成了错误的 url,我看不出有任何理由在这里使用它。

尝试${createLink(uri: '/j_spring_security_check'}代替${resource('file': 'j_spring_security_check')}

于 2012-06-20T04:53:05.647 回答
0

我发现改变

<g:form>

<form>

解决了这个问题。不知道为什么 GSP 表单标签会这样做,但使用普通的旧元素是可行的。

于 2012-06-20T12:25:22.793 回答