当前位置: 首页 > 知识库问答 >
问题:

Spring Boot:Autowired CRUDRepository为空,在某些类中

巫墨一
2023-03-14
public interface UsersRepositoryInterface extends CrudRepository<User, Long> {

    // Query to search for users via email
    List<User> findByEmail(@Param("email") String email);
}

首先是WebSecurityConfiguererAdapter类,我在其中向筛选器添加路径。请注意带有“new jWTLoginFilter”的行,这是我尝试自动连接的类:

@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()
            )
        );
    }
}

在调用jwtloginfilter时,println中的println将始终返回Null。

现在管用了。

使用

@Component("someName")

并将其注入WebSecurityConfig

@Resource(name="someName")
private JWTLoginFilter myFilter;
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}
@Override
@Autowired
public void setAuthenticationManager(AuthenticationManager authenticationManager) {
    super.setAuthenticationManager(authenticationManager);
}

在移除

setAuthenticationManager(authManager);

在JWTLoginFilter中的构造函数中

暂时还没有答案

 类似资料:
  • 我知道拥有一个空的类通常是一个设计缺陷,但是在我的情况下,它是否是最好的选择,或者对于这个Scnario来说,什么是最好的设计呢? ClassA是一个空的超类这里是我的代码 ClassB是从ClassA派生的

  • 你好,我看到的日志信息只在某些类,不知道在哪里看。是我的原木模式搞错了还是?这是我的log4j配置,我将它与sfl4j一起使用: null 我创建的记录器是这样的,类字段: 和日志记录,如: 有什么想法吗? 更新 根据Petar Minchev的回答改为信息,但我仍然没有得到任何东西。 更新II 现在,在添加了一些依赖项之后,它可以在一些类上工作(仍然不能在其他一些类上工作)。有一些实现runna

  • 所以我的数据集有一些信息,按业务n日期如下: 我需要以下格式的数据:我如何转换它。我不想在我的输出数据集中使用多级 我尝试了以下语法: 我得到的结果如下: 当我打印列时,它不会将LOB显示为列。我的最终数据帧还应该包括业务,日期字段作为列,以便我可以加入这个数据帧与另一个业务数据帧

  • 问题内容: 我正在解析包含以下行的文件 我想将其拆分为单独的字段。 在我的示例中,有四个字段:类型,标题,页面和注释。 分割后的预期结果是 显然,简单的字符串拆分将不起作用,因为它将仅在每个空格处拆分。我想分割空格,但保留括号和引号之间的所有内容。 我该如何拆分? 问题答案: 这个正则表达式应该为您工作 说明

  • 问题内容: 我有一个来自客户的错误,当我查看日志时,我们跟踪异常,某些堆栈跟踪没有行号: 请注意:我已将包名称替换为“ xx”),并且所有类和方法均在我们的应用程序中定义: 完整的堆栈跟踪如下: 我对为什么会发生这样的事情很感兴趣,我的客户是否有可能对现有代码(自定义)进行某些处理? 问题答案: 不显示行号的代码是在没有调试信息的情况下编译的。

  • 我为我的AAR库做了C#绑定,但是扩展支持片段的片段不包括在绑定中。另外,返回类型为支持片段的其他类的方法也不包括在内。 我的继承层次结构:ScreenAFragment(公共)->PermissionsRequesterFragment(公共抽象)->BaseFragment(公共抽象)->Fragment(Android.support.v4.app包) 有人能告诉我这种行为的原因,并告诉我如