grails spring security自定义用户注册页面
Posted
技术标签:
【中文标题】grails spring security自定义用户注册页面【英文标题】:grails spring security custom user registration page 【发布时间】:2016-07-09 02:48:54 【问题描述】:我正在尝试创建一个使用 Spring security core 2.0 的应用程序,并且我正在尝试创建一个自定义注册页面。我认为我的问题出在我的 register.gsp 中,因为每次我尝试注册一个新用户时,它都会重定向到完全空白的 http://localhost:8080/home/register。可能是这种情况的进一步证据是,我在我的注册操作中包含了几个 println(),它们应该根据注册失败的位置触发,但它们在任何时候都不会触发。此外,没有在 dbconsole 中创建用户。请看我的gsp:
<html>
<head>
<meta name="layout" content="front"/>
<title>MSK | Register</title>
<style>
...
</style>
</head>
<body>
<div class="jumbotron">
<form action="$postUrl ?: '/home/register'" method="POST" id="registerForm" class="cssform" autocomplete="off">
<h2 class="form-signin-heading">Please register</h2>
<label for="username" class="sr-only">Username</label>
<input style="margin-bottom: 10px" type="text" id="username" class="form-control" placeholder="Username" name="$usernameParameter ?: 'j_username'"/>
<label for="email" class="sr-only">Email</label>
<input style="margin-bottom: 10px" type="email" id="email" class="form-control" placeholder="Email" name="$emailParameter ?: 'email'"/>
<label for="fName" class="sr-only">First Name</label>
<input style="margin-bottom: 10px" type="text" id="FName" class="form-control" placeholder="First Name" name="$fNameParameter ?: 'fName'"/>
<label for="lName" class="sr-only">Last Name</label>
<input style="margin-bottom: 10px" type="text" id="lName" class="form-control" placeholder="Last Name" name="$lNameParameter ?: 'lName'"/>
<label for="password" class="sr-only">Password</label>
<input style="margin-bottom: 10px" type="password" id="password" class="form-control" placeholder="Password" name="$passwordParameter ?: 'j_password'"/>
<label for="confirmPassword" class="sr-only">Confirm Password</label>
<input style="margin-bottom: 10px" type="password" id="confirmPassword" class="form-control" placeholder="Confirm Password" name="$passwordCParameter ?: 'confirmPassword'"/>
<g:actionSubmit style="margin-top: 20px" class="btn btn-lg btn-success btn-block" action="register" value="create"/>
<button style="margin-top: 20px" class="btn btn-lg btn-success btn-block" type="submit">Register</button>
</form>
</div>
</body>
这是控制器和操作,如果有帮助的话:
package com.mypackage
class HomeController
def springSecurityService
//many actions
def register()
switch(request.getMethod())
case 'POST':
withForm
if(params.username && params.email && params.fName && params.lName && params.password && params.confirmPassword)
if(params.password == params.confirmPassword)
def userRole = new Role(authority: 'ROLE_USER', name: 'User').save(flush: true)
def newUser = User.findByUsername(params.id)
if(!newUser)
newUser.username = params.username
newUser.email = params.email
newUser.fName = params.fName
newUser.lName = params.lName
newUser.password = params.password
newUser.enabled = true
newUser.accountExpired = false
newUser.accountLocked = false
newUser.passwordExpired = false
newUser.authorities ?: userRole
if(loginService.register(newUser))
flash.message = "Your account has been created. Welcome " + newUser.fName
flash.message_type = 'success'
redirect(controller: 'login', action: 'auth')
else
flash.message = "Unable to create your account. Please try again later"
flash.message_type = 'danger'
redirect(action: 'register')
println("saving user")
else
flash.message = "That username has already been taken. Please choose a new username."
flash.message_type = 'danger'
println("checking username")
else
flash.message = "Your passwords don't match. Please try again"
flash.message_type = 'danger'
println("matching passwords")
else
flash.message = "Please fill out all required fields"
flash.message_type = 'danger'
println("filling out fields")
break;
case 'GET':
break;
println("POSTing form")
这里也是 loginService 的代码:
com.mypackage
//imports
@Transactional
class LoginService
def register(newUser)
try
def userRole = new Role(authority: 'ROLE_USER', name: 'User').save(flush: true)
newUser.save(flush: true, failOnError: true)
UserRole.create(newUser, userRole, true)
return true
catch(all)
all.printStackTrace()
return false
如果所有代码没有正确缩进,我深表歉意,我基本上是从我的实际项目中复制并粘贴出来的。任何帮助将不胜感激,谢谢!
【问题讨论】:
【参考方案1】: 您可以使用 spring-security-ui 插件 使用验证密码的命令对象 使用资源,即向 /user 发帖,这将调用保存操作【讨论】:
以上是关于grails spring security自定义用户注册页面的主要内容,如果未能解决你的问题,请参考以下文章
如何在grails中自定义spring security插件登录页面
如何使用 grails 1.3.2 和插件 spring-security-core 1 实现自定义 FilterSecurityInterceptor?
grails spring security自定义用户注册页面
如何处理 grails spring-security-rest 插件中的自定义身份验证异常?