Skip to content

Commit

Permalink
feat:蓝盾APP Oauth2授权登录实现 #9353
Browse files Browse the repository at this point in the history
  • Loading branch information
fcfang123 committed Oct 23, 2023
1 parent b71f9be commit e1c13aa
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,13 @@ interface ServiceDeptResource {
@ApiParam("用户名称", required = true)
name: String
): Result<UserAndDeptInfoVo?>

@GET
@Path("/getUserInfoAndWaterMark")
@ApiOperation("获取单个用户信息和水印信息")
fun getUserInfoAndWaterMark(
@HeaderParam(AUTH_HEADER_DEVOPS_USER_ID)
@ApiParam("用户ID", required = true)
userId: String
): Result<UserAndDeptInfoVo?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,7 @@ object AuthMessageCode {
const val ERROR_MONITOR_SPACE_NOT_EXIST = "2121077" // 监控空间不存在
const val ERROR_MONITOR_READ_ONLY_ACTIONS_NOT_EXIST = "2121078" // 业务只读组不存在
const val ERROR_MONITOR_OPS_ACTIONS_NOT_EXIST = "2121079" // 业务运维组不存在

const val ERROR_WATER_MARK_NOT_EXIST = "2121080" // 水印信息不存在
const val ERROR_USER_NOT_EXIST = "2121081" // 用户不存在
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.tencent.devops.auth.pojo.dto

import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

@ApiModel("安全水印")
data class SecOpsWaterMarkDTO(
@ApiModelProperty("场景token")
val token: String,
@ApiModelProperty("用户名称")
val username: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.tencent.devops.auth.pojo.vo

import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

@ApiModel("用户水印信息")
data class SecOpsWaterMarkInfoVo(
@ApiModelProperty("类型")
val type: String,
@ApiModelProperty("水印信息")
val data: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ data class UserAndDeptInfoVo(
@ApiModelProperty("用户部门详细信息")
val deptInfo: List<BkUserDeptInfo>? = null,
@ApiModelProperty("用户额外详细信息")
val extras: BkUserExtras? = null
val extras: BkUserExtras? = null,
@ApiModelProperty("水印信息")
val waterMark: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import com.tencent.devops.auth.service.sample.SamplePermissionRoleMemberService
import com.tencent.devops.auth.service.sample.SamplePermissionRoleService
import com.tencent.devops.auth.service.sample.SamplePermissionSuperManagerService
import com.tencent.devops.auth.service.sample.SamplePermissionUrlServiceImpl
import com.tencent.devops.auth.service.secops.DefaultSecOpsServiceImpl
import com.tencent.devops.auth.service.secops.SecOpsService
import com.tencent.devops.common.client.Client
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.context.annotation.Bean
Expand Down Expand Up @@ -141,4 +143,8 @@ class MockAuthCoreAutoConfiguration {
@Bean
@ConditionalOnMissingBean(AuthMonitorSpaceService::class)
fun sampleAuthMonitorSpaceService() = SampleAuthMonitorSpaceService()

@Bean
@ConditionalOnMissingBean(SecOpsService::class)
fun defaultSecOpsServiceImpl() = DefaultSecOpsServiceImpl()
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ class ServiceDeptResourceImpl @Autowired constructor(
override fun getUserInfo(userId: String, name: String): Result<UserAndDeptInfoVo?> {
return Result(deptService.getUserInfo(userId, name))
}

override fun getUserInfoAndWaterMark(userId: String): Result<UserAndDeptInfoVo?> {
return Result(deptService.getUserInfoAndWaterMark(userId))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,27 @@ import com.tencent.devops.auth.entity.UserDeptTreeInfo
import com.tencent.devops.auth.pojo.vo.BkUserInfoVo
import com.tencent.devops.auth.pojo.vo.DeptInfoVo
import com.tencent.devops.auth.pojo.vo.UserAndDeptInfoVo
import com.tencent.devops.auth.service.secops.SecOpsService
import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.common.api.exception.OperationException
import com.tencent.devops.common.api.util.JsonUtil
import com.tencent.devops.common.api.util.OkhttpUtils
import com.tencent.devops.common.auth.api.pojo.EsbBaseReq
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.common.web.utils.I18nUtil
import java.util.Optional
import java.util.concurrent.TimeUnit
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Request
import okhttp3.RequestBody
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import java.util.Optional
import java.util.concurrent.TimeUnit

class AuthDeptServiceImpl @Autowired constructor(
val redisOperation: RedisOperation,
val objectMapper: ObjectMapper
val objectMapper: ObjectMapper,
val secOpsService: SecOpsService
) : DeptService {

@Value("\${esb.code:#{null}}")
Expand Down Expand Up @@ -263,6 +266,26 @@ class AuthDeptServiceImpl @Autowired constructor(
return userInfoCache.getIfPresent(name)?.get() ?: getUserAndPutInCache(userId, name)
}

override fun getUserInfoAndWaterMark(userId: String): UserAndDeptInfoVo? {
val userInfo = getUserInfo(
userId = userId,
name = userId
) ?: throw ErrorCodeException(
errorCode = AuthMessageCode.ERROR_USER_NOT_EXIST,
defaultMessage = "user not exist!$userId"
)
val userWaterMark = secOpsService.getUserWaterMark(userId = userId)
return UserAndDeptInfoVo(
id = userInfo.id,
name = userInfo.name,
type = userInfo.type,
hasChild = userInfo.hasChild,
deptInfo = userInfo.deptInfo,
extras = userInfo.extras,
waterMark = userWaterMark.data
)
}

private fun getUserAndPutInCache(userId: String, name: String): UserAndDeptInfoVo? {
return getUserAndDeptByName(
name = name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,10 @@ class DefaultDeptServiceImpl : DeptService {
name = name,
type = ManagerScopesEnum.USER
)

override fun getUserInfoAndWaterMark(userId: String): UserAndDeptInfoVo? = UserAndDeptInfoVo(
id = 0,
name = name,
type = ManagerScopesEnum.USER
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ interface DeptService {

// 获取单个用户信息
fun getUserInfo(userId: String, name: String): UserAndDeptInfoVo?

fun getUserInfoAndWaterMark(userId: String): UserAndDeptInfoVo?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.tencent.devops.auth.service.secops

import com.tencent.devops.auth.pojo.vo.SecOpsWaterMarkInfoVo

class DefaultSecOpsServiceImpl : SecOpsService {
override fun getUserWaterMark(userId: String): SecOpsWaterMarkInfoVo {
return SecOpsWaterMarkInfoVo(
type = "",
data = ""
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tencent.devops.auth.service.secops

import com.tencent.devops.auth.pojo.vo.SecOpsWaterMarkInfoVo

/**
* 安全相关接口
*/
interface SecOpsService {
/**
* 获取用户水印信息
*/
fun getUserWaterMark(userId: String): SecOpsWaterMarkInfoVo
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class ApigwUserManagementResourceV4Impl @Autowired constructor(
userId: String
): Result<UserAndDeptInfoVo?> {
logger.info("OPENAPI_GET_USER_INFO_V4|$appCode|$userId")
return client.get(ServiceDeptResource::class).getUserInfo(
userId = userId,
name = userId
return client.get(ServiceDeptResource::class).getUserInfoAndWaterMark(
userId = userId
)
}

Expand Down

0 comments on commit e1c13aa

Please sign in to comment.