我正在运行一个SpringBoot应用程序,其根类注有@SpringBootApplication
,@EnableAutoConfiguration
。Spring Boot:Autowired CRUDRepository null,在某些类中
我创建了一个UserRepositoryInterface
接口,延伸的CrudRepository
接口与我的用户JPA对象。这个接口没有实现,而且还没有需要。本应用程序中没有任何配置文件。除了JPA数据库连接,但它的作品。
public interface UsersRepositoryInterface extends CrudRepository<User, Long> {
// Query to search for users via email
List<User> findByEmail(@Param("email") String email);
}
而且我成功地将它自动装配到一些REST端点。问题出现时,我尝试将Autowire安装到我的安全类中。我正在尝试使用JWT进行身份验证,并且它可以工作。现在我想在登录过程中调用数据库,并且遇到问题。这里是类:
首先WebSecurityConfiguererAdapter
类,其中我添加到过滤器的路径。注意与“new JWTLoginFilter
”行了,这是我努力的Autowire类:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService userDetailsServ;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
//Allow options pre-flight request
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
// Allow POST request to /login
.antMatchers(HttpMethod.POST, "/login").permitAll()
// Others must be authenticated
.anyRequest().authenticated()
.and()
// We filter the api/login requests
.addFilterBefore(new JWTLoginFilter("/login", authenticationManager()),
UsernamePasswordAuthenticationFilter.class)
// And filter other requests to check the presence of JWT in header
.addFilterBefore(new JWTAuthenticationFilter(),
UsernamePasswordAuthenticationFilter.class);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// Change logging in from username+password to email+password
auth.userDetailsService(userDetailsServ);
}
}
而且JWTLoginFilter
类。我中省略一些无关紧要代码:
public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter {
@Autowired
private UsersRepositoryInterface userRepo;
public JWTLoginFilter(String url, AuthenticationManager authManager) {
super(new AntPathRequestMatcher(url));
setAuthenticationManager(authManager);
}
@Override
public Authentication attemptAuthentication(
HttpServletRequest req, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
//Check if userRepo is injected
if(userRepo == null) {
System.out.println("Null");
}
AccountCredentials creds = new ObjectMapper()
.readValue(req.getInputStream(), AccountCredentials.class);
return getAuthenticationManager().authenticate(
new UsernamePasswordAuthenticationToken(
creds.getEmail(),
creds.getPassword(),
Collections.emptyList()
)
);
}
}
的println
在JWTLoginFilter
总是返回空,当呼吁。
我错过了什么吗?
解决它:
现在的作品。
注释的JWTLoginFilter与
@Component("someName")
而且随着
@Resource(name="someName")
private JWTLoginFilter myFilter;
硬编码在JWTLoginFilter构造函数中的URL注入其在WebSecurityConfig,但我还是不得不从WebSecurityConfig自动装配的的AuthenticationManager进入JWTLoginFilter。
首先必须使AuthenticationManager成为一个Bean。这里使用了答案:How To Inject AuthenticationManager using Java Configuration in a Custom Filter
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
然后与答案在这里注入它:Spring Security authenticationmanager must be specified - for custom filter
@Override
@Autowired
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}
虽然在构造函数中删除的
setAuthenticationManager(authManager);
在JWTLoginFilter
能否请您添加的项目结构? – tmarwen