Stack Exchange
Why SecurityContextHolder.getContext() == null?
I don't run my app, because auth == null. Why? I use spring boot 2.2.7.RELEASE and spring-security-oauth2-autoconfigure. 
When starting SecurityContextHolder.getContext() always equal null
@Component
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Autowired
    UserRepository userRepository;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Bean
    public PasswordEncoder encoder() {
        return new BCryptPasswordEncoder();
    }

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/addPet").authenticated()
                .antMatchers("/", "/**").access("permitAll")
                .and().oauth2Login().loginPage("/login").defaultSuccessUrl("/", true).and().logout().logoutSuccessUrl("/")


                .and().formLogin().loginPage("/login").loginProcessingUrl("/login").usernameParameter("login").passwordParameter("password")
                .defaultSuccessUrl("/", true).and().logout().logoutSuccessUrl("/").deleteCookies("JSESSIONID").and().csrf().disable();

    }

    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery(
                        "select login, password, enabled from users where login=?")
                .authoritiesByUsernameQuery(
                        "select login, role from users where login=?");
    }
}



I assume that you're referring to this line,
Authentication auth = SecurityContextHolder.getContext().getAuthentication();

In this case, you don't need this line, it's not being used anywhere.  In the bigger answer, SecurityContextHolder.getContext() will never be null, according to the documentation.  The call to getAuthentication(), however, will be null unless there's an authenticated principal associated to the session.

Thank you, but how do I get a user who is currently logging in

Since you're using spring, simply add Principal to your controller method arguments and you will get it automagically
