Grails Authenticationã¨EmailConfirmationãã©ã°ã¤ã³ã§Railsã®Deviseã¨åãæ©è½ãå®è£ ãã
Javaã®å®è¡ç°å¢ã§ãRuby on Railsã®ãã©ã°ã¤ã³Deviseã®ããã«ã¡ã¼ã«èªè¨¼ä»ãã®ãã°ã¤ã³æ©æ§ãç°¡åã«å®è£ ã§ããªãããªãã¨æã調ã¹ã¦ã¿ã¾ããã
ã¾ããJavaå®è¡ç°å¢ã§åãRoRã¹ã¿ã¤ã«ã®ãã¬ã¼ã ã¯ã¼ã¯ã¯ãæè¿ã®ã¡ã¸ã£ã¼ã©ããã¯ä»¥ä¸ã®ãããªã¨ãããã¨æãã¾ãã
- JRuby on Rails: JRubyã®RoR
- Play! framework: scalaã«å¯¾å¿ããã®ã§æè¿äººæ°
- Grails: Javaã§åãLL ã§ã¯èèã®Groovyã§éçºããRoRã¹ã¿ã¤ã«ã®ãã¬ã¼ã ã¯ã¼ã¯
ä¸è¨3ã¤ãç°¡åã«èª¿ã¹ãå°è±¡ã§ãããã¾ãJRubyã¯ææ°çã§ã¯Deviseããããªãåãã¦ããã¾ããã§ããã
次ã«ãPlay! frameworkã§ããã軽ããã調ã¹ã¦ãªãã§ããã¡ã¼ã«ã®ã³ã³ãã¡ã¼ã ã¾ã§ã§ãããã©ã°ã¤ã³ãè¦ã¤ããã¾ããã§ãããã¾ã ãã©ã°ã¤ã³ãªã©ã¯å
å®ãã¦ããªãå°è±¡ãåããã®ã§ä»åã¯èª¿æ»ãè¦éãã
ããã§ãæããå¨ãGroovyã®Railsã§ããGrailsãè¦ã¦ã¿ããç®çã®ãã©ã°ã¤ã³ãè¦ã¤ãã£ãã®ã§ãGrailsã®ã¤ã³ã¹ãã¼ã«ããã¡ã¼ã«ã®ã³ã³ãã¡ã¼ã ãçµã¿è¾¼ãã èªè¨¼æ©æ§ãå®è£ ããæé ãã¡ã¢ãã¦ããã¾ãã
Grailsã¨ã¯
Grailsã¯æåã®ãã¼ã¸ã§ã³ã2006å¹´ã«ãªãªã¼ã¹ãããRoR Likeãªãã¬ã¼ã ã¯ã¼ã¯ã§ãéçºè¨èªã¯Javaã§åãã¹ã¯ãªããè¨èªã®å
ç¥ã§ããGroovyã使ãã¾ããGã¯Groovyã®Gã§ãã
ä»ã¯SpringSourceã®ããã¸ã§ã¯ãã«ããªããVMWareãããã¯ã«å±
ãããå°æ¥ã®ã¡ã³ããã³ã¹ã«ã¤ãã¦ãå®å¿ãã¦ä½¿ããã¨ãã§ãããã§ãã
ããã¥ã«ãããªãå
å®ãã¦ãã¾ãã
http://grails.org/doc/latest/guide/
Grailsã®ã¤ã³ã¹ãã¼ã«
ã¤ã³ã¹ãã¼ã«ã¯ãhttp://grails.org/Installationã«æ¸ãã¦ããéãã解åãã¦ãã¹ãéãã ãã§ãã
以ä¸ãMac OS X 10.7.2ã®å ´åã®æé ãã¡ã¢ãã¦ããã¾ãã
http://grails.org/Downloadããgrails-2.0.0.zipããã¦ã³ãã¼ã
解åãã¦ãbinããã¹ã«éãã解åãããã£ã¬ã¯ããªãGRAILS_HOMEã¨ãã¦ç°å¢å¤æ°ã«è¨å®ããã
ã¾ããJAVA_HOMEãå¿
è¦ã§ãã
grailsã¯/optã®ä¸ã«è§£åããã®ã§ã以ä¸ã®ããã«.bash_profileã®è¨å®ãã¾ããã
JAVA_HOME=/usr/bin/java GRAILS_HOME=/opt/grails-2.0.0 PATH=$GRAILS_HOME/bin:$PATH
è¨å®ãé©ç¨
$ source .bash_profile
ã¢ããªã±ã¼ã·ã§ã³ã®çæã¨åä½ç¢ºèª
é©å½ãªworkspaceã«ç§»åãã¦ã以ä¸ã®ã³ãã³ãããããã¾ãã(rails newã«å½ããã¾ã)
$ grails create-app my-project
jarã®ãã¦ã³ãã¼ããªã©å°ã æéããããã¾ãã
$ cd my-project $ grails run-app
Downloading: resources-1.1.5.zip |
run-appããã¨VMWareã«æ å ±ãéã£ã¦ãããï¼ã¨èããã¾ãããåé¡ç¡ããããªã®ã§yãé¸æã
ãã©ã¦ã¶ã§localhost:8080/my-projectã表示ããã¨ä»¥ä¸ã®ãããªããã©ã«ãã®ãã¼ã¸ã表示ããã¾ãã
scaffold
ãã¡ã¤ã³ã¢ãã«ã®çæ
$ grails create-domain-class org.example.Book Created file grails-app/domain/org/example/Book.groovy Created file test/unit/org/example/BookTests.groovy
Book.groovyã以ä¸ã®ããã«ç·¨éãã¾ã
package org.example class Book { String title String author static constraints = { title(blank: false) author(blank: false) } }
ã³ã³ããã¼ã©ã¼ã®çæ
$ grails create-controller org.example.Book Created file grails-app/controllers/org/example/BookController.groovy Created file grails-app/views/book Created file test/unit/org/example/BookControllerTests.groovy
package org.example class BookController { //def index {} def scaffold = Book // Note the capital "B" }
$ grails run-app
Authenticationãã©ã°ã¤ã³ã§èªè¨¼ãçµã¿è¾¼ã
å®è£ ãããµã¤ã³ãããããã®ç»é¢é·ç§»ã確èªãã¦ããã¾ãã
- ãµã¤ã³ããããé¢ã表示
- ç»é²å¾ã確èªã¡ã¼ã«ãéä¿¡
- ã¡ã¼ã«ã®ãªã³ã¯ãã¯ãªãã¯
- 確èªå¾æå®ãããã¢ã¯ã·ã§ã³ã«é·ç§»
ã¾ããåºæ¬çãªèªè¨¼æ©æ§ãæä¾ããAuthenticationãã©ã°ã¤ã³(http://grails.org/plugin/authentication)ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ grails install-plugin authentication
ãã£ã«ã¿ã¼ãè¨å®
Grailsã®ãã£ã«ã¿ã¼(ãµã¼ãã¬ãããã£ã«ã¿ã¼)ã¯ä»¥ä¸ã«è§£èª¬ãããã¾ãã
http://grails.org/doc/latest/guide/theWebLayer.html#6.6 Filters
ãã©ã°ã¤ã³ã®ããã¥ã¡ã³ãã®éãã以ä¸ã®ããã«ãã£ã«ã¿ã¼ãè¨å®ãã¾ãã
$ vi grails-app/conf/AuthenticationFilters.groovy
class AuthenticationFilters { static nonAuthenticatedActions = [ [controller:'authentication', action:'*'] ] def filters = { accessFilter(controller:'*', action:'*') { before = { boolean needsAuth = !nonAuthenticatedActions.find { (it.controller == controllerName) && *1 } if (needsAuth) { return applicationContext.authenticationService.filterRequest( request, response, "${request.contextPath}/authentication/index" ) } else return true } } } }
次ã«ããµã¤ã³ãããå¾ç¢ºèªã¡ã¼ã«ãéä¿¡ããä»çµã¿ãçµã¿è¾¼ãã§è¡ãã¾ãã
ã¡ã¼ã«ç¢ºèªã®ä»çµã¿ã¯Email Confirmationãã©ã°ã¤ã³(http://grails.org/plugin/email-confirmation)ãå©ç¨ãã¾ãã
Email Confirmationã¯mailãã©ã°ã¤ã³ã¨quartzãã©ã°ã¤ã³ã«ä¾åãã¦ããã®ã§ãããããã¤ã³ã¹ãã¼ã«ãã¾ãã
$ grails install-plugin mail $ grails install-plugin quartz $ grails install-plugin email-confirmation
ã¡ã¼ã«éä¿¡è¨å®ããhttp://grails.org/plugin/mailã®Configurationãåèã«Config.groovyã«è¿½å ãã¾ã
$ vi grails-app/Config.groovy
ãã¡ã¤ã«ã®æ«å°¾ã«ä»¥ä¸ã追å
grails { mail { host = "smtp.gmail.com" port = 465 username = "youracount@gmail.com" password = "yourpassword" props = ["mail.smtp.auth":"true", "mail.smtp.socketFactory.port":"465", "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory", "mail.smtp.socketFactory.fallback":"false"] } }
Email Confirmationã®ã³ã³ããã¼ã©ã¼ãèªè¨¼ããé¤å¤ããããã«ãã£ã«ã¿ã¼ãè¨å®ãã¾ãã
class AuthenticationFilters {
static nonAuthenticatedActions = [
[controller:'authentication', action:'*'],
[controller:'emailConfirmation', action:'*']
]
...
}
次ã«ãAuthenticationãã©ã°ã¤ã³ã®onSignupã¤ãã³ãã使ã£ã¦ããµã¤ã³ã¢ããå¾ã«å ¥åãããã¡ã¼ã«ã¢ãã¬ã¹ã«å¯¾ãã¦ç¢ºèªã¡ã¼ã«ãéä¿¡ããè¨å®ããã¦ããã¾ãã
$ vi grails-app/conf/BootStrap.groovy
import org.springframework.web.context.support.WebApplicationContextUtils class BootStrap { def emailConfirmationService def init = { servletContext -> def appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) // Emailã«ãã確èªãæå¹ã«ãã(1) appCtx.authenticationService.events.onConfirmAccount= { user -> return true } // ãµã¤ã³ãããå¾ã«å¼ã°ããããªã¬ã¼(2) appCtx.authenticationService.events.onSignup = { user -> // user.paramsã«ã¯ãµã¤ã³ãããã§ãã¹ãããããã©ã¡ã¼ã¿ãå«ã¾ã // sendConfirmationã®ç¬¬ï¼å¼æ°ã¯ã以ä¸ã®onConfirmationã®uidã«æ¸¡ã(3) emailConfirmationService.sendConfirmation(user.params.email, "Please confirm", [from:"hrendoh@gmail.com"], user.params.login) } // 確èªã¡ã¼ã«ã®ãªã³ã¯ãã¯ãªãã¯ãã¦ããã¼ã¯ã³ã確èªããå¾å¼ã°ãã emailConfirmationService.onConfirmation = { email, uid -> log.info("User with id $uid has confirmed their email address $email") appCtx.authenticationService.confirmUser(uid) return [controller:'book', action:'index'] } emailConfirmationService.onInvalid = { uid -> log.warn("User with id $uid failed to confirm email address after 30 days") } emailConfirmationService.onTimeout = { email, uid -> log.warn("User with id $uid failed to confirm email address after 30 days") } } def destroy = { } }
åãã©ã°ã¤ã³ã®ããã¥ã¢ã«ãããã¤ã³ãã®ã¿æç²ãã¾ãã
(1) Emailã«ãã確èªãæå¹ã«ãã
Called to see if email confirmation is required, return true if user cannot log in yet until confirmed
onConfirmAccount: { user -> }
(http://grails.org/plugin/authentication)
(2) ãµã¤ã³ãããå¾ã«å¼ã°ããããªã¬ã¼
Called on successful signup, although email may not be confirmed yet - params are the request (form) params
onSignup: { params -> }
(http://grails.org/plugin/authentication)
(3) sendConfirmationã«uidã渡ã
def sendConfirmation(String emailAddress, String theSubject, Map model = null, String userToken = null)
userToken - a string that your application can use to tie up this request to your own data. This is passed back to your application in onConfirmation and onTimeout events, along with the email address.
(http://grails.org/plugin/email-confirmation)
以ä¸ã§ãç¬èªã®ã¦ã¼ã¶ã¢ã«ã¦ã³ãã§èªè¨¼ãããWebã¢ããªã®ã²ãªå½¢ãåºæ¥ä¸ããã¾ãã
å¾ã¯ã以ä¸ã®ãããªé ç®ãå¿ è¦ã¨ãªãã¾ãããä»åã¯ããã¾ã§
- ãã¹ã¯ã¼ããªã»ãã
- ãã°ã¤ã³IDãDeviseã¨åãããã«Emailã«ãã
- Spring Securityãªã©ã¨ã®é£æº
ããã ãã§ãRailsã®Deviseã¨åãæ©è½ãã»ã¼å®è£ ã§ãã¦ããã®ã§ãå®è¡ç°å¢ãJavaãããªãã¨ã ããªå ´åã«ã¯ããªãéå®ããããªæ°ããã¾ãã
*1:it.action == '*') || (it.action == actionName