0

I have searched StackOverflow and found the following question regarding injecting AuthenticaitonManager: How To Inject AuthenticationManager using Java Configuration in a Custom Filter , however this question is three years old and my configuration is all Java.

After migrating code from another project to Spring Boot 1.5.3, I get the following error:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
15:48:00.316 [main] ERROR o.s.b.SpringApplication#815 Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityServiceImpl': Unsatisfied dependency expressed through field 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationManager' defined in class path resource [org/springframework/boot/autoconfigure/security/AuthenticationManagerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.authentication.AuthenticationManager]: Factory method 'authenticationManager' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsServiceImpl' defined in file [/Users/intheshell/Desktop/Project_C/target/classes/com/hellokoding/account/service/UserDetailsServiceImpl.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)

I get an error if I try to instantiate authenticationManager that the type cannot be instantiated. Do I need to "explicitly expose AuthenticationManager as a bean" as mentioned here: Spring Security Java Config ? Is this still relevant to 2017 and my situation? What's the best practice?

Here is my SecurityServiceImpl

 @Controller
 @Service
 public class SecurityServiceImpl implements SecurityService{
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserDetailsService userDetailsService;

    private static final Logger logger = LoggerFactory.getLogger(SecurityServiceImpl.class);

    @Override
    public String findLoggedInUsername() {
        Object userDetails = SecurityContextHolder.getContext().getAuthentication().getDetails();
        if (userDetails instanceof UserDetails) {
            return ((UserDetails)userDetails).getUsername();
        }

        return null;
    }

    @Override
    public void autologin(String username, String password) {
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());

        authenticationManager.authenticate(usernamePasswordAuthenticationToken);

        if (usernamePasswordAuthenticationToken.isAuthenticated()) {
            SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
            logger.debug(String.format("Auto login %s successfully!", username));
        }
    }


}
Community
  • 1
  • 1
InTheShell
  • 159
  • 4
  • 12

1 Answers1

1

Looks like you need the dependency

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.9</version>
</dependency>
StanislavL
  • 56,971
  • 9
  • 68
  • 98