-
-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document how to use this if Keycloak is on the classpath #2
Comments
@wimdeblauwe I'm very sorry, I didn't see your issue until today (wasn't notified). I'm using Keycloak as authentication server, but not |
@wimdeblauwe I initiated the sample project with Keycloak libs for resource-server with spring-boot and spring-security: https://github.com/ch4mpy/spring-addons/tree/master/keycloak-sample, but If you find an easy fix... |
Really appreciated. I am struggling on how to setup test with Keycloak on the classpath atm. That is how I stumbled on you repo. I will look at your sample project and see whether this clarifies it for me. |
@pvannierop alternatively to having Keycloak on the classpath, you can have a look at this sample in the repo: https://github.com/ch4mpy/spring-addons/blob/master/spring-security-oauth2-test-webmvc-addons/src/test/java/com/c4_soft/springaddons/samples/webmvc/oidcid/OidcIdServletApp.java I runs against a Keycloak authorization-server. This how I configure my micro-services. |
@ch4mpy I played around with your keycloak sample and I could reproduce your error. What I do not understand is that most tests of the GreetingControllerTest pass when not using the KeycloakConfig.class by changing:
into
I had to make one trivial update to a test to make it succeed. Change:
into
So, you are proposing to use spring-security-oauth2 here instead of keycloak adapter, right? I have been thinking about this too. I will try when I have time tonight. PS Sorry for perhaps not knowing obvious stuff. I work at a open-source company in the Netherlands (The Hyve), but I am a biologist by training. I am working hard to get up to speed on security in spring. Thnx again for your time. |
I had a quick look and it seems you are not importing Having said that, things are still not working when I changed these things and I don't really understand why given that my production application does basically the same thing. |
I had tried but it didn't trigger proper Keycloak auto-configuration, which would probably be enough to solve the problem
@wimdeblauwe this is a hack to keep produced jars small (not pollute ossrh with useless "fat jars"). The boot apps in this project are intended to run from within an IDE for experiments, not as
@pvannierop yes. I added more mvc samples to those already demonstrating that. |
@wimdeblauwe I downgraded Keycloak from 9.0.3 to 9.0.0 as you mentioned you were using, and got it working. Will dig why it is now broken tomorrow (almost midnight here) |
@ch4mpy @wimdeblauwe Thnx for all your work. I'll test it later tonight. I also would appreciate your view on why most tests succeeded when disabling the Keycloak integration (see my last post). |
Just moved Keycloak sample with other webmvc samples under https://github.com/ch4mpy/spring-addons/tree/master/spring-security-oauth2-test-webmvc-addons/src/test/java/com/c4_soft/springaddons/samples/webmvc I also edited main and spring-security-oauth2-test-webmvc-addons READMEs The issue with Keycloak 9.0.3 is not solved, but, IMO, this is a separate problem. @wimdeblauwe may we close this issue? |
Sure. Weird that 9.0.3 does not work. |
I'll try some investigation and maybe open a Keykloak ticket. |
I have created controller tests similar to the sample project and all works fine! However ..... when running integration tests that use the full application context I found that one change proposed by @wimdeblauwe is essential which is moving the bean definition of This change involves creating a dedicated class that imports the central config and annotating the bean witrh @primary, like so:
If I do this both unit and integration tests run fine. I have no clue why this is any different from the unit level tests. It would be nice to somehow document this or integrate this into the sample project, just to prevent this problem. B.t.w. for reference the exception thrown when not implementing this is:
|
Thank you for this project. It is just what I needed for my
@WebMvcTest
tests in my Spring Boot project. I am using Spring Boot 2.2.6 withkeycloak-spring-boot-starter
9.0.0.After adding:
to my project, I also had to do the following things:
@TestConfiguration
for my test:Where
WebSecurityConfiguration
is my actual production security config class:application-webmvc-test.properties
insrc/test/resources
:Might be good to add something similar to the documentation, as most people will have Keycloak on the classpath in their project (unlike the example in your repo currently).
The text was updated successfully, but these errors were encountered: