Use Spring Boot driven JPA in KeyCloak StorageProvider - spring-boot

I have to create KeyCloak Custom User Provider. To do so I quickly create Spring Boot app, setup database, create UserRepository with UserService etc. Test succesfully read users table. Then I created UserStorageProvider and UserStorageProviderFactory classes required by KeyCloak. And deploy jar library to keycloak-18.0.0 instance. UserStorageProvider detected by keycloak and show up in drop box on User federation page. But I still can't login with external users because of error in the log KC-SERVICES0013: Failed authentication: java.lang.NullPointerException: Cannot invoke "com.abc.financetech.smt.services.UserService.getUserByUsername(String)" because "this.userService" is null. Obviously, the reason for this is that jar file wasn't started as Spring Boot application, thus no UserRepository and UserService wasn't instantiated and initialized at all. How can I do this?

Related

Why createEntityManager is called before my REST controller method

This is more of a Spring JPA (Hibernate) related issue than AspectJ one, I believe.
Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE, Hibernate v5.4.9.Final, Spring Data Repositories
I am trying to implement Multi-tenancy in my existing single tenant application.
The tenant is chosen by the users via front-end. The tenant id is
stored in HttpSession.
An advice for Rest Controller methods, reads
the tenant id and keeps it in a ThreadLocal variable.
Now I have to enable a Hibernate Filter using the tenant id. For this, an AspectJ advice for my Repository (interface extends JpaRepository) methods work, but gets executed for each repository method call from Service.
This post is about the following (identical) implementations where the advice for enabling Hibernate Filter is fired before the advice for reading the tenant id from HttpSession. How do I correct the order? The advice for reading the tenant id does not make any repository calls.
#AfterReturning(pointcut = "execution(* javax.persistence.EntityManagerFactory+.createEntityManager(..))", returning = "entityManager")
Ref: https://github.com/spring-projects/spring-framework/issues/25125 (This thread contains a non-AOP way also - post-processing transactional EntityManager instances - That gets triggered only when the Spring Boot starts)
#AfterReturning(pointcut="bean(entityManagerFactory) && execution(* createEntityManager(..))", returning="retVal")
Ref: Access to Session using Spring JPA and Hibernate in order to enable filters
I tried setting the application property spring.jpa.open-in-view=false, but that didn't help.

Spring boot provided "spring.datasource.password" can change to "spring.ds.pwd" without creating new bean for DataSource

My team is using latest spring boot for one of the project.
Spring provides database configuration in application.properties file and for password this is the key "spring.datasource.password"
Now, we want to change this to "spring.ds.pwd", but, the change should not required to create new Bean for datasource is it possible?
I followed below link, but, here new beans are creating to handle the session, which should not be expected. Other search information is also similar.
https://dzone.com/articles/spring-boot-jpa-mysql-sample-app-code-example
I was able to achieve what I want by overriding the DataSourceProperties java class with same package name.

circular dependency error for dataSource after adding spring DATA JPA

I am working in spring boot security with Oauth2. Oauth2 to makes use of jdbcAuthentication in AuthorizationServerConfigurerImpl. But in my webSecurityCofigurer implementing class i want to use Spring data JPA to implement userDetailsService. After enabling #EnableJpaRepositories it is throwing circular depenency error and not able to load dataSource which i am defining in AuthorizationServerConfigurerImpl.
Please help me resolving issue

Spring Boot: purpose of #EnableSocial anotation

in my Spring Boot app I am implementing the social login using Spring Social (1.1.4.RELEASE), I have following configuration class:
#Configuration
#EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {
I see that #EnableSocial calls #Import(SocialConfiguration.class), but when I remove annotation #EnableSocial the social login works the same and SocialConfiguration is used anyway.
It's difficult to say without knowing more about your code, but even if you don't use #EnableSocial, Spring Boot will still assume you mean to use it if you provide sufficient social information (I'll use Facebook as an example here):
Your project depends on org.springframework.social:spring-social-facebook directly or transitively through for example org.springframework.boot:spring-boot-starter-social-facebook.
You specify information that the app can use to connect to the social network site. To figure out exactly what you need for Spring Boot to automatically configure it, you should check out the AutoConfiguration class for the social site in question, for example FacebookAutoConfiguration.
By the way, here's an example on how to get it working without #EnableSocial. Does it look similar to your setup? https://spring.io/guides/gs/accessing-facebook/

Spring Bean Container

I wanted to write test cased for testing my service using springjunit runner.
My service will call another service and transform its out put and send the response.
I thouhgt that server need to be up and running for calling the other service while running junit.
But I was told that spring junit doesnt need server to be running.
Spring container will do the magic it seems.
Am not pretty sure how this happens.
Can any one enlighten me with how spring container can act as server?
If its so silly quesiton,sorry for that.Thanks in advance
do you need server to run java.class , answer is no, untill unless if you have any ejb component to be called from your service, or you service need some external web service to
respond (here you may need to either mock this service to give mock data or run the service on server)
i have service which calls data access layer , sometimes service calls another service.
all you need to have spring context configuration in your test class
#ContextConfiguration({ "classpath:spring-context.xml", "classpath:otherservice-context.xml"})
#RunWith(SpringJUnit4ClassRunner.class)
#Component
public class TestJuint{
#Autowire
private otherService otherServiceImpl;
#Autowire
private service serviceImpl;
#Test
public void testDummy{
serviceImpl.addDummy(dummyObj);
}
}
suppose if you need to have another service from some other package then you might want to include its context file in context configuration, so that its bean reference will be in spring context while autowiring

Resources