- grails create-app basicauthdemo
- cd basicauthdemo
- grails install-plugin spring-security-core
- grails s2-quickstart basicauthdemo User Role
- Edit grails-app/conf/Config.groovy and add two lines telling the Spring Security plugin to use HTTP basic authentication:
grails.plugins.springsecurity.useBasicAuth = true grails.plugins.springsecurity.basic.realmName = "HTTP Basic Auth Demo"
- Edit grails-app/conf/BootStrap.groovy to setup a user and role:
import basicauthdemo.* class BootStrap { def init = { servletContext -> def userRole = Role.findByAuthority("ROLE_USER") ?: new Role(authority: "ROLE_USER").save(flush: true) def user = User.findByUsername("tst") ?: new User(username: "tst", password: "foo", enabled: true).save(flush: true) UserRole.create(user, userRole, true) } def destroy = { } }
- grails create-controller hello
- Edit grails-app/controllers/basicauthdemo/HelloController.groovy and add a security annotation:
package basicauthdemo import grails.plugins.springsecurity.Secured class HelloController { @Secured(['ROLE_USER']) def index() { render "Hello World!" } }
- grails run-app
- Open http://localhost:8080/basicauthdemo/hello
Monday, December 31, 2012
HTTP basic authentication in Grails with Spring Security
Setting up HTTP basic authentication in Grails using Spring Security is pretty straightforward. Here's a quick how-to:
Subscribe to:
Post Comments (Atom)
HI can u post User and Role Classes.
ReplyDeleteThe User and Role classes are generated when you execute the "grails s2-quickstart basicauthdemo User Role" command.
ReplyDelete