Skip to content
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

Spring Formatters are not being registered when some class extends WebSecurityConfigurerAdapter [SPR-15239] #19804

Closed
spring-projects-issues opened this issue Feb 9, 2017 · 7 comments
Assignees
Labels
in: core in: web status: declined

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Feb 9, 2017

Juan Carlos García del Canto opened SPR-15239 and commented

The desired steps to a valid Formatters registration are:

  1. First of all, the method addWebMvcConfigurers located in WebMvcConfigurationComposite should registrate all the web configurers. That includes the formatters.
  2. After that, when some component needs to @Autowired a ConversionService instance, the method mvcConversionService located in WebMvcConfigurationSupport obtains a ConversionService instance and register all the existing formatters on it before return the instance.

However, if a @Configuration class extends the WebSecurityConfigurerAdapter abstract class, some component is trying to @Autowired a ConversionService instance before the formatters have been registered in the Spring context, so the addFormatters method doesn't include any formatters on it.

I've just created the following proof of concept that uses Spring Boot to reproduce this issue:

https://github.com/jcagarcia/proofs/tree/master/spring-security-and-formatters

If you execute this proof of concept using the mvn compile spring-boot:run command, you could check that the Create Pets view shows an enum value without apply the conversion to String.

!https://i.stack.imgur.com/qugji.png!

Seems like the component that requires the ConversionService instance is the ContentNegotiationStrategy that is beeing @Autowired in the WebSecurityConfigurerAdapter.

A simple work-around is to @Override the setContentNegotiationStrategy method in our SecurityConfiguration class without include the @Autowired annotation. (The proof of concept includes this work-around commented)

After that, execute this proof of concept again using the mvn compile spring-boot:run command and you could check that the Create Pets view shows an enum value with a valid format applied.

!https://i.stack.imgur.com/Dm5zE.png!

I think this is not the best solution because the ContentNegotiationStrategy is not beeing @Autowired anymore.

I've just created a question in StackOverflow to obtain some answer:

http://stackoverflow.com/questions/42086046/spring-formatters-have-been-not-registered-when-some-class-extends-websecurityco

Also, I've created an issue in spring-security project.

spring-projects/spring-security#4202

Best Regards,


Affects: 4.3.6

Reference URL: http://stackoverflow.com/questions/42086046/spring-formatters-have-been-not-registered-when-some-class-extends-websecurityco

1 votes, 4 watchers

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 9, 2017

Rossen Stoyanchev commented

How do you run this inside an IDE? In IntelliJ I'm getting compile errors on types like org.springframework.roo.petclinic.domain.QOwner and on start:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
	- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
	- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans

I can't make sense of the description so trying to run is the only way to try to find out. For example none of the ContentNegotiationStrategy implementations depend on a ConversionService, they have no reason to. Also what ConversionService is autowired before formatters have been registered? The mvcConversionService bean is obtained from the @Bean method which initializes it with Formatters.

It would help if you could provide a simple example that injects a ConversionService and shows it with and without formatters.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 9, 2017

Juan Carlos García del Canto commented

Hi Rossen,

At first thanks for your interest in this issue. To be able to execute the sample project is necessary to generate the Q classes used by the repositories. You could generate them using the apt-maven-plugin executing mvn clean install. After that, include the target/generated-sources folder as project source. I'm executing this project using STS.

As I've described in the issue, If I extend some @Configuration class from the WebSecurityConfigurerAdapter, a ConversionService is obtained before to register the Spring Formatters in the Spring context.

If you need more information or a simplest example, I'll try to provide you tomorrow.

Regards and thanks again,

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2017

Rossen Stoyanchev commented

Okay I was able to run and debug the application. I can confirm seeing the mvcConversionService bean loads before DelegatingWebMvcConfigurationSupport has been injected with WebMvcConfigurer instances which in turn means that any converters registered through a WebMvcConfigurer are not registered as they should be.

From what I can see in the debugger:
WebSecurityConfigurerAdapter needs a ContentNegotiationStrategy which triggers loading
WebMvcAutoConfiguration$EnableWebMvcConfiguration which needs HttpMessageConvertersAutoConfiguration
JacksonHttpMessageConvertersConfiguration then loads JacksonAutoConfiguration
That's looking for @JsonComponent's like OwnerDeserializer and PetDeserializer
Those are autowired with a ConversionService

This looks to me like a question for Spring Boot. Possibly a configuration ordering issue. Stéphane Nicoll or Andy Wilkinson do you have any idea?

It certainly does not look like the cause is anything to do with the web framework.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2017

Rossen Stoyanchev commented

I tried with Boot 1.4.4 as well.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2017

Juan Carlos García del Canto commented

Thanks for your interest Rossen Stoyanchev and your support!!

Do you recommend me to create an issue in the Spring Boot issue tracker or wait to Stéphane Nicoll and Andy Wilkinson answers?

Regards and thank you again!!

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 10, 2017

Andy Wilkinson commented

There's no need for another issue at this stage. This issue, the Spring Security issue, and the question on Stackoverflow are more than enough already. We'll take a look when we get a chance and figure out what needs to be changed and where.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Feb 28, 2017

Andy Wilkinson commented

We might be able to do something in Spring Boot to break the chain that Rossen describes above, but a user's own configuration could trigger the same problem. I'd like to see what Rob has to say on the Spring Security issue (which I'm now watching) before we take things any further. IMO, this ticket can be closed as I don't think a change in the framework is going to be the answer.

@spring-projects-issues spring-projects-issues added type: bug status: declined in: core in: web labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core in: web status: declined
Projects
None yet
Development

No branches or pull requests

2 participants