Witam, zmierzam się z pewnym problemem. Wyciągam aktualnie zalogowanego usera jego email w ten sposób:
public String currentLoggedInUser() {
Object auth = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String email = (String) auth;
UserEntity createdByEmail = repository.findByEmail(email);
return createdByEmail.getUserId();
}
Jak widać zwraca ona mi userId.
Chciałbym po zalogowaniu, żeby przenosiło nas Od razu na endpoint z userId, lecz gdy to zrobię z pomocą tej metody wyskakuje mi wyjątek:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NullPointerException
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/h2-console").hasRole("ADMIN")
.anyRequest().permitAll()
.and()
.formLogin().defaultSuccessUrl("/users/" + currentLoggedInUser()).permitAll()
.and()
.logout().logoutUrl("/log-out").permitAll();
}
i teraz mam pytanie co muszę zrobić by to wszystko działało pozdrawiam :D