Closed straurob closed 2 years ago
@straurob The javadoc for @WebMvcTest
states the following:
By default, tests annotated with @WebMvcTest will also auto-configure Spring Security ...
I've tried your sample and confirmed that OAuth2ClientRegistrationRepositoryConfiguration
is indeed bootstrapped, which explains the error you are getting. You will need to make some changes to your test.
I'm going to close this as the behaviour is expected based on the test configuration.
In case anyone else will stumble upon this, it seems as if the exclusion of OAuth2ClientAutoConfiguration
resolves the issue:
@WebMvcTest(excludeAutoConfiguration = OAuth2ClientAutoConfiguration.class)
@straurob that doesn't work if you're trying to test your security. It would seem you can't mock your authentication server in a spring-security test. Am I missing something here?
I'm also fighting with this.
I have to remove all spring.security.oauth2.client.*
from application.yml, so it's not picked up by the test. (And create another profile for the actual dev and production app.)
But then I get Parameter 0 of method setFilterChains in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' that could not be found.
when the test starts, it won't boot.
That means I have to disable my SecurityConfiguration
class, i.e. the one with @EnableWebSecurity
annotation.
If I do that, then I can't use mvc.with(oidcLogin())
. It does nothing. I.e. in the controller's parameter @AuthenticationPrincipal principal: OidcUser
, the value is always null.
I want to test if the security rules and filters are correct and ODIC roles are correctly mapped to them (in a test marked with @SpringBootTest
using MockMvc). I can't find any real example of such configuration.
The closest I found is https://github.com/ch4mpy/spring-addons/blob/master/samples/tutorials/servlet-client/src/test/java/com/c4soft/springaddons/tutorials/ServletClientApplicationTests.java which actually uses @SpringBootTest
with MockMvc
and has everything in application.yml filled-in, so the autoconfiguration works.
But that repository mocks InMemoryClientRegistrationRepository
to get it to work. Is that the recommended way?
Describe the bug My Spring Boot application uses
spring-boot-starter-oauth2-client
andspring-boot-starter-oauth2-resource-server
. The application provides a simple REST controller returning the OAuth2 user's information. I created a@WebMvcTest
to verify the controller's behavior.However, this test attempts to create a real connection to the identiy provider's URL which is configured by
spring.security.oauth2.client.provider.keycloak.issuer-uri
.There is a similar issue #7624 but this is about
@SpringBootTest
and not@WebMvcTest
.Expected behavior The
@WebMvcTest
should not rely on a running instance of an identity provider.Sample
The complete example is also available on GitHub: https://github.com/straurob/spring-security-11784