Fix #3269 When nacos.core.auth.enabled = true, user information is saved to the request in login (#3543)

* Fix #3269 NacosAuthManager request.setAttribute(user) and ConfigController getSrcUserName(req)

* Fix #3269 format code

* Fix #3269 format code
This commit is contained in:
lidaoyang 2020-08-07 12:43:44 +08:00 committed by GitHub
parent 9734deeefc
commit fae37481e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 22 deletions

View File

@ -134,6 +134,7 @@ public class ConfigController {
final String srcIp = RequestUtil.getRemoteIp(request);
final String requestIpApp = RequestUtil.getAppName(request);
srcUser = RequestUtil.getSrcUserName(request);
// check tenant
ParamUtils.checkTenant(tenant);
ParamUtils.checkParam(dataId, group, "datumId", content);
@ -183,8 +184,8 @@ public class ConfigController {
* Get configure board infomation fail.
*
* @throws ServletException ServletException.
* @throws IOException IOException.
* @throws NacosException NacosException.
* @throws IOException IOException.
* @throws NacosException NacosException.
*/
@GetMapping
@Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class)
@ -239,10 +240,11 @@ public class ConfigController {
ParamUtils.checkParam(dataId, group, "datumId", "rm");
ParamUtils.checkParam(tag);
String clientIp = RequestUtil.getRemoteIp(request);
String srcUser = RequestUtil.getSrcUserName(request);
if (StringUtils.isBlank(tag)) {
persistService.removeConfigInfo(dataId, group, tenant, clientIp, null);
persistService.removeConfigInfo(dataId, group, tenant, clientIp, srcUser);
} else {
persistService.removeConfigInfoTag(dataId, group, tenant, tag, clientIp, null);
persistService.removeConfigInfoTag(dataId, group, tenant, tag, clientIp, srcUser);
}
final Timestamp time = TimeUtils.getCurrentTime();
ConfigTraceService.logPersistenceEvent(dataId, group, tenant, null, time.getTime(), clientIp,
@ -394,7 +396,7 @@ public class ConfigController {
* Execute to remove beta operation.
*
* @param dataId dataId string value.
* @param group group string value.
* @param group group string value.
* @param tenant tenant string value.
* @return Execute to operate result.
*/
@ -425,7 +427,7 @@ public class ConfigController {
* Execute to query beta operation.
*
* @param dataId dataId string value.
* @param group group string value.
* @param group group string value.
* @param tenant tenant string value.
* @return RestResult for ConfigInfo4Beta.
*/
@ -452,11 +454,11 @@ public class ConfigController {
/**
* Execute export config operation.
*
* @param dataId dataId string value.
* @param group group string value.
* @param dataId dataId string value.
* @param group group string value.
* @param appName appName string value.
* @param tenant tenant string value.
* @param ids id list value.
* @param tenant tenant string value.
* @param ids id list value.
* @return ResponseEntity.
*/
@GetMapping(params = "export=true")
@ -504,11 +506,11 @@ public class ConfigController {
/**
* Execute import and publish config operation.
*
* @param request http servlet request .
* @param srcUser src user string value.
* @param request http servlet request .
* @param srcUser src user string value.
* @param namespace namespace string value.
* @param policy policy model.
* @param file MultipartFile.
* @param policy policy model.
* @param file MultipartFile.
* @return RestResult Map.
* @throws NacosException NacosException.
*/
@ -605,11 +607,11 @@ public class ConfigController {
/**
* Execute clone config operation.
*
* @param request http servlet request .
* @param srcUser src user string value.
* @param namespace namespace string value.
* @param request http servlet request .
* @param srcUser src user string value.
* @param namespace namespace string value.
* @param configBeansList config beans list.
* @param policy config policy model.
* @param policy config policy model.
* @return RestResult for map.
* @throws NacosException NacosException.
*/

View File

@ -475,6 +475,7 @@ public final class RowMapperManager {
configHistoryInfo.setTenant(rs.getString("tenant_id"));
configHistoryInfo.setAppName(rs.getString("app_name"));
configHistoryInfo.setSrcIp(rs.getString("src_ip"));
configHistoryInfo.setSrcUser(rs.getString("src_user"));
configHistoryInfo.setOpType(rs.getString("op_type"));
configHistoryInfo.setCreatedTime(rs.getTimestamp("gmt_create"));
configHistoryInfo.setLastModifiedTime(rs.getTimestamp("gmt_modified"));

View File

@ -2005,7 +2005,7 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
int pageSize) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
String sqlCountRows = "select count(*) from his_config_info where data_id = ? and group_id = ? and tenant_id = ?";
String sqlFetchRows = "select nid,data_id,group_id,tenant_id,app_name,src_ip,op_type,gmt_create,gmt_modified from his_config_info where data_id = ? and group_id = ? and tenant_id = ? order by nid desc";
String sqlFetchRows = "select nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,gmt_create,gmt_modified from his_config_info where data_id = ? and group_id = ? and tenant_id = ? order by nid desc";
PaginationHelper<ConfigHistoryInfo> helper = createPaginationHelper();
return helper.fetchPage(sqlCountRows, sqlFetchRows, new Object[] {dataId, group, tenantTmp}, pageNo, pageSize,

View File

@ -2240,7 +2240,7 @@ public class ExternalStoragePersistServiceImpl implements PersistService {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
String sqlCountRows = "select count(*) from his_config_info where data_id = ? and group_id = ? and tenant_id = ?";
String sqlFetchRows =
"select nid,data_id,group_id,tenant_id,app_name,src_ip,op_type,gmt_create,gmt_modified from his_config_info "
"select nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,gmt_create,gmt_modified from his_config_info "
+ "where data_id = ? and group_id = ? and tenant_id = ? order by nid desc";
Page<ConfigHistoryInfo> page = null;
@ -2249,7 +2249,8 @@ public class ExternalStoragePersistServiceImpl implements PersistService {
.fetchPage(sqlCountRows, sqlFetchRows, new Object[] {dataId, group, tenantTmp}, pageNo, pageSize,
HISTORY_LIST_ROW_MAPPER);
} catch (DataAccessException e) {
LogUtil.FATAL_LOG.error("[list-config-history] error, dataId:{}, group:{}", new Object[] {dataId, group}, e);
LogUtil.FATAL_LOG
.error("[list-config-history] error, dataId:{}, group:{}", new Object[] {dataId, group}, e);
throw e;
}
return page;

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.config.server.utils;
import com.alibaba.nacos.core.auth.User;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
@ -35,6 +36,8 @@ public class RequestUtil {
public static final String CLIENT_APPNAME_HEADER = "Client-AppName";
public static final String NACOS_USER_KEY = "nacosuser";
/**
* get real client ip
*
@ -63,4 +66,31 @@ public class RequestUtil {
return request.getHeader(CLIENT_APPNAME_HEADER);
}
/**
* Gets the user of the client application in the Attribute.
*
* @param request {@link HttpServletRequest}
* @return may be return null
*/
public static User getUser(HttpServletRequest request) {
Object userObj = request.getAttribute(NACOS_USER_KEY);
if (userObj == null) {
return null;
}
User user = (User) userObj;
return user;
}
/**
* Gets the username of the client application in the Attribute.
*
* @param request {@link HttpServletRequest}
* @return may be return null
*/
public static String getSrcUserName(HttpServletRequest request) {
User user = getUser(request);
return user == null ? null : user.getUserName();
}
}

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.console.security.nacos;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.config.server.auth.RoleInfo;
import com.alibaba.nacos.config.server.utils.RequestUtil;
import com.alibaba.nacos.console.security.nacos.roles.NacosRoleServiceImpl;
import com.alibaba.nacos.console.security.nacos.users.NacosUser;
import com.alibaba.nacos.core.auth.AccessException;
@ -90,7 +91,7 @@ public class NacosAuthManager implements AuthManager {
}
}
}
req.setAttribute(RequestUtil.NACOS_USER_KEY, user);
return user;
}