!274 ♻️ Refactoring code. 是使用 @RequestMapping Headers属性替代 代码中指定header 参数

Merge pull request !274 from lbw/auto-441246-dev-bc4fb4a1
This commit is contained in:
lengleng 2022-11-12 04:32:52 +00:00 committed by Gitee
commit e74bc1a04b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
14 changed files with 33 additions and 50 deletions

View File

@ -27,7 +27,6 @@ import com.pig4cloud.pig.admin.api.vo.TokenVo;
import com.pig4cloud.pig.auth.support.handler.PigAuthenticationFailureEventHandler; import com.pig4cloud.pig.auth.support.handler.PigAuthenticationFailureEventHandler;
import com.pig4cloud.pig.common.core.constant.CacheConstants; import com.pig4cloud.pig.common.core.constant.CacheConstants;
import com.pig4cloud.pig.common.core.constant.CommonConstants; import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.core.util.R; import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.core.util.RetOps; import com.pig4cloud.pig.common.core.util.RetOps;
import com.pig4cloud.pig.common.core.util.SpringContextHolder; import com.pig4cloud.pig.common.core.util.SpringContextHolder;
@ -108,8 +107,7 @@ public class PigTokenEndpoint {
@RequestParam(OAuth2ParameterNames.CLIENT_ID) String clientId, @RequestParam(OAuth2ParameterNames.CLIENT_ID) String clientId,
@RequestParam(OAuth2ParameterNames.SCOPE) String scope, @RequestParam(OAuth2ParameterNames.SCOPE) String scope,
@RequestParam(OAuth2ParameterNames.STATE) String state) { @RequestParam(OAuth2ParameterNames.STATE) String state) {
SysOauthClientDetails clientDetails = RetOps SysOauthClientDetails clientDetails = RetOps.of(clientDetailsService.getClientDetailsById(clientId)).getData()
.of(clientDetailsService.getClientDetailsById(clientId, SecurityConstants.FROM_IN)).getData()
.orElseThrow(() -> new OAuthClientException("clientId 不合法")); .orElseThrow(() -> new OAuthClientException("clientId 不合法"));
Set<String> authorizedScopes = StringUtils.commaDelimitedListToSet(clientDetails.getScope()); Set<String> authorizedScopes = StringUtils.commaDelimitedListToSet(clientDetails.getScope());

View File

@ -47,6 +47,11 @@ public interface SecurityConstants {
*/ */
String FROM = "from"; String FROM = "from";
/**
* 请求header
*/
String HEADER_FROM_IN = FROM + ":" + FROM_IN;
/** /**
* 默认登录URL * 默认登录URL
*/ */

View File

@ -18,7 +18,6 @@ package com.pig4cloud.pig.common.log.event;
import com.pig4cloud.pig.admin.api.entity.SysLog; import com.pig4cloud.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.admin.api.feign.RemoteLogService; import com.pig4cloud.pig.admin.api.feign.RemoteLogService;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
@ -39,7 +38,7 @@ public class SysLogListener {
@EventListener(SysLogEvent.class) @EventListener(SysLogEvent.class)
public void saveSysLog(SysLogEvent event) { public void saveSysLog(SysLogEvent event) {
SysLog sysLog = (SysLog) event.getSource(); SysLog sysLog = (SysLog) event.getSource();
remoteLogService.saveLog(sysLog, SecurityConstants.FROM_IN); remoteLogService.saveLog(sysLog);
} }
} }

View File

@ -54,7 +54,7 @@ public class PigAppUserDetailsServiceImpl implements PigUserDetailsService {
return (PigUser) cache.get(phone).get(); return (PigUser) cache.get(phone).get();
} }
R<UserInfo> result = remoteUserService.infoByMobile(phone, SecurityConstants.FROM_IN); R<UserInfo> result = remoteUserService.infoByMobile(phone);
UserDetails userDetails = getUserDetails(result); UserDetails userDetails = getUserDetails(result);
if (cache != null) { if (cache != null) {

View File

@ -86,8 +86,7 @@ public class PigRemoteRegisteredClientRepository implements RegisteredClientRepo
@Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null") @Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
public RegisteredClient findByClientId(String clientId) { public RegisteredClient findByClientId(String clientId) {
SysOauthClientDetails clientDetails = RetOps SysOauthClientDetails clientDetails = RetOps.of(clientDetailsService.getClientDetailsById(clientId)).getData()
.of(clientDetailsService.getClientDetailsById(clientId, SecurityConstants.FROM_IN)).getData()
.orElseThrow(() -> new OAuthClientException("客户端查询异常,请检查数据库链接")); .orElseThrow(() -> new OAuthClientException("客户端查询异常,请检查数据库链接"));
RegisteredClient.Builder builder = RegisteredClient.withId(clientDetails.getClientId()) RegisteredClient.Builder builder = RegisteredClient.withId(clientDetails.getClientId())

View File

@ -19,7 +19,6 @@ package com.pig4cloud.pig.common.security.service;
import com.pig4cloud.pig.admin.api.dto.UserInfo; import com.pig4cloud.pig.admin.api.dto.UserInfo;
import com.pig4cloud.pig.admin.api.feign.RemoteUserService; import com.pig4cloud.pig.admin.api.feign.RemoteUserService;
import com.pig4cloud.pig.common.core.constant.CacheConstants; import com.pig4cloud.pig.common.core.constant.CacheConstants;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.core.util.R; import com.pig4cloud.pig.common.core.util.R;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -56,7 +55,7 @@ public class PigUserDetailsServiceImpl implements PigUserDetailsService {
return (PigUser) cache.get(username).get(); return (PigUser) cache.get(username).get();
} }
R<UserInfo> result = remoteUserService.info(username, SecurityConstants.FROM_IN); R<UserInfo> result = remoteUserService.info(username);
UserDetails userDetails = getUserDetails(result); UserDetails userDetails = getUserDetails(result);
if (cache != null) { if (cache != null) {
cache.put(username, userDetails); cache.put(username, userDetails);

View File

@ -26,7 +26,6 @@ import com.pig4cloud.pig.common.core.util.R;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import java.util.List; import java.util.List;
@ -40,19 +39,16 @@ public interface RemoteClientDetailsService {
/** /**
* 通过clientId 查询客户端信息 * 通过clientId 查询客户端信息
* @param clientId 用户名 * @param clientId 用户名
* @param from 调用标志
* @return R * @return R
*/ */
@GetMapping("/client/getClientDetailsById/{clientId}") @GetMapping(value = "/client/getClientDetailsById/{clientId}", headers = SecurityConstants.HEADER_FROM_IN)
R<SysOauthClientDetails> getClientDetailsById(@PathVariable("clientId") String clientId, R<SysOauthClientDetails> getClientDetailsById(@PathVariable("clientId") String clientId);
@RequestHeader(SecurityConstants.FROM) String from);
/** /**
* 查询全部客户端 * 查询全部客户端
* @param from 调用标识
* @return R * @return R
*/ */
@GetMapping("/client/list") @GetMapping(value = "/client/list", headers = SecurityConstants.HEADER_FROM_IN)
R<List<SysOauthClientDetails>> listClientDetails(@RequestHeader(SecurityConstants.FROM) String from); R<List<SysOauthClientDetails>> listClientDetails();
} }

View File

@ -22,7 +22,6 @@ import com.pig4cloud.pig.common.core.util.R;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import java.util.List; import java.util.List;
@ -36,8 +35,7 @@ public interface RemoteDeptService {
* 查收子级id列表 * 查收子级id列表
* @return 返回子级id列表 * @return 返回子级id列表
*/ */
@GetMapping("/dept/child-id/{deptId}") @GetMapping(value = "/dept/child-id/{deptId}", headers = SecurityConstants.HEADER_FROM_IN)
R<List<Long>> listChildDeptId(@PathVariable("deptId") Long deptId, R<List<Long>> listChildDeptId(@PathVariable("deptId") Long deptId);
@RequestHeader(SecurityConstants.FROM) String from);
} }

View File

@ -23,7 +23,6 @@ import com.pig4cloud.pig.common.core.util.R;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
/** /**
* @author lengleng * @author lengleng
@ -35,10 +34,9 @@ public interface RemoteLogService {
/** /**
* 保存日志 * 保存日志
* @param sysLog 日志实体 * @param sysLog 日志实体
* @param from 内部调用标志
* @return succesfalse * @return succesfalse
*/ */
@PostMapping("/log") @PostMapping(value = "/log", headers = SecurityConstants.HEADER_FROM_IN)
R<Boolean> saveLog(@RequestBody SysLog sysLog, @RequestHeader(SecurityConstants.FROM) String from); R<Boolean> saveLog(@RequestBody SysLog sysLog);
} }

View File

@ -6,7 +6,6 @@ import com.pig4cloud.pig.common.core.util.R;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
/** /**
* @author lengleng * @author lengleng
@ -20,10 +19,9 @@ public interface RemoteParamService {
/** /**
* 通过key 查询参数配置 * 通过key 查询参数配置
* @param key key * @param key key
* @param from 声明成内部调用避免MQ 等无法调用
* @return * @return
*/ */
@GetMapping("/param/publicValue/{key}") @GetMapping(value = "/param/publicValue/{key}", headers = SecurityConstants.HEADER_FROM_IN)
R<String> getByKey(@PathVariable("key") String key, @RequestHeader(SecurityConstants.FROM) String from); R<String> getByKey(@PathVariable("key") String key);
} }

View File

@ -34,19 +34,17 @@ public interface RemoteTokenService {
/** /**
* 分页查询token 信息 * 分页查询token 信息
* @param params 分页参数 * @param params 分页参数
* @param from 内部调用标志
* @return page * @return page
*/ */
@PostMapping("/token/page") @PostMapping(value = "/token/page", headers = SecurityConstants.HEADER_FROM_IN)
R getTokenPage(@RequestBody Map<String, Object> params, @RequestHeader(SecurityConstants.FROM) String from); R getTokenPage(@RequestBody Map<String, Object> params);
/** /**
* 删除token * 删除token
* @param token token * @param token token
* @param from 调用标志
* @return * @return
*/ */
@DeleteMapping("/token/{token}") @DeleteMapping(value = "/token/{token}", headers = SecurityConstants.HEADER_FROM_IN)
R<Boolean> removeToken(@PathVariable("token") String token, @RequestHeader(SecurityConstants.FROM) String from); R<Boolean> removeToken(@PathVariable("token") String token);
} }

View File

@ -23,7 +23,6 @@ import com.pig4cloud.pig.common.core.util.R;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import java.util.List; import java.util.List;
@ -39,11 +38,10 @@ public interface RemoteUserService {
/** /**
* 通过用户名查询用户角色信息 * 通过用户名查询用户角色信息
* @param username 用户名 * @param username 用户名
* @param from 调用标志
* @return R * @return R
*/ */
@GetMapping("/user/info/{username}") @GetMapping(value = "/user/info/{username}", headers = SecurityConstants.HEADER_FROM_IN)
R<UserInfo> info(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM) String from); R<UserInfo> info(@PathVariable("username") String username);
/** /**
* 通过手机号码查询用户角色信息 * 通过手机号码查询用户角色信息
@ -51,8 +49,8 @@ public interface RemoteUserService {
* @param from 调用标志 * @param from 调用标志
* @return R * @return R
*/ */
@GetMapping("/app/info/{phone}") @GetMapping(value = "/app/info/{phone}", headers = SecurityConstants.HEADER_FROM_IN)
R<UserInfo> infoByMobile(@PathVariable("phone") String phone, @RequestHeader(SecurityConstants.FROM) String from); R<UserInfo> infoByMobile(@PathVariable("phone") String phone);
/** /**
* 根据部门id查询对应的用户 id 集合 * 根据部门id查询对应的用户 id 集合
@ -60,8 +58,7 @@ public interface RemoteUserService {
* @param from 调用标志 * @param from 调用标志
* @return 用户 id 集合 * @return 用户 id 集合
*/ */
@GetMapping("/user/ids") @GetMapping(value = "/user/ids", headers = SecurityConstants.HEADER_FROM_IN)
R<List<Long>> listUserIdByDeptIds(@RequestParam("deptIds") Set<Long> deptIds, R<List<Long>> listUserIdByDeptIds(@RequestParam("deptIds") Set<Long> deptIds);
@RequestHeader(SecurityConstants.FROM) String from);
} }

View File

@ -3,7 +3,6 @@ package com.pig4cloud.pig.admin.api.util;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.admin.api.feign.RemoteParamService; import com.pig4cloud.pig.admin.api.feign.RemoteParamService;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.core.util.SpringContextHolder; import com.pig4cloud.pig.common.core.util.SpringContextHolder;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
@ -44,7 +43,7 @@ public class ParamResolver {
RemoteParamService remoteParamService = SpringContextHolder.getBean(RemoteParamService.class); RemoteParamService remoteParamService = SpringContextHolder.getBean(RemoteParamService.class);
String result = remoteParamService.getByKey(key, SecurityConstants.FROM_IN).getData(); String result = remoteParamService.getByKey(key).getData();
if (StrUtil.isNotBlank(result)) { if (StrUtil.isNotBlank(result)) {
return Convert.convert(clazz, result); return Convert.convert(clazz, result);

View File

@ -17,7 +17,6 @@
package com.pig4cloud.pig.admin.controller; package com.pig4cloud.pig.admin.controller;
import com.pig4cloud.pig.admin.api.feign.RemoteTokenService; import com.pig4cloud.pig.admin.api.feign.RemoteTokenService;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.core.util.R; import com.pig4cloud.pig.common.core.util.R;
import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@ -48,7 +47,7 @@ public class TokenController {
*/ */
@GetMapping("/page") @GetMapping("/page")
public R token(@RequestParam Map<String, Object> params) { public R token(@RequestParam Map<String, Object> params) {
return remoteTokenService.getTokenPage(params, SecurityConstants.FROM_IN); return remoteTokenService.getTokenPage(params);
} }
/** /**
@ -59,7 +58,7 @@ public class TokenController {
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
@PreAuthorize("@pms.hasPermission('sys_token_del')") @PreAuthorize("@pms.hasPermission('sys_token_del')")
public R<Boolean> delete(@PathVariable String id) { public R<Boolean> delete(@PathVariable String id) {
return remoteTokenService.removeToken(id, SecurityConstants.FROM_IN); return remoteTokenService.removeToken(id);
} }
} }