Grails spring bean in not loaded (custom UserDetailsService) - grails

I use custom implementation for Spring Security Plugin to override a default loadUser function. I read this manual https://grails-plugins.github.io/grails-spring-security-core/guide/userDetailsService.html and this is my resources.groovy file:
package spring
import security.ExtendedUserDetails
beans = {
UserDetailsService(ExtendedUserDetails)
}
but it was didn't loaded. App still use a default puligin implementation GormUserDetailsService. I use debugger and I see that ExtendedUserDetails never run.
So what's wrong?
This question is unanswered yet
Custom UserDetailsService Not Being Called - Grails & Spring Security Core

Looks like there is a typo in your bean name in resources.groovy. The name must be userDetailsService not UserDetailsService (capital U) and ExtendedUserDetails must implement GrailsUserDetailsService.

Related

HK2 Dependency Injection for DeltaSpike Data or Spring Data JPA

I'm developing Jersey-based RESTful Web Services. And, I'm choosing between DeltaSpike Data and Spring Data JPA for my repository layer. I have tried both of them. I'm amazed that they are almost the same.
DeltaSpike Data:
public interface AuthorRepository extends EntityRepository<Author, Long> {
}
Spring Data JPA:
public interface AuthorRepository extends CrudRepository<Author, Long> {
}
But my problem is not as to which one is better and I should choose but, how to apply HK2 dependency injection.
By manually creating AuthorRepository and AuthorRepositoryImpl, I can simply do this configuration:
public class ApplicationBinder extends AbstractBinder {
#Override
protected void configure() {
bind(AuthorRepositoryImpl.class).to(AuthorRepository.class).in(RequestScoped.class);
}
}
But I could not figure out how to apply above similar configuration if I use either DeltaSpike Data or Spring Data JPA since there is not an implementation class for repository interface.
Any help will be appreciated. Thank you.
Personally I would go with Spring Data, as Jersey/HK2 already has an integration module for Spring. This will allow you to inject any Spring beans into your Jersey resources. And the Spring Data repository being a Spring bean, the injection works seamlessly; no need to configure anything with HK2/Jersey. All you would need to configure is the Data configuration for Spring. To get it working, you need to take the following steps:
1) Add the jersey-spring dependency.
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey2.version}</version>
</dependency>
This will give you the Spring/Jersey integration, allowing you to inject your Spring beans into your Jersey components. See also Combining Spring project and Jersey. It shows some different examples of using both Java configuration an XML configuration.
2) Configure the Spring/Data beans.
This will be your normal configuration, assuming you have done Data configuration with Spring before. This will consist of configuring the JPA vendor, transaction manager, and data source.
3) Inject your repository into your Jersey resource and enjoy.
You can find a complete example in this GitHub Repo

#Post Filter is not filtering the method returned collection using acl and oauth spring security

I m trying to integrate both Oauth security and acl spring security.
Instead of below oauth expression handler
<sec:global-method-security pre-post-annotations="enabled" proxy-target- class="true">
<sec:expression-handler ref="oauthExpressionHandler" />
</sec:global-method-security>
I used the acl expression handler following configuration
as explained in http://krams915.blogspot.in/2011/01/spring-security-3-full-acl-tutorial_30.html.
I am able to make acl entries in table.But while using the #PostFilter the objects returned by the method are not getting filtered using acl permission.
Can some one please help
In my configuration I made two mistakes which made #PostFilter inactive.
As told by Denim in the above comments I loaded component scan twice once by dispatcher servlet and again i loaded dispatcher servlet xml using context loader listener.With this change i was able to detect the annotation in the package where i declared the context i,e webapplication module.but in my service module the annotation was not being detected.
2 the issue in my service layer is
I had my service class as below and applied annotation as below
#Service("a")
#Transactional
Class A{
public List<Users> getUsers() {
getNames();
}
#PostFilter("hasPermission(filterObject,'edit')")
public List<Users> getNames() {
}
The annotation will not be considered as both the methods will be in the same proxy can refer
the following url
Spring AOP not working for method call inside another method

NotSerializableException on ViewScoped ManagedBean with CDI Injected properties

I'm trying to create a scalable JSF application.
I would like to save view states on client side but I have troubles with ViewScoped ManagedBean with CDI Injected attributes.
SomeService.java :
#Singleton
public class SomeService {
// ...
}
SomeBean.java
#ManagedBean
#ViewScoped
public class SomeBean implements Serializable {
#Inject
private SomeService someService;
}
Unfortunately glassfish fails to serialize someService which I don't want to be serializabled but re-injected.
I tried to make it transient which ends up to a NullPointerException when accessing someService after de-serialization.
What shall I do?
I'm aware that I could use CDI ViewScoped with Seam Faces or CODI but I want to minimize at most dependencies.
I could also wait for JEE7 which will provide #ViewScoped for CDI but we won't be using JEE7 before months.
UPDATE :
I just wanted to add that I was using embedded EJB bundled in a jar which is itself linked to my war.
NotSerializableException's stack trace has the following message :
com.company.core.service.__EJB31_Generated__SomeService__Intf____Bean__
I don't like to self respond to my own questions but after some more research I found that it was a bug in Mojarra 2.1.6 (I'm using Glassfish 3.1.2.2) which is now solved in Mojarra 2.1.20.
To update Mojarra you just need to download a fresher version (eg: https://maven.java.net/content/repositories/releases/org/glassfish/javax.faces/2.1.20/javax.faces-2.1.20.jar) and place it in the $GLASSFISH/modules directory as javax.faces.jar.

How to access a local interfaces from one project into another within the same ear project

I have a web project that has FacesValidator, this validator needs to access an EJB service to verify if a record exists. Unfortunately, I cannot inject my enterprise beans since the validator is not a managed-bean, so I'm trying to access it via InitialContext. I've tried different combination from http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html but failed.
What works is this format:
java:global/myProject-ear-1.0.0/myProject/MyService!com.czetsuya.myProject.service.membership.MyService,
My question is can it be simplify? Seems too long.
Thanks,
czetsuya
Look at the server logs. A bit decent EJB container (at least, Glassfish 3 and JBoss 6/7 do), logs all available JNDI names of the EJB during EJB deployment step. Provided that the validator is properly been put in the WAR and the EJB has a #Local interface, then the shortest JNDI name would be the java:app one which should in your case have been java:app/myProject/MyService.
A completely different alternative is to just make the validator a JSF or CDI managed bean instead, so that you can just use the #EJB annotation.
#ManagedBean // Or #Named.
#ApplicationScoped // Provided that the instance doesn't have any state.
public class MyValidator implements Validator {
#EJB
private MyService myService;
// ...
}
and reference it by binding instead of validatorId:
<f:validator binding="#{myValidator}" />
Note that from JSF 2.2 on, you should be able to inject #EJB in a #FacesValidator (and #FacesConverter).
See also:
How to inject in #FacesValidator with #EJB, #PersistenceContext, #Inject, #Autowired

Spring Webflow Serialization error on object holding reference to JPA Data Repository (JSF Integration)

Spring Webflow: 2.3.1
Spring Data: 1.0.2
JSF: 2.1.9
Primefaces: 3.3.1
I'm trying to combine Spring Data JPA Repositories with JSF DataModel, in this case, Primefaces with LazyDataModel. This happens in a WebApp integrating Spring with JSF, using Spring Webflow.
The problem is when i use a JpaRepository inside LazyDataModel that lives in ViewScope of Spring Webflow:
Could not serialize flow execution; make sure all objects stored in
flow or flash scope are serializable
org.springframework.webflow.execution.repository.snapshot.SerializedFlowExecutionSnapshot.(SerializedFlowExecutionSnapshot.java:75)"
Without the JpaRepository inside LazyDataModel, i cannot get the correct page and use the Pagination model of spring data. I already found someone with the same problem, but unfortunately no one answered:
http://forum.springsource.org/showthread.php?116022-Webflow-Serialization-error-on-object-holding-ref-to-JPA-Data-Repository
Thanks for you help in advance
Best regards
JSimas
Already found a solution for this problem!
Whenever you need to add a reference to a Spring Data JPA Repository in an object that will live in your spring webflow context, just to declare the JPA repository as transient! Then you need to add the following annotations in the object that contain the transient reference:
#Configurable - to mark the class to be configured on the fly (this will add some overhead to your application, be careful to use this annotation only when you need)
#Autowired - add this to the transient JPA Repository
So, if you're extending a DataModel JSF base class, and you want to add some JPA repository, here is an example:
#Configurable
public class XptoLazyDataModel extends LazyDataModel<Xpto> {
#Autowired
private transient JpaRepository<Xpto> repository;
(...)
}
And there you go. Hope that this can help someone.
Best regards
It also occurred to me that I should apply the transient keyword to my JPA repository field, as you have. This solved the problem, however, I did not need to use #Configurable or #Autowired.

Resources