Grails用户验证及验证码
下面是控制器
// def scaffold = Sipaccount //拦截器 def beforeInterceptor = [action:this.&auth, except:["index","list","login","authenticate","logout"]] //验证码插件 def jcaptchaService def auth(){ if(!session.user){ //保留认证前的页面 session.originReqParams=params redirect(controller:"sipaccount",action:"login") return false } } //认证页面 def login = {} def authenticate = { //先检查验证码 if (jcaptchaService.validateResponse("imageCaptcha", session.id, params.captchaResponse)) { //验证用户 def user = Sipaccount.findByUser_nameAndPassword(params.login, params.password) if(user){ session.user = user flash.message = "Hello ${user.user_name}!" // redirect(controller:"sipaccount", action:"list") //回到认证前页面 redirect(session.originReqParams) }else{ //用户验证错误 flash.message = "Sorry, ${params.login}. Please try again." redirect(action:"login") } }else{ //验证码错误 flash.message = "对不起,验证码错误,请重试!" redirect(action:"login") } } def logout = { flash.message = "Goodbye ${session.user.name}" session.user = null redirect(controller:"Sipaccount", action:"list") }
login.gsp
<%@ page import="sipaccount.Sipaccount" %> <!DOCTYPE html> <html> <head> <meta name="layout" content="main" /> <title>Login</title> <g:javascript> <!--刷新验证码函数--> function changePic(){ <!--$("#imageCaptcha").attr("src","/sipaccount/jcaptcha/jpeg/imageCaptcha?id="+new Date()); --> document.getElementById("imageCaptcha").src="/sipaccount/jcaptcha/jpeg/imageCaptcha?id="+new Date() } </g:javascript> </head> <body> <div> <h1>Login</h1> <g:if test="${flash.message}"> <div>${flash.message}</div> </g:if> <g:form action="authenticate" method="post" > <div> <table> <tbody> <tr> <td> <label for="login">Login:</label> </td> <td> <input type="text" id="login" name="login"/> </td> </tr> <tr> <td> <label for="password">Password:</label> </td> <td> <input type="password" id="password" name="password"/> </td> </tr> <tr> <td> <label for="captcha"></label> </td> <td> <!--显示验证码--> <img id="imageCaptcha" src="/sipaccount/jcaptcha/jpeg/imageCaptcha?id=${new Date()}"/> <a href="javascript:changePic();">看不清,换一张</a> <!--<jcaptcha:jpeg name="imageCaptcha"/>--> </td> </tr> <tr> <td> <label for="captchaResponse">验证码:</label> </td> <td> <input type="text" id="captchaResponse" name="captchaResponse"/> </td> </tr> </tbody> </table> </div> <div> <span> <input type="submit" value="Login" /> </span> </div> </g:form> </div> </body> </html>
验证码插件安装:
grails install-plugin jcaptcha
Config.groovy的配置:
最后加上下面的内容
import java.awt.Font import java.awt.Color import com.octo.captcha.service.multitype.GenericManageableCaptchaService import com.octo.captcha.engine.GenericCaptchaEngine import com.octo.captcha.image.gimpy.GimpyFactory import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator import com.octo.captcha.component.image.color.SingleColorGenerator import com.octo.captcha.component.image.textpaster.NonLinearTextPaster import com.octo.captcha.service.sound.DefaultManageableSoundCaptchaService jcaptchas { imageCaptcha = new GenericManageableCaptchaService( new GenericCaptchaEngine( new GimpyFactory( new RandomWordGenerator( "abcdefghijklmnopqrstuvwxyz1234567890" ), new ComposedWordToImage( new RandomFontGenerator( 20, // min font size 30, // max font size [new Font("Arial", 0, 10)] as Font[] ), new GradientBackgroundGenerator( 140, // width 35, // height new SingleColorGenerator(new Color(0, 60, 0)), new SingleColorGenerator(new Color(20, 20, 20)) ), new NonLinearTextPaster( 6, // minimal length of text 6, // maximal length of text new Color(0, 255, 0) ) ) ) ), 180, // minGuarantedStorageDelayInSeconds 180000 // maxCaptchaStoreSize ) // soundCaptcha = new DefaultManageableSoundCaptchaService() }
URL Mapping
class UrlMappings { static mappings = { "/$controller/$action?/$id?"{ constraints { // apply constraints here } } // "/"(view:"/index") //将"/"映射到对应的页面 "/"(controller:"Sipaccount",action:"list") "500"(view:'/error') } }
启动时检查是否有超级用户,没有的话创建:
BootStrap.groovy
class UrlMappings { import sipaccount.Sipaccount class BootStrap { def init = { servletContext -> if(!Sipaccount.findByUser_name('admin')){ new Sipaccount(user_name:'admin',sip_id:'admin',password:'12345678',user_level:0).save() } } def destroy = { } }