DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Programmatic Registration of Java Configuration Beans

Programmatic Registration of Java Configuration Beans

Migrating from XML configuration to Java configuration in Spring can be challenging sometimes. We take a look at a real-world example via Spring Security.

Mick Knutson user avatar by
Mick Knutson
·
Apr. 27, 17 · Tutorial
Like (2)
Save
Tweet
Share
9.72K Views

Join the DZone community and get the full member experience.

Join For Free

I have been working on refactoring the Spring Security Calendar application from an XML configuration to a Java configuration.

In the migration, the is a DefaultService.java file that is using UserDetailsManager, which extends UserDetailsService:

DefaultService:

@Repository
public class DefaultCalendarService implements CalendarService {
 
    ...
    private UserDetailsManager userDetailsManager;
 
    @Autowired
    public DefaultCalendarService(final EventDao eventDao,
                                  final CalendarUserDao userDao,
                                  final UserDetailsManager userDetailsManager) {
        ...


UserDetailsManager:

public interface UserDetailsManager extends UserDetailsService {
    ...


But, during the Autowire sequence, the Security Objects created from the WebSecurityConfigurerAdapter and used in other @Components can’t be found:
***************************
APPLICATION FAILED TO START
***************************
Description:
 
Parameter 2 of constructor in com.packtpub.springsecurity.service.DefaultCalendarService required a bean of type 'org.springframework.security.provisioning.UserDetailsManager' that could not be found.
 
Action:
 
Consider defining a bean of type 'org.springframework.security.provisioning.UserDetailsManager' in your configuration.


The crux of the issue is that with Java configuration and @Autowired-driven configuration, the configuration initialization ordering does not allow for Security Objects to be initialized before other @Component’s.

There is a JIRA ticket already discussing this issue, and eluding to a possible fix in Spring Security 5. I created a custom WebSecurityConfigurerAdapter:

@Configuration
@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


Which has a method we can override to get access to the UserDetailsService as seen here:
@Bean
@Override
public UserDetailsService userDetailsService() {
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser(User.withUsername("user").password("password").roles("USER").build());
    manager.createUser(User.withUsername("admin").password("admin").roles("USER", "ADMIN").build());
    manager.createUser(User.withUsername("user1@example.com").password("user1").roles("USER").build());
    manager.createUser(User.withUsername("admin1@example.com").password("admin1").roles("USER", "ADMIN").build());
    return manager;
}


The issue is that we actually needed a UserDetailsManager not a UserDetailsService, so I changed the return type to UserDetailsManager as seen:

@Bean
@Override
public UserDetailsManager userDetailsService() {
    ...


Now we can successfully Autowire a UserDetailsManager into other @Components in our IoC.

Java (programming language) Spring Security

Published at DZone with permission of Mick Knutson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Development Trends 2023
  • Kotlin Is More Fun Than Java And This Is a Big Deal
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • How to Develop a Portrait Retouching Function

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: