【发布时间】:2018-03-01 06:14:20
【问题描述】:
在使用 springsecurity 编写自定义身份验证的 spring-boot 应用程序中。 CustomUserService 是一个接口,具有一个实现类和一个用于从数据库中获取数据的存储库。
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider{
@Autowired
private CustomUserService userService;
@override
someMethod(){//method of CustomUserService interface.
userService.display();//here "userService" is always null coming
}
}
将@service 保留在实现中并使用@ComponentScan 进行基础包发现
【问题讨论】:
-
CustomUserService 实现类是否使用了注解
-
把
@Service或@Component放在实现CustomUserService接口的类上,也别忘了把这个实现类放在组件扫描的雷达上。 -
向你展示stacktrace,完整类
CustomAuthenticationProvider和主类 -
删除了一些行: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] 在
标签: spring spring-mvc spring-boot spring-security autowired