Skip to content

Commit

Permalink
Implementing UserDetailsServiceImpl and creating UserRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
kabi239 committed Jan 11, 2025
1 parent 2ed7441 commit 3524112
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/ecommerce/sb_ecom/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ecommerce.sb_ecom.repository;

import com.ecommerce.sb_ecom.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByUsername(String username);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ecommerce.sb_ecom.security.services;

import com.ecommerce.sb_ecom.model.User;
import com.ecommerce.sb_ecom.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

@Autowired
UserRepository userRepository;

@Override
@Transactional
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username)
.orElseThrow(()->new UsernameNotFoundException("User Not Found With Username : "+ username));
return UserDetailsImpl.build(user);
}
}

0 comments on commit 3524112

Please sign in to comment.