I have this security config class below. Since i am implementing jwt authentication, i commented out the old lines and added UserDetailsServiceImpl bean in place.
@Configuration
public class SecurityConfig {
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Autowired
private JwtFilter jwtFilter;
// @Bean
// public UserDetailsManager userDetailsManager(DataSource dataSource) {
// JdbcUserDetailsManager theUserDetailsManager = new JdbcUserDetailsManager(dataSource);
// theUserDetailsManager
// .setUsersByUsernameQuery("select user_email, user_password, enabled from chat_user where user_email=?");
//
// theUserDetailsManager.setAuthoritiesByUsernameQuery("select user_email, authority from authorities where user_email=?");
// return theUserDetailsManager;
// }
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(configurer -> configurer
.requestMatchers("/css/**", "/images/**").permitAll() //initially non authenticated users does not have access to css, so this is added
.requestMatchers("/showSignUpForm","/showLoginForm","/processSignUpForm").permitAll() // allow unauthenticated access to sign up page
.anyRequest().authenticated())
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
.formLogin(form -> form.loginPage("/showLoginForm")
.loginProcessingUrl("/authenticateTheUser")
.usernameParameter("user_email")
.passwordParameter("user_password")
.defaultSuccessUrl("/showChatPage", true)
.permitAll());
return http.build();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration auth) throws Exception {
return auth.getAuthenticationManager();
}
My UserDetailsServiceImpl looks like this
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private ChatUserService chatUserService;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
ChatUser user = chatUserService.findByUserEmail(email);
if (user != null) {
return org.springframework.security.core.userdetails.User.builder()
.username(user.getUser_email())
.password(user.getUser_password())
.roles("ROLE_USER")
.build();
}
throw new UsernameNotFoundException("User not found with username: " + email);
}
}
But now i’m getting this error
Description:
Field sessionFactory in com.springprojects.realtimechatapp.dao.ChatUserDAOImpl required a bean of type ‘org.hibernate.SessionFactory’ that could not be found.
The injection point has the following annotations:
– @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type ‘org.hibernate.SessionFactory’ in your configuration.
My ChatUserDaoImpl class looks like this:
@Repository
public class ChatUserDAOImpl implements ChatUserDAO{
@Autowired
private SessionFactory sessionFactory;
@Override
public List<ChatUser> getchatUsers() {
Session currentSession = sessionFactory.getCurrentSession();
Query<ChatUser> theQuery = currentSession.createQuery("from ChatUser", ChatUser.class);
List<ChatUser> chatUsers = theQuery.getResultList();
return chatUsers;
}
@SuppressWarnings("deprecation")
@Override
public void saveChatUser(ChatUser theChatUser) {
Session currentSession = sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(theChatUser);
}
@Override
public ChatUser getChatUser(int theId) {
Session currentSession = sessionFactory.getCurrentSession();
ChatUser theChatUser = currentSession.get(ChatUser.class, theId);
return theChatUser;
}
@Override
public void deleteChatUser(int theId) {
Session currentSession = sessionFactory.getCurrentSession();
Query theQuery = currentSession.createQuery("delete from ChatUser where chat_id=:chatUserId");
theQuery.setParameter("chatUserId", theId);
theQuery.executeUpdate();
}
@Override
public ChatUser findByUsername(String username) {
Session currentSession = sessionFactory.getCurrentSession();
Query<ChatUser> theQuery = currentSession.createQuery("from ChatUser where user_name=:u", ChatUser.class);
theQuery.setParameter("u", username);
try {
return theQuery.getSingleResult();
} catch (NoResultException nre) {
return null;
}
}
@Override
public ChatUser findByUserEmail(String email) {
Session currentSession = sessionFactory.getCurrentSession();
Query<ChatUser> theQuery = currentSession.createQuery("from ChatUser where user_email=:e", ChatUser.class);
theQuery.setParameter("e", email);
try {
return theQuery.getSingleResult();
} catch (NoResultException nre) {
return null;
}
}
}
Please help what can I do here differently to make it work?