Spring boot @Autowired repository instance null when using spring security

Arber Hoxha

My situation is this:

I am building a spring boot application, when I autowire the UserRepository in the controller it initializes it and when I try to call the findByUserName method everything is OK.

UserController

@Controller    
@RequestMapping(path="/api/v1/users") 
public class UserController {

@Autowired 
private UserRepository userRepository;

@GetMapping(path="/{userName}")
public @ResponseBody AuthenticationDetails getUserByUsername(@PathVariable String userName) throws UserNotFoundException {

    User user = userRepository.findByUserName(userName);=
    ...
    }
}

After creating the controller I needed to use Spring Security to secure the paths of the controller so I added in the SecurityConfig class the following configuration:

SecurityConfig

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.csrf().disable().authorizeRequests()
            .antMatchers(HttpMethod.POST, "/login").permitAll().anyRequest().authenticated().and()
            .addFilterBefore(new JWTLoginFilter("/login", authenticationManager()),
                    UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(new JWTAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

}
...
}

Now, when I try to post a request to the /login path I get a NullPointerException in the CustomAuthenticationProvider class when I try to load the data through userRepository instance by calling the findByUserName method because userRepository instance is null.

CustomAuthenticationProvider

public class CustomAuthenticationProvider implements AuthenticationProvider {

@Autowired 
private UserRepository userRepository;

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    User userFromRepository = userRepository.findByUserName(authentication.getName().toLowerCase()); 
    ...
}

My questions are this:

Is not the state of the beans the same during the run-time of the application? Are the beans created when the application loads right?

Why Spring Boot manages to autowire the instance with the bean in my controller and in the same application but in another class it does not autowire them?

Leffchik

The problem is that you create your CustomAuthenticationProvider like this new CustomAuthenticationProvider(), so it's not really a spring bean and it's fields can not be injected. What you need to do is define CustomAuthenticationProvider bean and it will work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring Boot Autowired Repository null

From Dev

Spring Boot JPA : Autowired JPA repository is null

From Dev

Autowired Repository null in Controller for a Spring Boot application

From Dev

Spring boot mongoDB autowired null repository

From Dev

Spring Boot Autowired null

From Dev

spring @Autowired a repository returns null

From Dev

Spring @Autowired gives null in a @Repository bean

From Dev

Autowired property is null - Spring Boot Configuration

From Dev

Spring Boot JavaConfig with Autowired dependencies that are null

From Dev

Autowired in CustomInterceptor getting null(Spring Boot)

From Dev

Autowired property is null - Spring Boot Configuration

From Dev

Autowired field is null in spring boot application

From Dev

Error 403 on image when using Spring Boot + Spring Security

From Dev

How @Repository and @Autowired work in spring boot Dao and Service Layer?

From Dev

Spring bean is created, but is null when Autowired

From Dev

Understanding Spring Boot @Autowired

From Dev

Understanding Spring Boot @Autowired

From Dev

How to use @Autowired in manually new instance in Spring Boot

From Dev

Spring Boot Autowiring Repository Always Null

From Dev

Spring Autowired null if not found

From Dev

Spring @Autowired comes as null

From Dev

Spring - autowired fields are null

From Dev

Spring @Autowired field is null?

From Dev

Spring - autowired fields are null

From Dev

Spring autowired is null

From Dev

Nullpoint when using Spring Autowired annotation

From Dev

Spring Security with Spring Boot

From Dev

Null @AuthenticationPrincipal with Spring-Boot / Security

From Dev

Spring-Boot @Autowired in main class is getting null

Related Related

HotTag

Archive