mirror of
https://gitee.com/log4j/pig.git
synced 2024-12-22 20:54:25 +08:00
✨ add NoToken Feign 注解自动维护header
This commit is contained in:
parent
04f01302f0
commit
993879a6c3
@ -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;
|
||||||
@ -109,7 +108,7 @@ public class PigTokenEndpoint {
|
|||||||
@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, SecurityConstants.FROM_IN))
|
.of(clientDetailsService.getClientDetailsById(clientId))
|
||||||
.getData()
|
.getData()
|
||||||
.orElseThrow(() -> new OAuthClientException("clientId 不合法"));
|
.orElseThrow(() -> new OAuthClientException("clientId 不合法"));
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
|
|||||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
|
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
|
||||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
|
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.pig4cloud.pig.common.feign.core.PigFeignInnerRequestInterceptor;
|
||||||
import com.pig4cloud.pig.common.feign.core.PigFeignRequestCloseInterceptor;
|
import com.pig4cloud.pig.common.feign.core.PigFeignRequestCloseInterceptor;
|
||||||
import com.pig4cloud.pig.common.feign.sentinel.ext.PigSentinelFeign;
|
import com.pig4cloud.pig.common.feign.sentinel.ext.PigSentinelFeign;
|
||||||
import com.pig4cloud.pig.common.feign.sentinel.handle.PigUrlBlockHandler;
|
import com.pig4cloud.pig.common.feign.sentinel.handle.PigUrlBlockHandler;
|
||||||
@ -45,33 +46,44 @@ import org.springframework.context.annotation.Scope;
|
|||||||
@AutoConfigureBefore(SentinelFeignAutoConfiguration.class)
|
@AutoConfigureBefore(SentinelFeignAutoConfiguration.class)
|
||||||
public class PigFeignAutoConfiguration {
|
public class PigFeignAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Scope("prototype")
|
@Scope("prototype")
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@ConditionalOnProperty(name = "feign.sentinel.enabled")
|
@ConditionalOnProperty(name = "feign.sentinel.enabled")
|
||||||
public Feign.Builder feignSentinelBuilder() {
|
public Feign.Builder feignSentinelBuilder() {
|
||||||
return PigSentinelFeign.builder();
|
return PigSentinelFeign.builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public BlockExceptionHandler blockExceptionHandler(ObjectMapper objectMapper) {
|
public BlockExceptionHandler blockExceptionHandler(ObjectMapper objectMapper) {
|
||||||
return new PigUrlBlockHandler(objectMapper);
|
return new PigUrlBlockHandler(objectMapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public RequestOriginParser requestOriginParser() {
|
public RequestOriginParser requestOriginParser() {
|
||||||
return new PigHeaderRequestOriginParser();
|
return new PigHeaderRequestOriginParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add http connection close header
|
* add http connection close header
|
||||||
* @return
|
*
|
||||||
*/
|
* @return
|
||||||
@Bean
|
*/
|
||||||
public PigFeignRequestCloseInterceptor pigFeignRequestCloseInterceptor() {
|
@Bean
|
||||||
return new PigFeignRequestCloseInterceptor();
|
public PigFeignRequestCloseInterceptor pigFeignRequestCloseInterceptor() {
|
||||||
}
|
return new PigFeignRequestCloseInterceptor();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* add inner request header
|
||||||
|
*
|
||||||
|
* @return PigFeignInnerRequestInterceptor
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public PigFeignInnerRequestInterceptor pigFeignInnerRequestInterceptor() {
|
||||||
|
return new PigFeignInnerRequestInterceptor();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.pig4cloud.pig.common.feign.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务无token调用声明注解
|
||||||
|
* <p>
|
||||||
|
* 只有发起方没有 token 时候才需要添加此注解, @NoToken + @Inner
|
||||||
|
* <p>
|
||||||
|
*/
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface NoToken {
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.pig4cloud.pig.common.feign.core;
|
||||||
|
|
||||||
|
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
|
||||||
|
import com.pig4cloud.pig.common.feign.annotation.NoToken;
|
||||||
|
import feign.RequestInterceptor;
|
||||||
|
import feign.RequestTemplate;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lengleng
|
||||||
|
* @date 2024/6/1
|
||||||
|
*/
|
||||||
|
public class PigFeignInnerRequestInterceptor implements RequestInterceptor, Ordered {
|
||||||
|
/**
|
||||||
|
* Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
|
||||||
|
*
|
||||||
|
* @param template
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void apply(RequestTemplate template) {
|
||||||
|
Method method = template.methodMetadata().method();
|
||||||
|
NoToken noToken = method.getAnnotation(NoToken.class);
|
||||||
|
if (noToken != null) {
|
||||||
|
template.header(SecurityConstants.FROM, SecurityConstants.FROM_IN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return Integer.MIN_VALUE;
|
||||||
|
}
|
||||||
|
}
|
@ -24,7 +24,6 @@ import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
|||||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||||
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 com.pig4cloud.pig.common.core.jackson.PigJavaTimeModule;
|
import com.pig4cloud.pig.common.core.jackson.PigJavaTimeModule;
|
||||||
import com.pig4cloud.pig.common.log.config.PigLogProperties;
|
import com.pig4cloud.pig.common.log.config.PigLogProperties;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@ -45,45 +44,45 @@ import java.util.Objects;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class SysLogListener implements InitializingBean {
|
public class SysLogListener implements InitializingBean {
|
||||||
|
|
||||||
// new 一个 避免日志脱敏策略影响全局ObjectMapper
|
// new 一个 避免日志脱敏策略影响全局ObjectMapper
|
||||||
private final static ObjectMapper objectMapper = new ObjectMapper();
|
private final static ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
private final RemoteLogService remoteLogService;
|
private final RemoteLogService remoteLogService;
|
||||||
|
|
||||||
private final PigLogProperties logProperties;
|
private final PigLogProperties logProperties;
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Async
|
@Async
|
||||||
@Order
|
@Order
|
||||||
@EventListener(SysLogEvent.class)
|
@EventListener(SysLogEvent.class)
|
||||||
public void saveSysLog(SysLogEvent event) {
|
public void saveSysLog(SysLogEvent event) {
|
||||||
SysLogEventSource source = (SysLogEventSource) event.getSource();
|
SysLogEventSource source = (SysLogEventSource) event.getSource();
|
||||||
SysLog sysLog = new SysLog();
|
SysLog sysLog = new SysLog();
|
||||||
BeanUtils.copyProperties(source, sysLog);
|
BeanUtils.copyProperties(source, sysLog);
|
||||||
|
|
||||||
// json 格式刷参数放在异步中处理,提升性能
|
// json 格式刷参数放在异步中处理,提升性能
|
||||||
if (Objects.nonNull(source.getBody())) {
|
if (Objects.nonNull(source.getBody())) {
|
||||||
String params = objectMapper.writeValueAsString(source.getBody());
|
String params = objectMapper.writeValueAsString(source.getBody());
|
||||||
sysLog.setParams(StrUtil.subPre(params, logProperties.getMaxLength()));
|
sysLog.setParams(StrUtil.subPre(params, logProperties.getMaxLength()));
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteLogService.saveLog(sysLog, SecurityConstants.FROM_IN);
|
remoteLogService.saveLog(sysLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
objectMapper.addMixIn(Object.class, PropertyFilterMixIn.class);
|
objectMapper.addMixIn(Object.class, PropertyFilterMixIn.class);
|
||||||
String[] ignorableFieldNames = logProperties.getExcludeFields().toArray(new String[0]);
|
String[] ignorableFieldNames = logProperties.getExcludeFields().toArray(new String[0]);
|
||||||
|
|
||||||
FilterProvider filters = new SimpleFilterProvider().addFilter("filter properties by name",
|
FilterProvider filters = new SimpleFilterProvider().addFilter("filter properties by name",
|
||||||
SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
|
SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
|
||||||
objectMapper.setFilterProvider(filters);
|
objectMapper.setFilterProvider(filters);
|
||||||
objectMapper.registerModule(new PigJavaTimeModule());
|
objectMapper.registerModule(new PigJavaTimeModule());
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonFilter("filter properties by name")
|
@JsonFilter("filter properties by name")
|
||||||
class PropertyFilterMixIn {
|
class PropertyFilterMixIn {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ public class PigAppUserDetailsServiceImpl implements PigUserDetailsService {
|
|||||||
|
|
||||||
UserDTO userDTO = new UserDTO();
|
UserDTO userDTO = new UserDTO();
|
||||||
userDTO.setPhone(phone);
|
userDTO.setPhone(phone);
|
||||||
R<UserInfo> result = remoteUserService.info(userDTO, SecurityConstants.FROM_IN);
|
R<UserInfo> result = remoteUserService.info(userDTO);
|
||||||
|
|
||||||
UserDetails userDetails = getUserDetails(result);
|
UserDetails userDetails = getUserDetails(result);
|
||||||
if (cache != null) {
|
if (cache != null) {
|
||||||
|
@ -33,98 +33,101 @@ import java.util.Optional;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PigRemoteRegisteredClientRepository implements RegisteredClientRepository {
|
public class PigRemoteRegisteredClientRepository implements RegisteredClientRepository {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 刷新令牌有效期默认 30 天
|
* 刷新令牌有效期默认 30 天
|
||||||
*/
|
*/
|
||||||
private final static int refreshTokenValiditySeconds = 60 * 60 * 24 * 30;
|
private final static int refreshTokenValiditySeconds = 60 * 60 * 24 * 30;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求令牌有效期默认 12 小时
|
* 请求令牌有效期默认 12 小时
|
||||||
*/
|
*/
|
||||||
private final static int accessTokenValiditySeconds = 60 * 60 * 12;
|
private final static int accessTokenValiditySeconds = 60 * 60 * 12;
|
||||||
|
|
||||||
private final RemoteClientDetailsService clientDetailsService;
|
private final RemoteClientDetailsService clientDetailsService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the registered client.
|
* Saves the registered client.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* IMPORTANT: Sensitive information should be encoded externally from the
|
* IMPORTANT: Sensitive information should be encoded externally from the
|
||||||
* implementation, e.g. {@link RegisteredClient#getClientSecret()}
|
* implementation, e.g. {@link RegisteredClient#getClientSecret()}
|
||||||
* @param registeredClient the {@link RegisteredClient}
|
*
|
||||||
*/
|
* @param registeredClient the {@link RegisteredClient}
|
||||||
@Override
|
*/
|
||||||
public void save(RegisteredClient registeredClient) {
|
@Override
|
||||||
}
|
public void save(RegisteredClient registeredClient) {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the registered client identified by the provided {@code id}, or
|
* Returns the registered client identified by the provided {@code id}, or
|
||||||
* {@code null} if not found.
|
* {@code null} if not found.
|
||||||
* @param id the registration identifier
|
*
|
||||||
* @return the {@link RegisteredClient} if found, otherwise {@code null}
|
* @param id the registration identifier
|
||||||
*/
|
* @return the {@link RegisteredClient} if found, otherwise {@code null}
|
||||||
@Override
|
*/
|
||||||
public RegisteredClient findById(String id) {
|
@Override
|
||||||
throw new UnsupportedOperationException();
|
public RegisteredClient findById(String id) {
|
||||||
}
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the registered client identified by the provided {@code clientId}, or
|
* Returns the registered client identified by the provided {@code clientId}, or
|
||||||
* {@code null} if not found.
|
* {@code null} if not found.
|
||||||
* @param clientId the client identifier
|
* @param clientId the client identifier
|
||||||
* @return the {@link RegisteredClient} if found, otherwise {@code null}
|
* @return the {@link RegisteredClient} if found, otherwise {@code null}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重写原生方法支持redis缓存
|
* 重写原生方法支持redis缓存
|
||||||
* @param clientId
|
*
|
||||||
* @return
|
* @param clientId
|
||||||
*/
|
* @return
|
||||||
@Override
|
*/
|
||||||
@SneakyThrows
|
@Override
|
||||||
@Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
|
@SneakyThrows
|
||||||
public RegisteredClient findByClientId(String clientId) {
|
@Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
|
||||||
|
public RegisteredClient findByClientId(String clientId) {
|
||||||
|
|
||||||
SysOauthClientDetails clientDetails = RetOps
|
SysOauthClientDetails clientDetails = RetOps
|
||||||
.of(clientDetailsService.getClientDetailsById(clientId, SecurityConstants.FROM_IN))
|
.of(clientDetailsService.getClientDetailsById(clientId))
|
||||||
.getData()
|
.getData()
|
||||||
.orElseThrow(() -> new OAuth2AuthorizationCodeRequestAuthenticationException(
|
.orElseThrow(() -> new OAuth2AuthorizationCodeRequestAuthenticationException(
|
||||||
new OAuth2Error("客户端查询异常,请检查数据库链接"), null));
|
new OAuth2Error("客户端查询异常,请检查数据库链接"), null));
|
||||||
|
|
||||||
RegisteredClient.Builder builder = RegisteredClient.withId(clientDetails.getClientId())
|
RegisteredClient.Builder builder = RegisteredClient.withId(clientDetails.getClientId())
|
||||||
.clientId(clientDetails.getClientId())
|
.clientId(clientDetails.getClientId())
|
||||||
.clientSecret(SecurityConstants.NOOP + clientDetails.getClientSecret())
|
.clientSecret(SecurityConstants.NOOP + clientDetails.getClientSecret())
|
||||||
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC);
|
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC);
|
||||||
|
|
||||||
for (String authorizedGrantType : clientDetails.getAuthorizedGrantTypes()) {
|
for (String authorizedGrantType : clientDetails.getAuthorizedGrantTypes()) {
|
||||||
builder.authorizationGrantType(new AuthorizationGrantType(authorizedGrantType));
|
builder.authorizationGrantType(new AuthorizationGrantType(authorizedGrantType));
|
||||||
|
|
||||||
}
|
}
|
||||||
// 回调地址
|
// 回调地址
|
||||||
Optional.ofNullable(clientDetails.getWebServerRedirectUri())
|
Optional.ofNullable(clientDetails.getWebServerRedirectUri())
|
||||||
.ifPresent(redirectUri -> Arrays.stream(redirectUri.split(StrUtil.COMMA))
|
.ifPresent(redirectUri -> Arrays.stream(redirectUri.split(StrUtil.COMMA))
|
||||||
.filter(StrUtil::isNotBlank)
|
.filter(StrUtil::isNotBlank)
|
||||||
.forEach(builder::redirectUri));
|
.forEach(builder::redirectUri));
|
||||||
|
|
||||||
// scope
|
// scope
|
||||||
Optional.ofNullable(clientDetails.getScope())
|
Optional.ofNullable(clientDetails.getScope())
|
||||||
.ifPresent(scope -> Arrays.stream(scope.split(StrUtil.COMMA))
|
.ifPresent(scope -> Arrays.stream(scope.split(StrUtil.COMMA))
|
||||||
.filter(StrUtil::isNotBlank)
|
.filter(StrUtil::isNotBlank)
|
||||||
.forEach(builder::scope));
|
.forEach(builder::scope));
|
||||||
|
|
||||||
return builder
|
return builder
|
||||||
.tokenSettings(TokenSettings.builder()
|
.tokenSettings(TokenSettings.builder()
|
||||||
.accessTokenFormat(OAuth2TokenFormat.REFERENCE)
|
.accessTokenFormat(OAuth2TokenFormat.REFERENCE)
|
||||||
.accessTokenTimeToLive(Duration.ofSeconds(
|
.accessTokenTimeToLive(Duration.ofSeconds(
|
||||||
Optional.ofNullable(clientDetails.getAccessTokenValidity()).orElse(accessTokenValiditySeconds)))
|
Optional.ofNullable(clientDetails.getAccessTokenValidity()).orElse(accessTokenValiditySeconds)))
|
||||||
.refreshTokenTimeToLive(Duration.ofSeconds(Optional.ofNullable(clientDetails.getRefreshTokenValidity())
|
.refreshTokenTimeToLive(Duration.ofSeconds(Optional.ofNullable(clientDetails.getRefreshTokenValidity())
|
||||||
.orElse(refreshTokenValiditySeconds)))
|
.orElse(refreshTokenValiditySeconds)))
|
||||||
.build())
|
.build())
|
||||||
.clientSettings(ClientSettings.builder()
|
.clientSettings(ClientSettings.builder()
|
||||||
.requireAuthorizationConsent(!BooleanUtil.toBoolean(clientDetails.getAutoapprove()))
|
.requireAuthorizationConsent(!BooleanUtil.toBoolean(clientDetails.getAutoapprove()))
|
||||||
.build())
|
.build())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,6 @@ import com.pig4cloud.pig.admin.api.dto.UserDTO;
|
|||||||
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;
|
||||||
@ -40,36 +39,37 @@ import org.springframework.security.core.userdetails.UserDetails;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class PigUserDetailsServiceImpl implements PigUserDetailsService {
|
public class PigUserDetailsServiceImpl implements PigUserDetailsService {
|
||||||
|
|
||||||
private final RemoteUserService remoteUserService;
|
private final RemoteUserService remoteUserService;
|
||||||
|
|
||||||
private final CacheManager cacheManager;
|
private final CacheManager cacheManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户名密码登录
|
* 用户名密码登录
|
||||||
* @param username 用户名
|
*
|
||||||
* @return
|
* @param username 用户名
|
||||||
*/
|
* @return
|
||||||
@Override
|
*/
|
||||||
@SneakyThrows
|
@Override
|
||||||
public UserDetails loadUserByUsername(String username) {
|
@SneakyThrows
|
||||||
Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
|
public UserDetails loadUserByUsername(String username) {
|
||||||
if (cache != null && cache.get(username) != null) {
|
Cache cache = cacheManager.getCache(CacheConstants.USER_DETAILS);
|
||||||
return (PigUser) cache.get(username).get();
|
if (cache != null && cache.get(username) != null) {
|
||||||
}
|
return (PigUser) cache.get(username).get();
|
||||||
|
}
|
||||||
|
|
||||||
UserDTO userDTO = new UserDTO();
|
UserDTO userDTO = new UserDTO();
|
||||||
userDTO.setUsername(username);
|
userDTO.setUsername(username);
|
||||||
R<UserInfo> result = remoteUserService.info(userDTO, SecurityConstants.FROM_IN);
|
R<UserInfo> result = remoteUserService.info(userDTO);
|
||||||
UserDetails userDetails = getUserDetails(result);
|
UserDetails userDetails = getUserDetails(result);
|
||||||
if (cache != null) {
|
if (cache != null) {
|
||||||
cache.put(username, userDetails);
|
cache.put(username, userDetails);
|
||||||
}
|
}
|
||||||
return userDetails;
|
return userDetails;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getOrder() {
|
public int getOrder() {
|
||||||
return Integer.MIN_VALUE;
|
return Integer.MIN_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,9 +38,8 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<!--feign 注解依赖-->
|
<!--feign 注解依赖-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>com.pig4cloud</groupId>
|
||||||
<artifactId>spring-cloud-openfeign-core</artifactId>
|
<artifactId>pig-common-feign</artifactId>
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<!--mybatis 依赖-->
|
<!--mybatis 依赖-->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -20,13 +20,12 @@
|
|||||||
package com.pig4cloud.pig.admin.api.feign;
|
package com.pig4cloud.pig.admin.api.feign;
|
||||||
|
|
||||||
import com.pig4cloud.pig.admin.api.entity.SysOauthClientDetails;
|
import com.pig4cloud.pig.admin.api.entity.SysOauthClientDetails;
|
||||||
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
|
|
||||||
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
||||||
import com.pig4cloud.pig.common.core.util.R;
|
import com.pig4cloud.pig.common.core.util.R;
|
||||||
|
import com.pig4cloud.pig.common.feign.annotation.NoToken;
|
||||||
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
|
||||||
@ -35,15 +34,15 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
|||||||
@FeignClient(contextId = "remoteClientDetailsService", value = ServiceNameConstants.UPMS_SERVICE)
|
@FeignClient(contextId = "remoteClientDetailsService", value = ServiceNameConstants.UPMS_SERVICE)
|
||||||
public interface RemoteClientDetailsService {
|
public interface RemoteClientDetailsService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过clientId 查询客户端信息
|
* 通过clientId 查询客户端信息 (未登录,需要无token 内部调用)
|
||||||
* @param clientId 用户名
|
*
|
||||||
* @param from 调用标志
|
* @param clientId 用户名
|
||||||
* @return R
|
* @return R
|
||||||
*/
|
*/
|
||||||
@GetMapping("/client/getClientDetailsById/{clientId}")
|
@NoToken
|
||||||
R<SysOauthClientDetails> getClientDetailsById(@PathVariable("clientId") String clientId,
|
@GetMapping("/client/getClientDetailsById/{clientId}")
|
||||||
@RequestHeader(SecurityConstants.FROM) String from);
|
R<SysOauthClientDetails> getClientDetailsById(@PathVariable("clientId") String clientId);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,13 +20,12 @@
|
|||||||
package com.pig4cloud.pig.admin.api.feign;
|
package com.pig4cloud.pig.admin.api.feign;
|
||||||
|
|
||||||
import com.pig4cloud.pig.admin.api.entity.SysLog;
|
import com.pig4cloud.pig.admin.api.entity.SysLog;
|
||||||
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
|
|
||||||
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
||||||
import com.pig4cloud.pig.common.core.util.R;
|
import com.pig4cloud.pig.common.core.util.R;
|
||||||
|
import com.pig4cloud.pig.common.feign.annotation.NoToken;
|
||||||
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
|
||||||
@ -36,12 +35,12 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
|||||||
public interface RemoteLogService {
|
public interface RemoteLogService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存日志
|
* 保存日志 (异步多线程调用,无token)
|
||||||
* @param sysLog 日志实体
|
* @param sysLog 日志实体
|
||||||
* @param from 是否内部调用
|
|
||||||
* @return succes、false
|
* @return succes、false
|
||||||
*/
|
*/
|
||||||
|
@NoToken
|
||||||
@PostMapping("/log/save")
|
@PostMapping("/log/save")
|
||||||
R<Boolean> saveLog(@RequestBody SysLog sysLog, @RequestHeader(SecurityConstants.FROM) String from);
|
R<Boolean> saveLog(@RequestBody SysLog sysLog);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
package com.pig4cloud.pig.admin.api.feign;
|
package com.pig4cloud.pig.admin.api.feign;
|
||||||
|
|
||||||
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
|
|
||||||
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
||||||
import com.pig4cloud.pig.common.core.util.R;
|
import com.pig4cloud.pig.common.core.util.R;
|
||||||
|
import com.pig4cloud.pig.common.feign.annotation.NoToken;
|
||||||
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
|
||||||
@ -17,13 +16,14 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
|||||||
@FeignClient(contextId = "remoteParamService", value = ServiceNameConstants.UPMS_SERVICE)
|
@FeignClient(contextId = "remoteParamService", value = ServiceNameConstants.UPMS_SERVICE)
|
||||||
public interface RemoteParamService {
|
public interface RemoteParamService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过key 查询参数配置
|
* 通过key 查询参数配置
|
||||||
* @param key key
|
*
|
||||||
* @param from 声明成内部调用,避免MQ 等无法调用
|
* @param key key
|
||||||
* @return
|
* @NoToken 声明成内部调用,避免MQ 等无法调用
|
||||||
*/
|
*/
|
||||||
@GetMapping("/param/publicValue/{key}")
|
@NoToken
|
||||||
R<String> getByKey(@PathVariable("key") String key, @RequestHeader(SecurityConstants.FROM) String from);
|
@GetMapping("/param/publicValue/{key}")
|
||||||
|
R<String> getByKey(@PathVariable("key") String key);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,9 +18,9 @@
|
|||||||
package com.pig4cloud.pig.admin.api.feign;
|
package com.pig4cloud.pig.admin.api.feign;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
|
|
||||||
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
||||||
import com.pig4cloud.pig.common.core.util.R;
|
import com.pig4cloud.pig.common.core.util.R;
|
||||||
|
import com.pig4cloud.pig.common.feign.annotation.NoToken;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@ -33,34 +33,34 @@ import java.util.Map;
|
|||||||
@FeignClient(contextId = "remoteTokenService", value = ServiceNameConstants.AUTH_SERVICE)
|
@FeignClient(contextId = "remoteTokenService", value = ServiceNameConstants.AUTH_SERVICE)
|
||||||
public interface RemoteTokenService {
|
public interface RemoteTokenService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页查询token 信息
|
* 分页查询token 信息
|
||||||
* @param from 内部调用标志
|
*
|
||||||
* @param params 分页参数
|
* @param params 分页参数
|
||||||
* @param from 内部调用标志
|
* @return page
|
||||||
* @return page
|
*/
|
||||||
*/
|
@NoToken
|
||||||
@PostMapping("/token/page")
|
@PostMapping("/token/page")
|
||||||
R<Page> getTokenPage(@RequestBody Map<String, Object> params, @RequestHeader(SecurityConstants.FROM) String from);
|
R<Page> getTokenPage(@RequestBody Map<String, Object> params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除token
|
* 删除token
|
||||||
* @param from 内部调用标志
|
*
|
||||||
* @param token token
|
* @param token token
|
||||||
* @param from 内部调用标志
|
* @return
|
||||||
* @return
|
*/
|
||||||
*/
|
@NoToken
|
||||||
@DeleteMapping("/token/remove/{token}")
|
@DeleteMapping("/token/remove/{token}")
|
||||||
R<Boolean> removeTokenById(@PathVariable("token") String token, @RequestHeader(SecurityConstants.FROM) String from);
|
R<Boolean> removeTokenById(@PathVariable("token") String token);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验令牌获取用户信息
|
* 校验令牌获取用户信息
|
||||||
* @param token
|
*
|
||||||
* @param from
|
* @param token
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@GetMapping("/token/query-token")
|
@NoToken
|
||||||
R<Map<String, Object>> queryToken(@RequestParam("token") String token,
|
@GetMapping("/token/query-token")
|
||||||
@RequestHeader(SecurityConstants.FROM) String from);
|
R<Map<String, Object>> queryToken(@RequestParam("token") String token);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,15 +21,12 @@ package com.pig4cloud.pig.admin.api.feign;
|
|||||||
|
|
||||||
import com.pig4cloud.pig.admin.api.dto.UserDTO;
|
import com.pig4cloud.pig.admin.api.dto.UserDTO;
|
||||||
import com.pig4cloud.pig.admin.api.dto.UserInfo;
|
import com.pig4cloud.pig.admin.api.dto.UserInfo;
|
||||||
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
|
|
||||||
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
import com.pig4cloud.pig.common.core.constant.ServiceNameConstants;
|
||||||
import com.pig4cloud.pig.common.core.util.R;
|
import com.pig4cloud.pig.common.core.util.R;
|
||||||
|
import com.pig4cloud.pig.common.feign.annotation.NoToken;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.cloud.openfeign.SpringQueryMap;
|
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||||
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.PutMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lengleng
|
* @author lengleng
|
||||||
@ -38,22 +35,15 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
|||||||
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.UPMS_SERVICE)
|
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.UPMS_SERVICE)
|
||||||
public interface RemoteUserService {
|
public interface RemoteUserService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过用户名查询用户、角色信息
|
* (未登录状态调用,需要加 @NoToken)
|
||||||
* @param user 用户查询对象
|
* 通过用户名查询用户、角色信息
|
||||||
* @param from 调用标志
|
*
|
||||||
* @return R
|
* @param user 用户查询对象
|
||||||
*/
|
* @return R
|
||||||
@GetMapping("/user/info/query")
|
*/
|
||||||
R<UserInfo> info(@SpringQueryMap UserDTO user, @RequestHeader(SecurityConstants.FROM) String from);
|
@NoToken
|
||||||
|
@GetMapping("/user/info/query")
|
||||||
/**
|
R<UserInfo> info(@SpringQueryMap UserDTO user);
|
||||||
* 锁定用户
|
|
||||||
* @param username 用户名
|
|
||||||
* @param from 调用标识
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@PutMapping("/user/lock/{username}")
|
|
||||||
R<Boolean> lockUser(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM) String from);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -18,7 +18,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 com.pig4cloud.pig.common.log.annotation.SysLog;
|
import com.pig4cloud.pig.common.log.annotation.SysLog;
|
||||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
@ -53,7 +52,7 @@ public class SysTokenController {
|
|||||||
*/
|
*/
|
||||||
@RequestMapping("/page")
|
@RequestMapping("/page")
|
||||||
public R getTokenPage(@RequestBody Map<String, Object> params) {
|
public R getTokenPage(@RequestBody Map<String, Object> params) {
|
||||||
return remoteTokenService.getTokenPage(params, SecurityConstants.FROM_IN);
|
return remoteTokenService.getTokenPage(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,7 +65,7 @@ public class SysTokenController {
|
|||||||
@PreAuthorize("@pms.hasPermission('sys_token_del')")
|
@PreAuthorize("@pms.hasPermission('sys_token_del')")
|
||||||
public R removeById(@RequestBody String[] tokens) {
|
public R removeById(@RequestBody String[] tokens) {
|
||||||
for (String token : tokens) {
|
for (String token : tokens) {
|
||||||
remoteTokenService.removeTokenById(token, SecurityConstants.FROM_IN);
|
remoteTokenService.removeTokenById(token);
|
||||||
}
|
}
|
||||||
return R.ok();
|
return R.ok();
|
||||||
}
|
}
|
||||||
|
@ -202,7 +202,6 @@ public class SysUserController {
|
|||||||
* @param username 用户名
|
* @param username 用户名
|
||||||
* @return R
|
* @return R
|
||||||
*/
|
*/
|
||||||
@Inner
|
|
||||||
@PutMapping("/lock/{username}")
|
@PutMapping("/lock/{username}")
|
||||||
public R lockUser(@PathVariable String username) {
|
public R lockUser(@PathVariable String username) {
|
||||||
return userService.lockUser(username);
|
return userService.lockUser(username);
|
||||||
|
@ -22,7 +22,6 @@ package com.pig4cloud.pig.admin.service.impl;
|
|||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
@ -436,10 +435,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public R checkPassword(String password) {
|
public R checkPassword(String password) {
|
||||||
String username = SecurityUtils.getUser().getUsername();
|
SysUser sysUser = baseMapper.selectById(SecurityUtils.getUser().getId());
|
||||||
SysUser condition = new SysUser();
|
|
||||||
condition.setUsername(username);
|
|
||||||
SysUser sysUser = this.getOne(new QueryWrapper<>(condition));
|
|
||||||
|
|
||||||
if (!ENCODER.matches(password, sysUser.getPassword())) {
|
if (!ENCODER.matches(password, sysUser.getPassword())) {
|
||||||
log.info("原密码错误");
|
log.info("原密码错误");
|
||||||
|
Loading…
Reference in New Issue
Block a user