Skip to content

Commit

Permalink
code refactor and added audit time log for tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinodbalakumar committed Oct 7, 2024
1 parent c4a6f37 commit ca25871
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/java/vls/employee/portal/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
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;

@Entity
@Table(name = "users")
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {

@Id
Expand All @@ -34,14 +41,25 @@ public class User {
)
private Set<Role> 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...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/java/vls/employee/portal/jwt/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UserTest {

@BeforeEach
void setUp() {
userUnderTest = new User("username", "password", "email");
userUnderTest = new User("username", "password", "email","");
}

@Test
Expand Down

0 comments on commit ca25871

Please sign in to comment.