From ca25871f2432648a91aa3998a52f8e2f2b786311 Mon Sep 17 00:00:00 2001 From: "DESKTOP-G9OV8MO\\HP" Date: Mon, 7 Oct 2024 00:37:56 -0400 Subject: [PATCH] code refactor and added audit time log for tables. --- .../portal/controller/AuthController.java | 16 +++++++++------ .../java/vls/employee/portal/entity/User.java | 20 ++++++++++++++++++- .../employee/portal/jwt/JwtRequestFilter.java | 10 ++++++---- .../java/vls/employee/portal/jwt/JwtUtil.java | 2 -- .../service/CustomUserDetailsService.java | 1 + .../employee/portal/service/UserService.java | 2 +- .../vls/employee/portal/entity/UserTest.java | 2 +- 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/java/vls/employee/portal/controller/AuthController.java b/src/main/java/com/java/vls/employee/portal/controller/AuthController.java index 7eaf7c1..18e2132 100644 --- a/src/main/java/com/java/vls/employee/portal/controller/AuthController.java +++ b/src/main/java/com/java/vls/employee/portal/controller/AuthController.java @@ -13,14 +13,18 @@ @RequestMapping("/auth") public class AuthController { - @Autowired - private AuthenticationManager authenticationManager; + private final AuthenticationManager authenticationManager; - @Autowired - private JwtUtil jwtUtil; - @Autowired - private UserService userService; + private final JwtUtil jwtUtil; + + private final UserService userService; + + public AuthController(AuthenticationManager authenticationManager, JwtUtil jwtUtil, UserService userService){ + this.authenticationManager = authenticationManager; + this.jwtUtil = jwtUtil; + this.userService = userService; + } @PostMapping("/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password) throws Exception { diff --git a/src/main/java/com/java/vls/employee/portal/entity/User.java b/src/main/java/com/java/vls/employee/portal/entity/User.java index f4b25ae..81c73b5 100644 --- a/src/main/java/com/java/vls/employee/portal/entity/User.java +++ b/src/main/java/com/java/vls/employee/portal/entity/User.java @@ -1,9 +1,15 @@ package com.java.vls.employee.portal.entity; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Getter; import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; +import org.springframework.data.annotation.CreatedDate; import javax.persistence.*; +import java.time.LocalDateTime; import java.util.HashSet; import java.util.Set; @@ -11,6 +17,7 @@ @Table(name = "users") @Getter @Setter +@JsonInclude(JsonInclude.Include.NON_NULL) public class User { @Id @@ -34,14 +41,25 @@ public class User { ) private Set roles = new HashSet<>(); + private transient String role; + + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) + @CreationTimestamp + private LocalDateTime createdDate; + + @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) + @UpdateTimestamp + private LocalDateTime updatedDate; + // Constructors, Getters, and Setters public User() {} - public User(String username, String password, String email) { + public User(String username, String password, String email,String role) { this.username = username; this.password = password; this.email = email; + this.role = role; } // Getters and setters... diff --git a/src/main/java/com/java/vls/employee/portal/jwt/JwtRequestFilter.java b/src/main/java/com/java/vls/employee/portal/jwt/JwtRequestFilter.java index f952a76..aeff0aa 100644 --- a/src/main/java/com/java/vls/employee/portal/jwt/JwtRequestFilter.java +++ b/src/main/java/com/java/vls/employee/portal/jwt/JwtRequestFilter.java @@ -18,12 +18,14 @@ @Component public class JwtRequestFilter extends OncePerRequestFilter { - @Autowired - private JwtUtil jwtUtil; // Your JWT utility class + private final JwtUtil jwtUtil; // Your JWT utility class - @Autowired - private UserDetailsService userDetailsService; // Ensure this is your CustomUserDetailsService + private final UserDetailsService userDetailsService; // Ensure this is your CustomUserDetailsService + public JwtRequestFilter(JwtUtil jwtUtil,UserDetailsService userDetailsService) { + this.jwtUtil = jwtUtil; + this.userDetailsService = userDetailsService; + } @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { diff --git a/src/main/java/com/java/vls/employee/portal/jwt/JwtUtil.java b/src/main/java/com/java/vls/employee/portal/jwt/JwtUtil.java index 1b072e5..0e465f7 100644 --- a/src/main/java/com/java/vls/employee/portal/jwt/JwtUtil.java +++ b/src/main/java/com/java/vls/employee/portal/jwt/JwtUtil.java @@ -22,8 +22,6 @@ public class JwtUtil { private static final long EXPIRATION_TIME = 864_000_000; // 10 days - private static final SecretKey KEY = Keys.secretKeyFor(SignatureAlgorithm.HS256); - // Constructor to initialize the secret key public JwtUtil(@Value("${jwt.secret}") String secret) { byte[] decodedKey = Base64.getDecoder().decode(secret); diff --git a/src/main/java/com/java/vls/employee/portal/service/CustomUserDetailsService.java b/src/main/java/com/java/vls/employee/portal/service/CustomUserDetailsService.java index fc93562..516f2bc 100644 --- a/src/main/java/com/java/vls/employee/portal/service/CustomUserDetailsService.java +++ b/src/main/java/com/java/vls/employee/portal/service/CustomUserDetailsService.java @@ -21,6 +21,7 @@ public class CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); + if (user == null) { throw new UsernameNotFoundException("User not found: " + username); } diff --git a/src/main/java/com/java/vls/employee/portal/service/UserService.java b/src/main/java/com/java/vls/employee/portal/service/UserService.java index 24e074a..922b82c 100644 --- a/src/main/java/com/java/vls/employee/portal/service/UserService.java +++ b/src/main/java/com/java/vls/employee/portal/service/UserService.java @@ -28,7 +28,7 @@ public UserService(UserRepository userRepository, RoleRepository roleRepository, @Transactional public User registerUser(User user) { user.setPassword(passwordEncoder.encode(user.getPassword())); - Role userRole = roleRepository.findByName("ROLE_USER"); // or any role + Role userRole = roleRepository.findByName(user.getRole()); // or any role user.setRoles(new HashSet<>(Set.of(userRole))); return userRepository.save(user); } diff --git a/src/test/java/com/java/vls/employee/portal/entity/UserTest.java b/src/test/java/com/java/vls/employee/portal/entity/UserTest.java index 06cfd37..fcf3513 100644 --- a/src/test/java/com/java/vls/employee/portal/entity/UserTest.java +++ b/src/test/java/com/java/vls/employee/portal/entity/UserTest.java @@ -11,7 +11,7 @@ class UserTest { @BeforeEach void setUp() { - userUnderTest = new User("username", "password", "email"); + userUnderTest = new User("username", "password", "email",""); } @Test