-1

In spring-boot application using springsecurity for writing custom autentication. CustomUserService is a interface and has a implementation class and a repository to fetch data from database.

@Component
    public class CustomAuthenticationProvider implements AuthenticationProvider{
    @Autowired
    private CustomUserService userService;

    @override
    someMethod(){//method of CustomUserService interface.
    userService.display();//here  "userService"  is always  null coming
    }
    }

kept @service on implementation & used @ComponentScan for basepackages discovery

user2709752
  • 488
  • 6
  • 26
  • 1
    Did you use any anotations for the CustomUserService implementation class – Sunimal S.K Malkakulage Mar 01 '18 at 06:19
  • 1
    Put `@Service` or `@Component` on the class which implements `CustomUserService` interface and also don't forget to keep this implementing class in the radar of component scan. – Atul Dwivedi Mar 01 '18 at 06:23
  • show you stacktrace, the full class `CustomAuthenticationProvider` and the main class – Patrick Mar 01 '18 at 07:24
  • Removed some lines : java.lang.NullPointerException: null at com.example.demo.configs.CustomAuthenticationProvider.authenticate(CustomAuthenticationProvider.java:74) ~[classes/:na] at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174) ~[spring-security-core-4.1.4.RELEASE.jar:4.1.4.RELEASE] at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:199) ~[spring-security-core-4.1.4.RELEASE.jar:4.1.4.RELEASE] at – user2709752 Mar 01 '18 at 07:52
  • Can you show the code, where you use the CustomAuthenticationProvider class? Do you create an instance of that class by yourself with the new keyword? – dunni Mar 01 '18 at 08:14
  • I followed this code javainsimpleway.com/spring-security-using-custom-authentication-provider/ – user2709752 Mar 01 '18 at 08:55
  • 1
    Possible duplicate of [Why is my Spring @Autowired field null?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – dur Mar 01 '18 at 14:30

4 Answers4

0

As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring's component scan enabled, then Spring framework can find out the (interface, implementation) pair.

In this case, if it is null , then either the implementation class is not annotated with @Component or component scan is not working

CGS
  • 2,782
  • 18
  • 25
0

Check whether you annotate CustomUserService implementation class @Component or @Service and whether component scan can discover this service.

  • kept service annotation on implementation & ComponentScan annotation for basepackages discovery – user2709752 Mar 01 '18 at 06:49
  • I have a confusion in the provided code `@override someMethod(){//method of CustomUserService interface. userService.display();//here "userService" is always null coming }`. for method `someMethod()` you commented as `//method of CustomUserService interface`. but you used `@override` . still the class `CustomAuthenticationProvider` implements only `AuthenticationProvider`. – Sunimal S.K Malkakulage Mar 01 '18 at 07:30
  • I am using code in http://javainsimpleway.com/spring-security-using-custom-authentication-provider/ . In CustomAuthenticationProvider class, "userService" reference is always null – user2709752 Mar 01 '18 at 07:38
  • This also can be happen if you instantiate a object `CustomUserService` with `new` – Sunimal S.K Malkakulage Mar 01 '18 at 14:25
0

In spring boot application, you don't have to explicitly enable component scan. All the annotated classes will automatically be registered as beans which are present under the package where the spring-boot application class (annotated with @SpringBootApplication) is present.

So if you Application class is present in com.abc package all the subpackages of com.abc will be scanned for spring beans. Refer the tree below:

+--- src
|   +--- main
|   |   +--- java
|   |   |   +--- com
|   |   |   |   +--- application
|   |   |   |   |   +--- Application.java
|   |   |   |   |   +--- beans
|   |   |   |   |   |   +--- ASpringBean.java
|   |   |   |   |   |   +--- AnotherSpringBean.java

May be in your case the beans are not getting discovered because they are outside application class package.

Ubercool
  • 1,029
  • 2
  • 14
  • 29
0

My bad!.......some where in the code I used new Keyword to create instance of CustomAuthenticationProvider, so that's why spring is not autowiring instances inside of CustomAuthenticationProvider class.

user2709752
  • 488
  • 6
  • 26