♻️ Refactoring code. #2115 认证中心 和 数据库解耦,通过feign 调用数据源

This commit is contained in:
lengleng committed 2020-12-07 11:36:14 +08:00
1 parent f0b24a8e40
commit 011d25de6b
5 files changed
+78 -27

No files matched your search

-15
View File
@@ -58,11 +58,6 @@
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-feign</artifactId>
</dependency>
<!--mysql 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--缓存操作-->
<dependency>
<groupId>com.pig4cloud</groupId>
@@ -78,16 +73,6 @@
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-gray</artifactId>
</dependency>
<!--JDBC相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- druid 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -1,16 +1,18 @@
package com.pig4cloud.pigx.auth.service;
import com.pig4cloud.pigx.admin.api.entity.SysOauthClientDetails;
import com.pig4cloud.pigx.admin.api.feign.RemoteClientDetailsService;
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
/**
* @author lengleng
* @date 2020/03/25
@@ -18,11 +20,10 @@ import javax.sql.DataSource;
* 扩展 JdbcClientDetailsService 支持多租户
*/
@Service
public class PigxClientDetailsServiceImpl extends JdbcClientDetailsService {
@RequiredArgsConstructor
public class PigxClientDetailsServiceImpl implements ClientDetailsService {
public PigxClientDetailsServiceImpl(DataSource dataSource) {
super(dataSource);
}
private final RemoteClientDetailsService clientDetailsService;
/**
* 重写原生方法支持redis缓存
@@ -33,9 +34,18 @@ public class PigxClientDetailsServiceImpl extends JdbcClientDetailsService {
@Override
@Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
public ClientDetails loadClientByClientId(String clientId) {
super.setSelectClientDetailsSql(
String.format(SecurityConstants.DEFAULT_SELECT_STATEMENT, TenantContextHolder.getTenantId()));
return super.loadClientByClientId(clientId);
SysOauthClientDetails clientDetails = clientDetailsService
.getClientDetailsById(clientId, SecurityConstants.FROM_IN).getData();
if (clientDetails == null) {
return null;
}
// copy原值
BaseClientDetails client = new BaseClientDetails();
BeanUtils.copyProperties(clientDetails, client);
client.setClientSecret(String.format("{noop}%s", clientDetails.getClientSecret()));
return client;
}
}
@@ -65,7 +65,7 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
public UserDetails loadUserByUsername(String username) {
Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
if (cache != null && cache.get(username) != null) {
return (PigxUser) cache.get(username).get();
return cache.get(username, PigxUser.class);
}
R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN);
@@ -0,0 +1,48 @@
/*
*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*
*/
package com.pig4cloud.pigx.admin.api.feign;
import com.pig4cloud.pigx.admin.api.entity.SysOauthClientDetails;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.constant.ServiceNameConstants;
import com.pig4cloud.pigx.common.core.util.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
/**
* @author lengleng
* @date 2020/12/05
*/
@FeignClient(contextId = "remoteClientDetailsService", value = ServiceNameConstants.UPMS_SERVICE)
public interface RemoteClientDetailsService {
/**
* 通过clientId 查询客户端信息
* @param clientId 用户名
* @param from 调用标志
* @return R
*/
@GetMapping("/client/getClientDetailsById/{clientId}")
R<SysOauthClientDetails> getClientDetailsById(@PathVariable("clientId") String clientId,
@RequestHeader(SecurityConstants.FROM) String from);
}
@@ -26,6 +26,7 @@ import com.pig4cloud.pigx.admin.api.entity.SysOauthClientDetails;
import com.pig4cloud.pigx.admin.service.SysOauthClientDetailsService;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
@@ -107,4 +108,11 @@ public class SysClientController {
return R.ok(clientDetailsService.updateClientById(clientDetailsDTO));
}
@Inner(false)
@GetMapping("/getClientDetailsById/{clientId}")
public R getClientDetailsById(@PathVariable String clientId) {
return R.ok(clientDetailsService.getOne(
Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId), false));
}
}