Programmatically logging in user in jsecurity plugin

04 / Mar / 2010 by Bhagwat Kumar 0 comments

In our current project we are using jsecurity plugin and some of our bootstrap code required a user to be logged in. I found a nice blog http://www.tothenew.com/blog/?p=335 on this topic but it was using groovy metaprogramming to override the normal behaviour of SecurityUtils.getSubject() which is suitable for test environment.

After a few minutes of digging into jsecurity plugin we found an easy solution :-

class MyController{
  def jsecSecurityManager

  def myAction={
        def authToken = new org.jsecurity.authc.UsernamePasswordToken("bootstrapuser", "password")
        jsecSecurityManager.login(authToken)
	
	/*
	Your code here that depends on logged in user.
	e.g. String userId=org.jsecurity.SecurityUtils.getSubject()?.getPrincipal()
	*/

   }
}

It worked for us.Hope it will save your time too.

~~~~Bhagwt Kumar~~~~
bhagwat@intelligrape.com
IntelliGrape Software

FOUND THIS USEFUL? SHARE IT

Leave a comment -