I have a service that's responsible for creating the relevant account hierarchy when a registration page is successfully filled out :
def userRole = Role.findByAuthority("ROLE_USER")
final account = new Account(email: command.email, password: command.password)
account.save(flush: true)
final publisher = new Publisher(name: command.name) //, account: account)
publisher.save(flush: true)
final accountRole = AccountRole.create account, userRole
Regardless of what I do, the AccountRole.create account, userRole fails at this line:
static AccountRole create(Account account, Role role) {
def instance = new AccountRole(account: account, role: role)
instance.save() // throws NullPointerException
instance
}
I have tried new AccountRole(account: account, role: userRole), but this too fails when I manually call save. The Account and AccountRole are classes generated (and tweaked slightly) by the Grails 3 Spring Security plugin.
Here's the stacktrace for the AccountRole.create(...) call that fails:
java.lang.reflect.InvocationTargetException: null
at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62)
at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
at grails.plugin.springsecurity.web.filter.DebugFilter.invokeWithWrappedRequest(DebugFilter.groovy:102)
at grails.plugin.springsecurity.web.filter.DebugFilter.doFilter(DebugFilter.groovy:69)
at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62)
at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: null
at package.AccountRole.create(AccountRole.groovy:48)
at package.RegistrationService.$tt__handleNewRegistration(RegistrationService.groovy:25)
Version information:
| Grails Version: 3.1.5
| Groovy Version: 2.4.6
| JVM Version: 1.8.0_73
Please save me from my misery! I've spent hours on this now :(
There's not really enough info to be sure, but it's unlikely that instance is null since you just created it with a constructor call, but you should check that to be sure, e.g.
static AccountRole create(Account account, Role role) {
def instance = new AccountRole(account: account, role: role)
println "instance null? ${instance == null}"
instance.save() // throws NullPointerException
instance
}
It's more likely that a problem during validation is causing the exception, so my guess is that there's something wrong with either userRole or account. Check that userRole was correctly retrieved and that there aren't validation errors from the Account save() call:
def account = new Account(email: command.email, password: command.password)
account.save(flush: true)
if (account.hasErrors()) {
log.warn "Failed to save Account $account: $account.errors"
}
Related
I'm creating a Grails (3.3.9) plugin to hold shared back-end code for some internal applications. For some reason, when I run the plugin to test it, my service is not being injected into my controller.
I've started with the default web-plugin profile, created a single domain class called Entry, and run generate-all to create a controller, service, and views. When I try to run my plugin as an application and view a single domain instance, I get the following error:
Cannot invoke method get() on null object. Stacktrace follows:
java.lang.reflect.InvocationTargetException: null
at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Cannot invoke method get() on null object
at com.mycompany.internal.EntryController.show(EntryController.groovy:18)
... 14 common frames omitted
The stacktrace takes me to line 18 in my controller:
def show(Long id) {
respond entryService.get(id)
}
This suggests to me that entryService is null.
My domain class looks like this:
class Entry {
String entryCode
String description
}
The controller is as follows:
class EntryController {
EntryService entryService
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
respond entryService.list(params), model:[entryCount: entryService.count()]
}
def show(Long id) {
respond entryService.get(id)
}
//snipped for brevity
And the service looks like this:
#Service(Entry)
interface EntryService {
Entry get(Serializable id)
//snipped for brevity
}
Based on the Grails plugin documentation, I would expect to be able to run the plugin standalone like any other application, and in normal applications, defining the service as an interface works fine. If I install this plugin to my local maven cache and use it in an application, it works exactly as I would expect; I am able to hit the controller's show endpoint and get back a result from my database.
At one point I tried implementing the service as a class rather than having it be an interface, but then I receive this error:
URI
/entry/index
Class
java.lang.IllegalStateException
Message
null
Caused by
Either class [com.mycompany.internal.Entry] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
What am I missing about how to set up and run a Grails plugin correctly?
Finally found an answer by digging into the GORM issue: class Authority is not a domain class or GORM has not been initialized correctly or has already been shutdown
Apparently the root of the problem was the need to add compile "org.grails.plugins:hibernate5" to my dependencies block in build.gradle. The weird thing is that the web-plugin profile provides the create-domain-class command, so I would think GORM and Hibernate support would be included by default, but I'm apparently misunderstanding some aspects of how plugins are supposed to work.
I'm unable to get springSecurityService.currentUser from a controller in Grails 3.2.8.
Please note that Grails 3.2.8 made some performance improvements not in previous versions that seem to be breaking Spring Security Core. As per Grails 3.2.8 dependency injection in domain classes, I set grails.gorm.autowire = true, which fixed that incompatibility.
Steps to reproduce:
Create a new Grails 3.2.8 web app with Spring Security 3.1.1 using Roles and Groups.
In application.yml, set grails.gorm.autowire = true
In Bootstrap:
def init = { servletContext ->
User user = new User(username: 'foo', password:'foo').save(flush: true)
Role role = new Role(authority: 'SomeRole').save(flush: true)
RoleGroup roleGroup = new RoleGroup(name: 'SomeRole').save(flush: true)
RoleGroupRole.create(roleGroup, role)
UserRoleGroup.create(user, roleGroup)
}
Create a controller TestController:
#Secured(['SomeRole'])
class TestController {
def springSecurityService
def index() {
println "springSecurityService==null? ${(springSecurityService==null).toString()}"
println "SecurityContextHolder.context==null? ${(SecurityContextHolder.context==null).toString()}"
println "SecurityContextHolder.context?.authentication==null? ${(SecurityContextHolder.context?.authentication==null).toString()}"
println "springSecurityService.principal==null? ${(springSecurityService.principal==null).toString()}"
println "springSecurityService.currentUser==null? ${(springSecurityService.currentUser==null).toString()}"
render "1" //Render something so we don't get an exception.
}
}
Start up the server and go to /test. The output is:
springSecurityService==null? false
SecurityContextHolder.context==null? false
SecurityContextHolder.context?.authentication==null? true
springSecurityService.principal==null? true
springSecurityService.currentUser==null? true`
Is there a good workaround?
If SecurityContextHolder.context is not null (it should never be null) but
SecurityContextHolder.context?.authentication is null, then this is probably not a Grails or a plugin issue - you're not authenticated. So there's no principal to get the cached user id from (i.e. springSecurityService.principal is null as your output shows) and there's no way to retrieve the current User instance.
SOLVED. Since version 3.2.8 there is this default setting in application.yml: grails.gorm.autowire=false. You have to set it to true then the services autowire as always.
I´ve got some weird behaviour using Grails 2.2.4, SpringSecurityCore 2.0 RC in Production on a Tomcat 7 with Groovy Compiler 2.0.8, Java 1.7.0.
We build a plugin which realize basic behaviors. We included the SpringSecurityCore plugin in it with a person class named User. There are some domain classes in the basic plugin which got User fields.
Then we build an application which include our basic plugin. The application got a special application person class called RUser which extend from User. RUser is set to SpringSecurityCore as person class.
Problem:
The application runs about 1 to 3 days without errors, but then ClassCastExceptions are thrown trying to set a RUser object on a User field of a plugin domain class.
User classes:
Plugin User class:
class User implements Serializable {
transient springSecurityService
String username
String password
...
}
Application User class:
class RUser extends User implements Serializable{
Cart cart
boolean businessUser = false
boolean taxIncluded = false
...
}
Application Config.groovy:
grails.plugin.springsecurity.userLookup.userDomainClassName = 'RUser'
Participated classes of our basic plugin:
class UserRole implements Serializable {
User user
Role role
static UserRole create(User user, Role role, boolean flush = false) {
new UserRole(user: user, role: role).save(flush: flush, insert: true)
}
...
}
class UserHistory implements Serializable{
User user
List historyEntries
static hasMany = [historyEntries:HistoryEntry]
...
}
The errors occur in this example situations:
In a Filter:
54: UserHistory.withTransaction{
def currentUser =springSecurityService.currentUser
def history = UserHistory.findByUser(currentUser)
if(!history){
96: history = new UserHistory(user:user).save(flush:true)
}
}
2014-05-23 13:52:17,279 ERROR errors.GrailsExceptionResolver| IllegalArgumentException occurred when processing request: [GET] /index
java.lang.ClassCastException#7ff92284. Stacktrace follows:
java.lang.reflect.UndeclaredThrowableException
at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:687)
at de.neomatt.history.HistoryFilters$_closure1_closure2_closure4.doCall(HistoryFilters.groovy:54)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.reflect.InvocationTargetException
at de.neomatt.history.HistoryFilters$_closure1_closure2_closure4_closure5.doCall(HistoryFilters.groovy:96)
... 10 more
Caused by: org.springframework.beans.MethodInvocationException: Property 'springSecurityService' threw exception; nested exception is java.lang.IllegalArgumentException: java.lang.ClassCastException#7ff92284
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
... 11 more
Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException#7ff92284
... 13 more
2014-05-23 13:52:17,288 ERROR errors.GrailsExceptionResolver| IllegalArgumentException occurred when processing request: [GET] /reservation_demo/ToolsOne
Method name must not be null. Stacktrace follows:
java.lang.IllegalArgumentException: Method name must not be null
at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.retrieveAction(ProxyAwareMixedGrailsControllerHelper.java:41)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
In a Controller:
RegistrationCode.withTransaction {
def user = lookupUserClass().findByUsername(registrationCode.username)
def role = Role.findByAuthority(roleName)
281: UserRole.create user, role
}
2014-05-26 13:02:24,780 ERROR errors.GrailsExceptionResolver| IllegalArgumentException occurred when processing request: [GET] /app/register/verifyRegistration
java.lang.ClassCastException#a7c83d. Stacktrace follows:
java.lang.IllegalArgumentException: java.lang.ClassCastException#a7c83d
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at de.neomatt.security.UserRole.create(UserRole.groovy:32)
at de.neomatt.security.RegisterController$_closure4_closure14.doCall(RegisterController.groovy:281)
at org.grails.datastore.gorm.GormStaticApi.withTransaction(GormStaticApi.groovy:687)
at de.neomatt.security.RegisterController$_closure4.doCall(RegisterController.groovy:271)
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53)
at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
The ClassCastExceptions occurs everywhere, where an object with type RUser is setted on a User field. The RUser object is retrieved from springSecurityService.currentUser or with the lookupUserClass().findBy(...) method.
protected Class<?> lookupUserClass() {
grailsApplication.getDomainClass(lookupUserClassName()).clazz
}
As mentioned before, the application runs without errors for a undefined period of time in a production environment (no reloading, no config changes). A new deployment of the same war solves the problem for the next few hours/days.
Any suggestions?
#neomatt
just trying to persist a relationship between GORM entities, and an overflow error appends on mapping during save.
1) I create a manyTomany relationship between User and DataStore:
User entity:
...
static belongsTo = DataStore
static hasMany = [groups: Groups,dataStore:DataStore]
Profile profile
Contacts contact
DataStore dataStore
...
DataStore entity:
...
static belongsTo = [service:Service]
static hasMany = [users:User]
Service service
List<User> users
...
2) Calling the service from a controller to save datas:
UserRole.create user, roleCustomer, true
UserRole.create user, roleAdmin, true
dataStoreService.createDS('ds',profile.service,user)
3) Service logic:
#Transactional
def createDS(ds,service,user) {
def key = service.domainkey
if (user && key) {
DataStore ds = new DataStore(ds:ds)
ds.validate() ? ds.save(flus:true) : ds.errors.allErrors.println()
ds.addToUsers(user).save(flush:true)
service.addToDataStore(ds).save(flush:true)
user.setDataStore(ds)
...}
4) The weird error i need to solve:
Stacktrace follows:
org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Executing action [sendOrder] of controller [$$.StoreController] caused exception: Runtime error executing action
at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195)
at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63)
at net.bull.javamelody.JspWrapper.invoke(JspWrapper.java:149)
at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:259)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:202)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:175)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
Caused by: org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Runtime error executing action
... 9 more
Caused by: java.lang.reflect.InvocationTargetException
... 9 more
Caused by: java.lang.StackOverflowError
at org.apache.commons.validator.EmailValidator.stripComments(EmailValidator.java:246)
at org.apache.commons.validator.EmailValidator.isValid(EmailValidator.java:95)
Any help please?
EDIT
After looking again at the stack trace the problem has nothing to do with Melody (maybe? maybe not?) but let us know what was wrong).
Check the email reference of the User (Not sure in which sub-domain class it is located, maybe contact class? print it or use a debugger to inspect the domain values and then double-check your constraints) . If the email issue is not related to your domain classes, then double-check again melody...
org.apache.commons.validator.EmailValidator.isValid(EmailValidator.java:95)....
-- Still double-check what Melody is doing.
If the problem was related to Melody, please let us know, just for personal information. I've seen so many random issues from people using that plugin, it looks useful but buggy.
You have the variable dataStore defined twice: once in your hasMany and once as a straight reference.
Try:
User entity:
...
static belongsTo = [datastore: DataStore]
static hasMany = [groups: Groups,dataStores:DataStore]
Profile profile
Contacts contact
...
just redone my model following hibernate doc:
class User implements Serializable {
static hasMany = [dataStores:DataStore,contacts:Contacts,groups: Groups]
Profile profile
Contacts contacts
List<DataStore> dataStores
with contraint: dataStores nullable:true
And DS entity:
class DataStore implements Serializable{
static hasMany = [users:User,contacts:Contacts]
static belongsTo = [service:Service]
Service service
User users
removes definetely the several overflows and keeps the model reliable.
I have HttpSessionListener to listen for when sessions are created and destroyed. I have a user domain which has loggedIn boolean column which I update whenever user logs in or logs out which I use for admin management. I also store the session Id in the database.
I also want to update the loggedIn column whenever the session is destroyed. Below is the code written in the sessionDestroyed method.
def user = User.findByUserSessionId(session.getId())
if(user) {
user.setLoggedIn(false)
user.setUserSessionId("SESSION DESTROYED")
user.save(flush: true)
}
The problem is the user table never gets updated.
Below is the error reported in log file:
[2010-10-16 11:45:07.781] ERROR core.ContainerBase.[Tomcat].[localhost].[/SAMPLE] Session event listener threw exception
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)
at org.codehaus.groovy.grails.orm.hibernate.validation.HibernateDomainClassValidator.validate(HibernateDomainClassValidator.java:66)
at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractSavePersistentMethod.doInvokeInternal(AbstractSavePersistentMethod.java:129)
at org.codehaus.groovy.grails.orm.hibernate.metaclass.AbstractDynamicPersistentMethod.invoke(AbstractDynamicPersistentMethod.java:59)
at sun.reflect.GeneratedMethodAccessor500.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:188)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:132)
at org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport$_addBasicPersistenceMethods_closure71.doCall(HibernatePluginSupport.groovy:812)
Can I know how the proper way to update the user table when session is destroyed.
Thank You.
Jay Chandran.
try
User.withTransaction{ txStatus -> .... }
or
User.withSession{ session - > .... }
or perhaps inject a service that does what you need it to do, as service methods should have transactions by default.
edit -- usually I don't go this far, but Im in a good mood today...something like the following should work. You should really read the grails documentation or buy a book...
User.withTransaction( txStatus ->
def user = User.findByUserSessionId(session.getId())
if(user) {
user.setLoggedIn(false)
user.setUserSessionId("SESSION DESTROYED")
user.save(flush: true)
}
}