parent
bb90b69df0
commit
fd91bd15ca
@ -1,19 +1,97 @@ |
||||
package net.lensfrex.dscape.auth; |
||||
|
||||
import cn.dev33.satoken.stp.StpInterface; |
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import net.lensfrex.dscape.dao.entity.RolePermission; |
||||
import net.lensfrex.dscape.dao.entity.UserRole; |
||||
import net.lensfrex.dscape.dao.service.RolePermissionService; |
||||
import net.lensfrex.dscape.dao.service.UserRoleService; |
||||
import net.lensfrex.dscape.dao.service.impl.UserRoleServiceImpl; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
@Slf4j |
||||
@Component |
||||
public class StpInterfaceImpl implements StpInterface { |
||||
|
||||
private final UserRoleService userRoleService; |
||||
private final RolePermissionService rolePermissionService; |
||||
|
||||
private final RedisTemplate<String, String> redis; |
||||
|
||||
@Autowired |
||||
public StpInterfaceImpl(RedisTemplate<String, String> redis, |
||||
UserRoleService userRoleService, |
||||
RolePermissionService rolePermissionService) { |
||||
|
||||
this.userRoleService = userRoleService; |
||||
this.rolePermissionService = rolePermissionService; |
||||
this.redis = redis; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public List<String> getPermissionList(Object loginId, String loginType) { |
||||
return null; |
||||
String uid = (String) loginId; |
||||
|
||||
List<String> userRoles = this.getRoleList(loginId, loginType); |
||||
List<String> rolePermissions = new ArrayList<>(); |
||||
|
||||
for (String role : userRoles) { |
||||
String redisRolePermissionKey = "dscape:auth:role.permission:" + role; |
||||
|
||||
if (Boolean.FALSE.equals(redis.hasKey(redisRolePermissionKey))) { |
||||
log.debug("角色 " + uid + " 的权限信息缓存不存在,从数据库中查找并放入缓存"); |
||||
|
||||
rolePermissions.addAll(rolePermissionService.getPermissions(role)); |
||||
if (rolePermissions.isEmpty()) { |
||||
return rolePermissions; |
||||
} |
||||
|
||||
redis.opsForList().leftPushAll(redisRolePermissionKey, rolePermissions); |
||||
} else { |
||||
List<String> cachedUserRole = redis.opsForList().range(redisRolePermissionKey, 0, -1); |
||||
if (cachedUserRole == null) { |
||||
return rolePermissions; |
||||
} |
||||
|
||||
rolePermissions.addAll(cachedUserRole); |
||||
} |
||||
} |
||||
|
||||
return rolePermissions; |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getRoleList(Object loginId, String loginType) { |
||||
return null; |
||||
String uid = (String) loginId; |
||||
String redisUserRoleKey = "dscape:auth:user.role:" + uid; |
||||
|
||||
List<String> userRoleList = new ArrayList<>(2); |
||||
if (Boolean.FALSE.equals(redis.hasKey(redisUserRoleKey))) { |
||||
log.debug("用户 " + uid + " 的角色信息缓存不存在,从数据库中查找并放入缓存"); |
||||
|
||||
userRoleList.addAll(userRoleService.getRoles(uid)); |
||||
|
||||
if (userRoleList.isEmpty()) { |
||||
return userRoleList; |
||||
} |
||||
|
||||
redis.opsForList().leftPushAll(redisUserRoleKey, userRoleList); |
||||
} else { |
||||
List<String> cachedUserRole = redis.opsForList().range(redisUserRoleKey, 0, -1); |
||||
if (cachedUserRole == null) { |
||||
return userRoleList; |
||||
} |
||||
|
||||
userRoleList.addAll(cachedUserRole); |
||||
} |
||||
|
||||
return userRoleList; |
||||
} |
||||
} |
@ -0,0 +1,52 @@ |
||||
package net.lensfrex.dscape.dao.entity; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import java.time.LocalDateTime; |
||||
import java.io.Serializable; |
||||
import com.baomidou.mybatisplus.annotation.IdType; |
||||
import com.baomidou.mybatisplus.annotation.TableId; |
||||
|
||||
/** |
||||
* @description role_permission |
||||
* @author lensfrex |
||||
* @date 2022-08-17 |
||||
*/ |
||||
@Data |
||||
public class RolePermission implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
@TableId(type = IdType.AUTO) |
||||
/** |
||||
* id |
||||
*/ |
||||
private Long id; |
||||
|
||||
/** |
||||
* 角色id |
||||
*/ |
||||
private Integer role; |
||||
|
||||
/** |
||||
* 权限id |
||||
*/ |
||||
private Integer permission; |
||||
|
||||
/** |
||||
* create_time |
||||
*/ |
||||
private LocalDateTime createTime; |
||||
|
||||
/** |
||||
* edit_time |
||||
*/ |
||||
private LocalDateTime editTime; |
||||
|
||||
/** |
||||
* is_deleted |
||||
*/ |
||||
private int isDeleted; |
||||
|
||||
public RolePermission() {} |
||||
} |
@ -0,0 +1,52 @@ |
||||
package net.lensfrex.dscape.dao.entity; |
||||
|
||||
import lombok.Data; |
||||
import java.io.Serializable; |
||||
import java.time.LocalDateTime; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType; |
||||
import com.baomidou.mybatisplus.annotation.TableId; |
||||
|
||||
/** |
||||
* @description user_role |
||||
* @author lensfrex |
||||
* @date 2022-08-17 |
||||
*/ |
||||
@Data |
||||
public class UserRole implements Serializable { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
/** |
||||
* id |
||||
*/ |
||||
@TableId(type = IdType.AUTO) |
||||
private Long id; |
||||
|
||||
/** |
||||
* 角色id |
||||
*/ |
||||
private Integer role; |
||||
|
||||
/** |
||||
* 用户uid |
||||
*/ |
||||
private String uid; |
||||
|
||||
/** |
||||
* create_time |
||||
*/ |
||||
private LocalDateTime createTime; |
||||
|
||||
/** |
||||
* edit_time |
||||
*/ |
||||
private LocalDateTime editTime; |
||||
|
||||
/** |
||||
* is_deleted |
||||
*/ |
||||
private int isDeleted; |
||||
|
||||
public UserRole() {} |
||||
} |
@ -0,0 +1,15 @@ |
||||
package net.lensfrex.dscape.dao.mapper; |
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import org.apache.ibatis.annotations.Mapper; |
||||
import org.apache.ibatis.annotations.Select; |
||||
import net.lensfrex.dscape.dao.entity.RolePermission; |
||||
import java.util.List; |
||||
/** |
||||
* @description role_permissionMapper |
||||
* @author lensfrex |
||||
* @date 2022-08-17 |
||||
*/ |
||||
@Mapper |
||||
public interface RolePermissionMapper extends BaseMapper<RolePermission> { |
||||
|
||||
} |
@ -0,0 +1,15 @@ |
||||
package net.lensfrex.dscape.dao.mapper; |
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import org.apache.ibatis.annotations.Mapper; |
||||
import org.apache.ibatis.annotations.Select; |
||||
import net.lensfrex.dscape.dao.entity.UserRole; |
||||
import java.util.List; |
||||
/** |
||||
* @description user_roleMapper |
||||
* @author lensfrex |
||||
* @date 2022-08-17 |
||||
*/ |
||||
@Mapper |
||||
public interface UserRoleMapper extends BaseMapper<UserRole> { |
||||
|
||||
} |
@ -0,0 +1,22 @@ |
||||
package net.lensfrex.dscape.dao.service; |
||||
import net.lensfrex.dscape.dao.entity.RolePermission; |
||||
import org.apache.ibatis.annotations.Select; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.stereotype.Service; |
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @description role_permission服务层 |
||||
* @author lensfrex |
||||
* @date 2022-08-17 |
||||
*/ |
||||
@Service |
||||
public interface RolePermissionService extends IService<RolePermission> { |
||||
@Select("select permission from role_permission where role = #{role} and is_deleted = 0") |
||||
List<String> getPermissions(String role); |
||||
|
||||
@Select("select permission from role_permission where role = #{role} and is_deleted = 0") |
||||
List<String> getPermissions(List<String> roles); |
||||
} |
@ -0,0 +1,19 @@ |
||||
package net.lensfrex.dscape.dao.service; |
||||
import net.lensfrex.dscape.dao.entity.UserRole; |
||||
import org.apache.ibatis.annotations.Select; |
||||
import org.springframework.stereotype.Service; |
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @description user_role服务层 |
||||
* @author lensfrex |
||||
* @date 2022-08-17 |
||||
*/ |
||||
@Service |
||||
public interface UserRoleService extends IService<UserRole> { |
||||
|
||||
@Select("select role from user_role where uid = '#{uid}' and is_deleted != 1") |
||||
List<String> getRoles(String uid); |
||||
} |
@ -0,0 +1,51 @@ |
||||
/* |
||||
* Class created by lensfrex. |
||||
*/ |
||||
|
||||
package net.lensfrex.dscape.dao.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import net.lensfrex.dscape.dao.entity.RolePermission; |
||||
import net.lensfrex.dscape.dao.mapper.RolePermissionMapper; |
||||
import net.lensfrex.dscape.dao.service.RolePermissionService; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
@Service |
||||
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService { |
||||
@Override |
||||
public List<String> getPermissions(String role) { |
||||
QueryWrapper<RolePermission> wrapper = new QueryWrapper<>(); |
||||
wrapper.lambda() |
||||
.select(RolePermission::getPermission) |
||||
.eq(RolePermission::getRole, role) |
||||
.eq(RolePermission::getIsDeleted, 0); |
||||
|
||||
return this.getPermissions(wrapper); |
||||
} |
||||
|
||||
@Override |
||||
public List<String> getPermissions(List<String> roles) { |
||||
QueryWrapper<RolePermission> wrapper = new QueryWrapper<>(); |
||||
wrapper.lambda() |
||||
.select(RolePermission::getPermission) |
||||
.in(RolePermission::getRole, roles) |
||||
.eq(RolePermission::getIsDeleted, 0); |
||||
|
||||
return this.getPermissions(wrapper); |
||||
} |
||||
|
||||
private List<String> getPermissions(QueryWrapper<RolePermission> wrapper) { |
||||
|
||||
List<RolePermission> rolePermissionList = this.list(wrapper); |
||||
|
||||
List<String > permissions = new ArrayList<>(); |
||||
rolePermissionList.forEach(rolePermission -> permissions.add(rolePermission.getPermission().toString())); |
||||
|
||||
return permissions; |
||||
} |
||||
} |
@ -0,0 +1,37 @@ |
||||
/* |
||||
* Class created by lensfrex. |
||||
*/ |
||||
|
||||
package net.lensfrex.dscape.dao.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import net.lensfrex.dscape.dao.entity.UserRole; |
||||
import net.lensfrex.dscape.dao.mapper.UserRoleMapper; |
||||
import net.lensfrex.dscape.dao.service.UserRoleService; |
||||
import org.apache.ibatis.annotations.Select; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
@Service |
||||
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService { |
||||
|
||||
@Override |
||||
public List<String> getRoles(String uid) { |
||||
QueryWrapper<UserRole> wrapper = new QueryWrapper<>(); |
||||
wrapper.lambda() |
||||
.select(UserRole::getRole) |
||||
.eq(UserRole::getUid, uid) |
||||
.eq(UserRole::getIsDeleted, 0); |
||||
|
||||
List<UserRole> userRoleList = this.list(wrapper); |
||||
List<String> roles = new ArrayList<>(); |
||||
|
||||
userRoleList.forEach(userRole -> roles.add(userRole.getRole().toString())); |
||||
|
||||
return roles; |
||||
} |
||||
} |
@ -0,0 +1,15 @@ |
||||
|
||||
package net.lensfrex.dscape.dto.request; |
||||
|
||||
import javax.annotation.Generated; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class UserLoginRequestBody { |
||||
private String password; |
||||
|
||||
@JsonProperty("user_name") |
||||
private String userName; |
||||
} |
@ -0,0 +1,18 @@ |
||||
/* |
||||
* Class created by lensfrex. |
||||
*/ |
||||
|
||||
package net.lensfrex.dscape.web.service.user; |
||||
|
||||
import cn.dev33.satoken.stp.SaTokenInfo; |
||||
import cn.dev33.satoken.stp.StpUtil; |
||||
import net.lensfrex.dscape.dto.request.UserLoginRequestBody; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
@Service |
||||
public class UserService { |
||||
public SaTokenInfo login(UserLoginRequestBody requestBody) { |
||||
StpUtil.login(requestBody.getUserName()); |
||||
return StpUtil.getTokenInfo(); |
||||
} |
||||
} |
@ -0,0 +1,20 @@ |
||||
/* |
||||
* Class created by lensfrex. |
||||
*/ |
||||
|
||||
package net.lensfrex.dscape.test; |
||||
|
||||
import cn.dev33.satoken.stp.StpUtil; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
||||
@Slf4j |
||||
@SpringBootTest |
||||
public class TestMain { |
||||
|
||||
@Test |
||||
public void testPermission() { |
||||
StpUtil.getPermissionList("114514asdf").forEach(System.out::println); |
||||
} |
||||
} |
Loading…
Reference in new issue