30 September 2013

Grails - Setting Up Spring Security

This is pretty quick and Simple....

If you  have a new grails project, you may not need to do Step 2

Step 1: edit BuildConfig.groovy and in the plugins closure, add compile ':spring-security-core:1.2.7.3'

Step 2: if you have an existing project you may need to add this: grails.project.dependency.resolver = "maven" above the grails.project.dependency.resolution closure

Step 3: compile

Step 4: run this grails command on the project: s2-quickstart [your package] User Role

Step 5: Add a user to the bootstrap by editing your BootStrap.groovy file and adding after servletContext ->
def eventOwnerRole = new Role(authority: 'ROLE_EVENT_OWNER').save(flush:true)
def userRole = new Role(authority: 'ROLE_USER').save(flush:true)
def testUser=new User(username: 'me', enabled: true, password: 'password')
testUser.save(flush:true)

UserRole.create testUser, userRole, true

Step 6: in one of your controllers, add:
import [the package your user/role classes were put].Role
import [the package your user/role classes were put].User
import [the package your user/role classes were put].UserRole

Step 7: Edit an existing controller and add
import grails.plugins.springsecurity.Secured

@Secured(['ROLE_USER'])

Run the app... go to the controller... it should now prompt for user/pass...

No comments:

Post a Comment