feat: gateway增加统一鉴权功能
This commit is contained in:
@@ -26,4 +26,6 @@ public interface IClientService extends ISuperService<Client> {
|
||||
void delClient(long id);
|
||||
|
||||
Client loadClientByClientId(String clientId);
|
||||
|
||||
void loadAllClientToCache();
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.central.oauth.service;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.social.security.SocialUserDetails;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
@@ -31,5 +30,5 @@ public interface ZltUserDetailsService extends UserDetailsService {
|
||||
* 根据用户id/openId查询用户
|
||||
* @param userId 用户id/openId
|
||||
*/
|
||||
SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException;
|
||||
UserDetails loadUserByUserId(String userId) throws UsernameNotFoundException;
|
||||
}
|
||||
|
||||
@@ -8,13 +8,15 @@ import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.lock.DistributedLock;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.redis.template.RedisRepository;
|
||||
import com.central.common.service.impl.SuperServiceImpl;
|
||||
import com.central.oauth.mapper.ClientMapper;
|
||||
import com.central.oauth.model.Client;
|
||||
import com.central.oauth.service.IClientService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -29,18 +31,16 @@ import java.util.Map;
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class ClientServiceImpl extends SuperServiceImpl<ClientMapper, Client> implements IClientService {
|
||||
private final static String LOCK_KEY_CLIENTID = "clientId:";
|
||||
|
||||
@Autowired
|
||||
private RedisRepository redisRepository;
|
||||
private final RedissonClient redisson;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Autowired
|
||||
private DistributedLock lock;
|
||||
private final DistributedLock lock;
|
||||
|
||||
@Override
|
||||
public Result saveClient(Client client) throws Exception {
|
||||
@@ -53,6 +53,9 @@ public class ClientServiceImpl extends SuperServiceImpl<ClientMapper, Client> im
|
||||
, LOCK_KEY_CLIENTID+clientId
|
||||
, new QueryWrapper<Client>().eq("client_id", clientId)
|
||||
, clientId + "已存在");
|
||||
|
||||
redisson.getBucket(clientRedisKey(clientId)).set(client);
|
||||
|
||||
return Result.succeed("操作成功");
|
||||
}
|
||||
|
||||
@@ -73,14 +76,27 @@ public class ClientServiceImpl extends SuperServiceImpl<ClientMapper, Client> im
|
||||
public void delClient(long id) {
|
||||
String clientId = baseMapper.selectById(id).getClientId();
|
||||
baseMapper.deleteById(id);
|
||||
redisRepository.del(clientRedisKey(clientId));
|
||||
redisson.getBucket(clientRedisKey(clientId)).delete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Client loadClientByClientId(String clientId) {
|
||||
RBucket<Client> clientBucket = redisson.getBucket(clientRedisKey(clientId));
|
||||
Client client = clientBucket.get();
|
||||
if (client != null) {
|
||||
return client;
|
||||
}
|
||||
QueryWrapper<Client> wrapper = Wrappers.query();
|
||||
wrapper.eq("client_id", clientId);
|
||||
return this.getOne(wrapper);
|
||||
client = this.getOne(wrapper);
|
||||
clientBucket.set(client);
|
||||
return client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadAllClientToCache() {
|
||||
List<Client> clientList = this.list();
|
||||
clientList.forEach(c -> redisson.getBucket(clientRedisKey(c.getClientId())).set(c));
|
||||
}
|
||||
|
||||
private String clientRedisKey(String clientId) {
|
||||
|
||||
@@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.authentication.DisabledException;
|
||||
import org.springframework.security.authentication.InternalAuthenticationServiceException;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.social.security.SocialUserDetails;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -42,7 +41,7 @@ public class UserDetailServiceImpl implements ZltUserDetailsService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SocialUserDetails loadUserByUserId(String openId) {
|
||||
public UserDetails loadUserByUserId(String openId) {
|
||||
LoginAppUser loginAppUser = userService.findByOpenId(openId);
|
||||
return checkUser(loginAppUser);
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.common.redis.template.RedisRepository;
|
||||
import com.central.oauth.exception.ValidateCodeException;
|
||||
import com.central.oauth.service.IValidateCodeService;
|
||||
@@ -61,7 +61,7 @@ public class ValidateCodeServiceImpl implements IValidateCodeService {
|
||||
return Result.failed("验证码未失效,请失效后再次申请");
|
||||
}
|
||||
|
||||
SysUser user = userService.findByMobile(mobile);
|
||||
LoginAppUser user = userService.findByMobile(mobile);
|
||||
if (user == null) {
|
||||
log.error("根据用户手机号{}查询用户为空", mobile);
|
||||
return Result.failed("手机号不存在");
|
||||
|
||||
Reference in New Issue
Block a user