{"id":429,"date":"2010-03-04T13:04:09","date_gmt":"2010-03-04T07:34:09","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=429"},"modified":"2010-06-09T15:47:40","modified_gmt":"2010-06-09T10:17:40","slug":"programmatically-logging-in-user-in-jsecurity-plugin","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/programmatically-logging-in-user-in-jsecurity-plugin\/","title":{"rendered":"Programmatically logging in user in jsecurity plugin"},"content":{"rendered":"<p>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   <a href=\"http:\/\/www.tothenew.com\/blog\/?p=335\" target=\"_blank\"> http:\/\/www.tothenew.com\/blog\/?p=335<\/a>  on this topic but it was using groovy metaprogramming to override the normal behaviour of <code>SecurityUtils.getSubject()<\/code> which is suitable for <em>test environment<\/em>.<\/p>\n<p> After a few minutes of digging into jsecurity plugin we found an easy solution :-<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"groovy\">class MyController{\r\n  def jsecSecurityManager\r\n\r\n  def myAction={\r\n        def authToken = new org.jsecurity.authc.UsernamePasswordToken(\"bootstrapuser\", \"password\")\r\n        jsecSecurityManager.login(authToken)\r\n\t\r\n\t\/*\r\n\tYour code here that depends on logged in user.\r\n\te.g. String userId=org.jsecurity.SecurityUtils.getSubject()?.getPrincipal()\r\n\t*\/\r\n\r\n   }\r\n}<\/pre>\n<\/div>\n<\/blockquote>\n<p>It worked for us.Hope it will save your time too.<\/p>\n<p> ~~~~Bhagwt Kumar~~~~<br \/>\nbhagwat@intelligrape.com<br \/>\nIntelliGrape Software<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[178,177,156],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/429"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=429"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/429\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}