Skip to content

Commit

Permalink
add: integrate redis for faster authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
siuyunyip committed Nov 7, 2023
1 parent a805eaa commit 96a5a3d
Show file tree
Hide file tree
Showing 7 changed files with 649 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* @author Siuyun Yip
* @version 1.0
Expand All @@ -14,7 +16,7 @@
@NoArgsConstructor
@Entity
@Table(name = "roles")
public class RoleDO {
public class RoleDO implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -15,6 +16,7 @@
* @date 2023/8/31 15:39
*/


@Data
@NoArgsConstructor
@Entity
Expand All @@ -23,7 +25,7 @@
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email")
})
public class UserDO {
public class UserDO implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
String jwt = parseJwt(request);
if (jwt != null && jwtUtils.validateJwtToken(jwt)) {
String username = jwtUtils.getUserNameFromJwtToken(jwt);
// TODO fetch user data from Redis
UserDetails user = userDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.codemagician.onlinelibrary.domain.entity.UserDO;
import com.codemagician.onlinelibrary.dao.repo.UserRepository;
import com.codemagician.onlinelibrary.common.exception.AccessException;
import com.codemagician.onlinelibrary.util.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
Expand All @@ -23,10 +26,17 @@ public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
UserRepository userRepository;

@Resource
private RedisUtil redisUtil;

@Override
@Transactional
public UserDetails loadUserByUsername(String username) throws AccessException {
UserDO user = userRepository.findByUsernameWithRoles(username).orElseThrow(() -> new AccessException("User Not Found with username: " + username));
UserDO user;
if ((user = (UserDO)redisUtil.get(username)) == null) {
user = userRepository.findByUsernameWithRoles(username).orElseThrow(() -> new AccessException("User Not Found with username: " + username));
redisUtil.set(username, user);
}

return UserDetailsImpl.build(user);
}
Expand Down
Loading

0 comments on commit 96a5a3d

Please sign in to comment.