使用Spring Security Plugin在Grails中定制登录表单 [英] Custom Login Form in Grails using Spring Security Plugin
问题描述
我在Grails 2.0.3中使用Spring Security Plugin,并试图在每个页面的头部添加一个登录表单,如果用户没有登录。当我点击我的登录按钮时,它会尝试POST到一个没有任何代码来处理请求的控制器动作。这是我在做什么
I'm using the Spring Security Plugin in Grails 2.0.3, and am trying to add a login form to the header of each page if the user is not logged in. When I click my login button, it tries to POST to a controller action that doesn't really have any code to handle the request. Here's what I'm doing
BlogPostController
BlogPostController
class BlogPostController {
static defaultAction = "home"
/**
* Summary of the most recent blog posts.
*/
def home() {
// snip
}
def show() {
// snip
}
}
UrlMappings有一些额外的条目
UrlMappings has a few extra entries
"/blog/$year/$month/$day/$title"(controller: "blogPost", action: "show")
"/blog/$action?/$id?"(controller: "blogPost")
"/"(controller:"blogPost")
grails-app / views / layout / main.gsp
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
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>
Config.groovy
Config.groovy
grails.plugins.springsecurity.auth.loginFormUrl = '/'
当我点击我试图发布到/ myblog / blog /%2fmyblog%2fj_spring_security_check失败的登录按钮。我认为这是因为BlogPostController的主页操作不知道如何处理该请求。
When I click my login button it tries to post to "/myblog/blog/%2fmyblog%2fj_spring_security_check" which fails. I think it's because the BlogPostController's home action doesn't know how to handle that request.
任何想法我做错了?
谢谢!
Thanks!
推荐答案
我发现改变了
I found that changing
<g:form>
至
<form>
解决了这个问题。不知道为什么GSP表单标签正在这样做,但使用普通的旧元素工作。
solved this problem. Not sure why the GSP form tag is doing that, but using a plain old element works.
这篇关于使用Spring Security Plugin在Grails中定制登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!