spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

AlreadyBuiltException: This object has already been built

Open rwinch opened this issue 10 years ago • 9 comments

From https://github.com/spring-projects/spring-security/issues/3825#issuecomment-223795067

I had the same issue.

I was wiring a custom AuthenticationSuccessHandler into my security configuration that indirectly referenced a @Service bean with an @Autowired reference to AuthenticationManager. After some thought, I'm surprised it worked - but no problems at all at runtime! Perhaps some sort of wiring order / race condition helped me out? However, while running the same vacuous test mentioned by @whittle, I encountered the dreaded AlreadyBuiltException.

Note that in a very stripped down version of my application I was debugging this issue with, I did receive a BeanCurrentlyInCreationException on AuthenticationManager but only during testing - not at runtime. Not exactly the same issue, but could be related behavior. See here: https://github.com/davidkey/SecurityTestErrorPOC.

My fix was to add @Lazy to my AuthenticationSuccessHandler in my security configuration while I rethink my architecture a bit.

HTH.

rwinch avatar Jun 06 '16 22:06 rwinch

Hey @rwinch as I already mentioned on Gitter, here's my example of the problem, the latest commit is failing while the commit before that works: https://github.com/Turbots/data-jpa-preautorize-sample

Turbots avatar Jun 07 '16 07:06 Turbots

@davidkey Thanks for the comment. I'm trying to reproduce this and unable to. Can you provide exact steps for reproducing the issue?

rwinch avatar Jun 17 '16 15:06 rwinch

@Turbots Thanks for creating your sample. I was able to reproduce the issue using your sample. I'm going to break this out into a separate issue since I'd like to confirm your case separately.

rwinch avatar Jun 17 '16 15:06 rwinch

@Turbots Turns out your issue is very related to #3934 so I am not going to create a specific issue for it. However, I did add a test case to ensure that your problem is resolved.

rwinch avatar Jun 17 '16 16:06 rwinch

@rwinch Here's a gist with a full dump of what I'm seeing. Oddly, I was NOT able to reproduce the issue on travis-ci.

Going to give it a shot on a seperate (windows) install on the same machine and see if I see the same behavior. Will report back shortly.

davidkey avatar Jun 17 '16 19:06 davidkey

Same PC, running Windows 10 (vs Ubuntu 16) and I do not experience the issue. Same versions of source code / dependencies / maven / java (if that even makes a difference?).

Very strange. Perhaps some weird thread / prioritization issue? Let me know if there's anything else you'd like me to try.

davidkey avatar Jun 17 '16 19:06 davidkey

@davidkey Thanks for the fast response! I just pushed some fixes into master that may have fixed your issue.

Perhaps the easiest thing would be for you to try running against the latest SNAPSHOT yourself? To do so you have two options:

  • Wait until the build completes and add https://repo.spring.io/libs-snapshot to your maven repositories.
  • Alternatively, you can clone master and do a "./gradlew install" which will install the code into your local maven repository.

In your project you can then define a property of:

<properties>
    <spring-security.version>4.1.1.BUILD-SNAPSHOT</spring-security.version>
</properties>

If you could respond that your issue is fixed or not, that would be greatly appreciated! If it is not, we can continue to hunt for the issue.

rwinch avatar Jun 17 '16 19:06 rwinch

@rwinch Using the latest SNAPSHOT fixed it! Awesome - thanks!

davidkey avatar Jun 17 '16 20:06 davidkey

@davidkey Thanks for the fast report! Given your feedback I'm going to close this as fixed

rwinch avatar Jun 17 '16 20:06 rwinch