From 1516dd709ac60023b3637fe35a22db8e4b00d52b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Sun, 21 Jul 2024 16:05:39 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20=20NoToken=20Feign=20?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E8=87=AA=E5=8A=A8=E7=BB=B4=E6=8A=A4header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PigRemoteRegisteredClientRepository.java | 163 ++++---- .../admin/controller/SysDictController.java | 365 +++++++++--------- 2 files changed, 257 insertions(+), 271 deletions(-) diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/service/PigRemoteRegisteredClientRepository.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/service/PigRemoteRegisteredClientRepository.java index 9e7c217c..7d805801 100644 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/service/PigRemoteRegisteredClientRepository.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/service/PigRemoteRegisteredClientRepository.java @@ -33,98 +33,101 @@ import java.util.Optional; @RequiredArgsConstructor public class PigRemoteRegisteredClientRepository implements RegisteredClientRepository { - /** - * 刷新令牌有效期默认 30 天 - */ - private final static int refreshTokenValiditySeconds = 60 * 60 * 24 * 30; + /** + * 刷新令牌有效期默认 30 天 + */ + private final static int refreshTokenValiditySeconds = 60 * 60 * 24 * 30; - /** - * 请求令牌有效期默认 12 小时 - */ - private final static int accessTokenValiditySeconds = 60 * 60 * 12; + /** + * 请求令牌有效期默认 12 小时 + */ + private final static int accessTokenValiditySeconds = 60 * 60 * 12; - private final RemoteClientDetailsService clientDetailsService; + private final RemoteClientDetailsService clientDetailsService; - /** - * Saves the registered client. - * - *

- * IMPORTANT: Sensitive information should be encoded externally from the - * implementation, e.g. {@link RegisteredClient#getClientSecret()} - * @param registeredClient the {@link RegisteredClient} - */ - @Override - public void save(RegisteredClient registeredClient) { - } + /** + * Saves the registered client. + * + *

+ * IMPORTANT: Sensitive information should be encoded externally from the + * implementation, e.g. {@link RegisteredClient#getClientSecret()} + * + * @param registeredClient the {@link RegisteredClient} + */ + @Override + public void save(RegisteredClient registeredClient) { + } - /** - * Returns the registered client identified by the provided {@code id}, or - * {@code null} if not found. - * @param id the registration identifier - * @return the {@link RegisteredClient} if found, otherwise {@code null} - */ - @Override - public RegisteredClient findById(String id) { - throw new UnsupportedOperationException(); - } + /** + * Returns the registered client identified by the provided {@code id}, or + * {@code null} if not found. + * + * @param id the registration identifier + * @return the {@link RegisteredClient} if found, otherwise {@code null} + */ + @Override + public RegisteredClient findById(String id) { + throw new UnsupportedOperationException(); + } - /** - * Returns the registered client identified by the provided {@code clientId}, or - * {@code null} if not found. - * @param clientId the client identifier - * @return the {@link RegisteredClient} if found, otherwise {@code null} - */ + /** + * Returns the registered client identified by the provided {@code clientId}, or + * {@code null} if not found. + * @param clientId the client identifier + * @return the {@link RegisteredClient} if found, otherwise {@code null} + */ - /** - * 重写原生方法支持redis缓存 - * @param clientId - * @return - */ - @Override - @SneakyThrows - @Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null") - public RegisteredClient findByClientId(String clientId) { + /** + * 重写原生方法支持redis缓存 + * + * @param clientId + * @return + */ + @Override + @SneakyThrows + @Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null") + public RegisteredClient findByClientId(String clientId) { - SysOauthClientDetails clientDetails = RetOps - .of(clientDetailsService.getClientDetailsById(clientId, SecurityConstants.FROM_IN)) - .getData() - .orElseThrow(() -> new OAuth2AuthorizationCodeRequestAuthenticationException( - new OAuth2Error("客户端查询异常,请检查数据库链接"), null)); + SysOauthClientDetails clientDetails = RetOps + .of(clientDetailsService.getClientDetailsById(clientId)) + .getData() + .orElseThrow(() -> new OAuth2AuthorizationCodeRequestAuthenticationException( + new OAuth2Error("客户端查询异常,请检查数据库链接"), null)); - RegisteredClient.Builder builder = RegisteredClient.withId(clientDetails.getClientId()) - .clientId(clientDetails.getClientId()) - .clientSecret(SecurityConstants.NOOP + clientDetails.getClientSecret()) - .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC); + RegisteredClient.Builder builder = RegisteredClient.withId(clientDetails.getClientId()) + .clientId(clientDetails.getClientId()) + .clientSecret(SecurityConstants.NOOP + clientDetails.getClientSecret()) + .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC); - for (String authorizedGrantType : clientDetails.getAuthorizedGrantTypes()) { - builder.authorizationGrantType(new AuthorizationGrantType(authorizedGrantType)); - } + for (String authorizedGrantType : clientDetails.getAuthorizedGrantTypes()) { + builder.authorizationGrantType(new AuthorizationGrantType(authorizedGrantType)); + } - // 回调地址 - Optional.ofNullable(clientDetails.getWebServerRedirectUri()) - .ifPresent(redirectUri -> Arrays.stream(redirectUri.split(StrUtil.COMMA)) - .filter(StrUtil::isNotBlank) - .forEach(builder::redirectUri)); + // 回调地址 + Optional.ofNullable(clientDetails.getWebServerRedirectUri()) + .ifPresent(redirectUri -> Arrays.stream(redirectUri.split(StrUtil.COMMA)) + .filter(StrUtil::isNotBlank) + .forEach(builder::redirectUri)); - // scope - Optional.ofNullable(clientDetails.getScope()) - .ifPresent(scope -> Arrays.stream(scope.split(StrUtil.COMMA)) - .filter(StrUtil::isNotBlank) - .forEach(builder::scope)); + // scope + Optional.ofNullable(clientDetails.getScope()) + .ifPresent(scope -> Arrays.stream(scope.split(StrUtil.COMMA)) + .filter(StrUtil::isNotBlank) + .forEach(builder::scope)); - return builder - .tokenSettings(TokenSettings.builder() - .accessTokenFormat(OAuth2TokenFormat.REFERENCE) - .accessTokenTimeToLive(Duration.ofSeconds( - Optional.ofNullable(clientDetails.getAccessTokenValidity()).orElse(accessTokenValiditySeconds))) - .refreshTokenTimeToLive(Duration.ofSeconds(Optional.ofNullable(clientDetails.getRefreshTokenValidity()) - .orElse(refreshTokenValiditySeconds))) - .build()) - .clientSettings(ClientSettings.builder() - .requireAuthorizationConsent(!BooleanUtil.toBoolean(clientDetails.getAutoapprove())) - .build()) - .build(); + return builder + .tokenSettings(TokenSettings.builder() + .accessTokenFormat(OAuth2TokenFormat.REFERENCE) + .accessTokenTimeToLive(Duration.ofSeconds( + Optional.ofNullable(clientDetails.getAccessTokenValidity()).orElse(accessTokenValiditySeconds))) + .refreshTokenTimeToLive(Duration.ofSeconds(Optional.ofNullable(clientDetails.getRefreshTokenValidity()) + .orElse(refreshTokenValiditySeconds))) + .build()) + .clientSettings(ClientSettings.builder() + .requireAuthorizationConsent(!BooleanUtil.toBoolean(clientDetails.getAutoapprove())) + .build()) + .build(); - } + } } diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/SysDictController.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/SysDictController.java index 28c06926..1ee262fb 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/SysDictController.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/SysDictController.java @@ -34,7 +34,6 @@ import com.pig4cloud.pig.common.security.annotation.Inner; import com.pig4cloud.plugin.excel.annotation.ResponseExcel; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; -import javax.validation.Valid; import lombok.AllArgsConstructor; import org.springdoc.api.annotations.ParameterObject; import org.springframework.cache.annotation.CacheEvict; @@ -43,6 +42,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; import java.util.List; /** @@ -60,213 +60,196 @@ import java.util.List; @SecurityRequirement(name = HttpHeaders.AUTHORIZATION) public class SysDictController { - private final SysDictService sysDictService; + private final SysDictService sysDictService; - private final SysDictItemService sysDictItemService; + private final SysDictItemService sysDictItemService; - /** - * 通过ID查询字典信息 - * - * @param id ID - * @return 字典信息 - */ - @GetMapping("/details/{id}") - public R getById(@PathVariable Long id) { - return R.ok(sysDictService.getById(id)); - } + /** + * 通过ID查询字典信息 + * @param id ID + * @return 字典信息 + */ + @GetMapping("/details/{id}") + public R getById(@PathVariable Long id) { + return R.ok(sysDictService.getById(id)); + } - /** - * 查询字典信息 - * - * @param query 查询信息 - * @return 字典信息 - */ - @GetMapping("/details") - public R getDetails(@ParameterObject SysDict query) { - return R.ok(sysDictService.getOne(Wrappers.query(query), false)); - } + /** + * 查询字典信息 + * @param query 查询信息 + * @return 字典信息 + */ + @GetMapping("/details") + public R getDetails(@ParameterObject SysDict query) { + return R.ok(sysDictService.getOne(Wrappers.query(query), false)); + } - /** - * 分页查询字典信息 - * - * @param page 分页对象 - * @return 分页对象 - */ - @GetMapping("/page") - public R getDictPage(@ParameterObject Page page, @ParameterObject SysDict sysDict) { - return R.ok(sysDictService.page(page, - Wrappers.lambdaQuery() - .eq(StrUtil.isNotBlank(sysDict.getSystemFlag()), SysDict::getSystemFlag, sysDict.getSystemFlag()) - .like(StrUtil.isNotBlank(sysDict.getDictType()), SysDict::getDictType, sysDict.getDictType()))); - } + /** + * 分页查询字典信息 + * @param page 分页对象 + * @return 分页对象 + */ + @GetMapping("/page") + public R getDictPage(@ParameterObject Page page, @ParameterObject SysDict sysDict) { + return R.ok(sysDictService.page(page, + Wrappers.lambdaQuery() + .eq(StrUtil.isNotBlank(sysDict.getSystemFlag()), SysDict::getSystemFlag, sysDict.getSystemFlag()) + .like(StrUtil.isNotBlank(sysDict.getDictType()), SysDict::getDictType, sysDict.getDictType()))); + } + /** + * 添加字典 + * @param sysDict 字典信息 + * @return success、false + */ + @SysLog("添加字典") + @PostMapping + @PreAuthorize("@pms.hasPermission('sys_dict_add')") + public R save(@Valid @RequestBody SysDict sysDict) { + sysDictService.save(sysDict); + return R.ok(sysDict); + } - /** - * 添加字典 - * - * @param sysDict 字典信息 - * @return success、false - */ - @SysLog("添加字典") - @PostMapping - @PreAuthorize("@pms.hasPermission('sys_dict_add')") - public R save(@Valid @RequestBody SysDict sysDict) { - sysDictService.save(sysDict); - return R.ok(sysDict); - } + /** + * 删除字典,并且清除字典缓存 + * @param ids ID + * @return R + */ + @SysLog("删除字典") + @DeleteMapping + @PreAuthorize("@pms.hasPermission('sys_dict_del')") + @CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true) + public R removeById(@RequestBody Long[] ids) { + return R.ok(sysDictService.removeDictByIds(ids)); + } - /** - * 删除字典,并且清除字典缓存 - * - * @param ids ID - * @return R - */ - @SysLog("删除字典") - @DeleteMapping - @PreAuthorize("@pms.hasPermission('sys_dict_del')") - @CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true) - public R removeById(@RequestBody Long[] ids) { - return R.ok(sysDictService.removeDictByIds(ids)); - } + /** + * 修改字典 + * @param sysDict 字典信息 + * @return success/false + */ + @PutMapping + @SysLog("修改字典") + @PreAuthorize("@pms.hasPermission('sys_dict_edit')") + public R updateById(@Valid @RequestBody SysDict sysDict) { + return sysDictService.updateDict(sysDict); + } - /** - * 修改字典 - * - * @param sysDict 字典信息 - * @return success/false - */ - @PutMapping - @SysLog("修改字典") - @PreAuthorize("@pms.hasPermission('sys_dict_edit')") - public R updateById(@Valid @RequestBody SysDict sysDict) { - return sysDictService.updateDict(sysDict); - } + /** + * 分页查询 + * @param name 名称或者字典项 + * @return + */ + @GetMapping("/list") + public R getDictList(String name) { + return R.ok(sysDictService.list(Wrappers.lambdaQuery() + .like(StrUtil.isNotBlank(name), SysDict::getDictType, name) + .or() + .like(StrUtil.isNotBlank(name), SysDict::getDescription, name))); + } - /** - * 分页查询 - * - * @param name 名称或者字典项 - * @return - */ - @GetMapping("/list") - public R getDictList(String name) { - return R.ok(sysDictService.list(Wrappers.lambdaQuery() - .like(StrUtil.isNotBlank(name), SysDict::getDictType, name) - .or() - .like(StrUtil.isNotBlank(name), SysDict::getDescription, name))); - } + /** + * 分页查询 + * @param page 分页对象 + * @param sysDictItem 字典项 + * @return + */ + @GetMapping("/item/page") + public R getSysDictItemPage(Page page, SysDictItem sysDictItem) { + return R.ok(sysDictItemService.page(page, Wrappers.query(sysDictItem))); + } - /** - * 分页查询 - * - * @param page 分页对象 - * @param sysDictItem 字典项 - * @return - */ - @GetMapping("/item/page") - public R getSysDictItemPage(Page page, SysDictItem sysDictItem) { - return R.ok(sysDictItemService.page(page, Wrappers.query(sysDictItem))); - } + /** + * 通过id查询字典项 + * @param id id + * @return R + */ + @GetMapping("/item/details/{id}") + public R getDictItemById(@PathVariable("id") Long id) { + return R.ok(sysDictItemService.getById(id)); + } - /** - * 通过id查询字典项 - * - * @param id id - * @return R - */ - @GetMapping("/item/details/{id}") - public R getDictItemById(@PathVariable("id") Long id) { - return R.ok(sysDictItemService.getById(id)); - } + /** + * 查询字典项详情 + * @param query 查询条件 + * @return R + */ + @GetMapping("/item/details") + public R getDictItemDetails(SysDictItem query) { + return R.ok(sysDictItemService.getOne(Wrappers.query(query), false)); + } - /** - * 查询字典项详情 - * - * @param query 查询条件 - * @return R - */ - @GetMapping("/item/details") - public R getDictItemDetails(SysDictItem query) { - return R.ok(sysDictItemService.getOne(Wrappers.query(query), false)); - } + /** + * 新增字典项 + * @param sysDictItem 字典项 + * @return R + */ + @SysLog("新增字典项") + @PostMapping("/item") + @CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true) + public R save(@RequestBody SysDictItem sysDictItem) { + return R.ok(sysDictItemService.save(sysDictItem)); + } - /** - * 新增字典项 - * - * @param sysDictItem 字典项 - * @return R - */ - @SysLog("新增字典项") - @PostMapping("/item") - @CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true) - public R save(@RequestBody SysDictItem sysDictItem) { - return R.ok(sysDictItemService.save(sysDictItem)); - } + /** + * 修改字典项 + * @param sysDictItem 字典项 + * @return R + */ + @SysLog("修改字典项") + @PutMapping("/item") + public R updateById(@RequestBody SysDictItem sysDictItem) { + return sysDictItemService.updateDictItem(sysDictItem); + } - /** - * 修改字典项 - * - * @param sysDictItem 字典项 - * @return R - */ - @SysLog("修改字典项") - @PutMapping("/item") - public R updateById(@RequestBody SysDictItem sysDictItem) { - return sysDictItemService.updateDictItem(sysDictItem); - } + /** + * 通过id删除字典项 + * @param id id + * @return R + */ + @SysLog("删除字典项") + @DeleteMapping("/item/{id}") + public R removeDictItemById(@PathVariable Long id) { + return sysDictItemService.removeDictItem(id); + } - /** - * 通过id删除字典项 - * - * @param id id - * @return R - */ - @SysLog("删除字典项") - @DeleteMapping("/item/{id}") - public R removeDictItemById(@PathVariable Long id) { - return sysDictItemService.removeDictItem(id); - } + /** + * 同步缓存字典 + * @return R + */ + @SysLog("同步字典") + @PutMapping("/sync") + public R sync() { + return sysDictService.syncDictCache(); + } - /** - * 同步缓存字典 - * - * @return R - */ - @SysLog("同步字典") - @PutMapping("/sync") - public R sync() { - return sysDictService.syncDictCache(); - } + @ResponseExcel + @GetMapping("/export") + public List export(SysDictItem sysDictItem) { + return sysDictItemService.list(Wrappers.query(sysDictItem)); + } - @ResponseExcel - @GetMapping("/export") - public List export(SysDictItem sysDictItem) { - return sysDictItemService.list(Wrappers.query(sysDictItem)); - } + /** + * 通过字典类型查找字典 + * @param type 类型 + * @return 同类型字典 + */ + @GetMapping("/type/{type}") + @Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type", unless = "#result.data.isEmpty()") + public R> getDictByType(@PathVariable String type) { + return R.ok(sysDictItemService.list(Wrappers.query().lambda().eq(SysDictItem::getDictType, type))); + } - /** - * 通过字典类型查找字典 - * - * @param type 类型 - * @return 同类型字典 - */ - @GetMapping("/type/{type}") - @Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type", unless = "#result.data.isEmpty()") - public R> getDictByType(@PathVariable String type) { - return R.ok(sysDictItemService.list(Wrappers.query().lambda().eq(SysDictItem::getDictType, type))); - } - - /** - * 通过字典类型查找字典 (针对feign调用) - * TODO: 兼容性方案,代码重复 - * @param type 类型 - * @return 同类型字典 - */ - @Inner - @GetMapping("/remote/type/{type}") - @Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type", unless = "#result.data.isEmpty()") - public R> getRemoteDictByType(@PathVariable String type) { - return R.ok(sysDictItemService.list(Wrappers.query().lambda().eq(SysDictItem::getDictType, type))); - } + /** + * 通过字典类型查找字典 (针对feign调用) TODO: 兼容性方案,代码重复 + * @param type 类型 + * @return 同类型字典 + */ + @Inner + @GetMapping("/remote/type/{type}") + @Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type", unless = "#result.data.isEmpty()") + public R> getRemoteDictByType(@PathVariable String type) { + return R.ok(sysDictItemService.list(Wrappers.query().lambda().eq(SysDictItem::getDictType, type))); + } }