-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d00a37
commit 299c323
Showing
10 changed files
with
161 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
.../portal/controller/SecuredController.java → ...oyee/portal/controller/ApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/java/vls/employee/portal/entity/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.java.vls.employee.portal.entity; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "roles") | ||
public class Role { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(unique = true, nullable = false) | ||
private String name; | ||
|
||
public Role() { | ||
// Default constructor | ||
} | ||
|
||
public Role(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/java/vls/employee/portal/repository/RoleRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.java.vls.employee.portal.repository; | ||
|
||
import com.java.vls.employee.portal.entity.Role; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface RoleRepository extends JpaRepository<Role, Long> { | ||
Role findByName(String name); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/java/vls/employee/portal/service/CustomUserDetailsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.java.vls.employee.portal.service; | ||
|
||
import com.java.vls.employee.portal.entity.User; | ||
import com.java.vls.employee.portal.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
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 java.util.Collection; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class CustomUserDetailsService implements UserDetailsService { | ||
|
||
@Autowired | ||
private UserRepository userRepository; // Make sure you have this repository defined | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
User user = userRepository.findByUsername(username); | ||
if (user == null) { | ||
throw new UsernameNotFoundException("User not found: " + username); | ||
} | ||
|
||
// Convert roles to GrantedAuthority | ||
Set<SimpleGrantedAuthority> authorities = user.getRoles().stream() | ||
.map(role -> new SimpleGrantedAuthority(role.getName())) | ||
.collect(Collectors.toSet()); | ||
|
||
return new org.springframework.security.core.userdetails.User( | ||
user.getUsername(), user.getPassword(), authorities | ||
); | ||
} | ||
} |
48 changes: 20 additions & 28 deletions
48
src/main/java/com/java/vls/employee/portal/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,40 @@ | ||
package com.java.vls.employee.portal.service; | ||
|
||
import com.java.vls.employee.portal.entity.Role; | ||
import com.java.vls.employee.portal.entity.User; | ||
import com.java.vls.employee.portal.repository.RoleRepository; | ||
import com.java.vls.employee.portal.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.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collections; | ||
import javax.transaction.Transactional; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Service | ||
public class UserService implements UserDetailsService { | ||
public class UserService { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
private final UserRepository userRepository; | ||
private final RoleRepository roleRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
@Autowired | ||
private BCryptPasswordEncoder passwordEncoder; | ||
public UserService(UserRepository userRepository, RoleRepository roleRepository, PasswordEncoder passwordEncoder) { | ||
this.userRepository = userRepository; | ||
this.roleRepository = roleRepository; | ||
this.passwordEncoder = passwordEncoder; | ||
} | ||
|
||
@Transactional | ||
public User registerUser(User user) { | ||
// Check if the user already exists | ||
if (userRepository.findByUsername(user.getUsername()) != null) { | ||
throw new RuntimeException("User already exists"); | ||
} | ||
|
||
// Encrypt the password | ||
user.setPassword(passwordEncoder.encode(user.getPassword())); | ||
|
||
// Save the user | ||
Role userRole = roleRepository.findByName("ROLE_USER"); // or any role | ||
user.setRoles(new HashSet<>(Set.of(userRole))); | ||
return userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
// Fetch the user from the database | ||
User user = userRepository.findByUsername(username); | ||
|
||
if (user == null) { | ||
throw new UsernameNotFoundException("User not found"); | ||
} | ||
|
||
// Return a UserDetails object (required by Spring Security) | ||
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), Collections.emptyList()); | ||
public User findByUsername(String username) { | ||
return userRepository.findByUsername(username); | ||
} | ||
} |