Pre Merge pull request !10 from 坎坎坷坷/master
This commit is contained in:
commit
8629bca3d5
|
|
@ -118,22 +118,17 @@ public class AuthServerConfig extends AuthorizationServerConfigurerAdapter
|
||||||
@Bean
|
@Bean
|
||||||
public TokenEnhancer tokenEnhancer()
|
public TokenEnhancer tokenEnhancer()
|
||||||
{
|
{
|
||||||
return new TokenEnhancer()
|
return (accessToken,authentication)->{
|
||||||
{
|
if (accessToken instanceof DefaultOAuth2AccessToken)
|
||||||
@Override
|
|
||||||
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication)
|
|
||||||
{
|
{
|
||||||
if (accessToken instanceof DefaultOAuth2AccessToken)
|
DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
|
||||||
{
|
LoginUser user = (LoginUser) authentication.getUserAuthentication().getPrincipal();
|
||||||
DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
|
Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();
|
||||||
LoginUser user = (LoginUser) authentication.getUserAuthentication().getPrincipal();
|
additionalInformation.put(SecurityConstants.DETAILS_USERNAME, authentication.getName());
|
||||||
Map<String, Object> additionalInformation = new LinkedHashMap<String, Object>();
|
additionalInformation.put(SecurityConstants.DETAILS_USER_ID, user.getUserId());
|
||||||
additionalInformation.put(SecurityConstants.DETAILS_USERNAME, authentication.getName());
|
token.setAdditionalInformation(additionalInformation);
|
||||||
additionalInformation.put(SecurityConstants.DETAILS_USER_ID, user.getUserId());
|
}
|
||||||
token.setAdditionalInformation(additionalInformation);
|
return accessToken;
|
||||||
}
|
|
||||||
return accessToken;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,71 +0,0 @@
|
||||||
package com.ruoyi.common.redis.configure;
|
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
|
||||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
||||||
import com.fasterxml.jackson.databind.JavaType;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
|
||||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
||||||
import org.springframework.data.redis.serializer.SerializationException;
|
|
||||||
import com.alibaba.fastjson.parser.ParserConfig;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Redis使用FastJson序列化
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
|
|
||||||
{
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
private ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
|
|
||||||
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
|
||||||
|
|
||||||
private Class<T> clazz;
|
|
||||||
|
|
||||||
static
|
|
||||||
{
|
|
||||||
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FastJson2JsonRedisSerializer(Class<T> clazz)
|
|
||||||
{
|
|
||||||
super();
|
|
||||||
this.clazz = clazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] serialize(T t) throws SerializationException
|
|
||||||
{
|
|
||||||
if (t == null)
|
|
||||||
{
|
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T deserialize(byte[] bytes) throws SerializationException
|
|
||||||
{
|
|
||||||
if (bytes == null || bytes.length <= 0)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String str = new String(bytes, DEFAULT_CHARSET);
|
|
||||||
|
|
||||||
return JSON.parseObject(str, clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setObjectMapper(ObjectMapper objectMapper)
|
|
||||||
{
|
|
||||||
Assert.notNull(objectMapper, "'objectMapper' must not be null");
|
|
||||||
this.objectMapper = objectMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected JavaType getJavaType(Class<?> clazz)
|
|
||||||
{
|
|
||||||
return TypeFactory.defaultInstance().constructType(clazz);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,11 +1,16 @@
|
||||||
package com.ruoyi.common.redis.configure;
|
package com.ruoyi.common.redis.configure;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
||||||
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
import org.springframework.cache.annotation.CachingConfigurerSupport;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
|
|
@ -21,23 +26,20 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
public class RedisConfig extends CachingConfigurerSupport
|
public class RedisConfig extends CachingConfigurerSupport
|
||||||
{
|
{
|
||||||
@Bean
|
@Bean
|
||||||
@SuppressWarnings(value = { "unchecked", "rawtypes", "deprecation" })
|
public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
|
RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
|
||||||
{
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||||
RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
|
||||||
template.setConnectionFactory(connectionFactory);
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||||
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
|
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance ,
|
||||||
|
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||||
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||||
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
|
||||||
serializer.setObjectMapper(mapper);
|
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||||
|
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
|
||||||
template.setValueSerializer(serializer);
|
redisTemplate.afterPropertiesSet();
|
||||||
// 使用StringRedisSerializer来序列化和反序列化redis的key值
|
return redisTemplate;
|
||||||
template.setKeySerializer(new StringRedisSerializer());
|
|
||||||
template.afterPropertiesSet();
|
|
||||||
return template;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
package com.ruoyi.common.security.feign;
|
package com.ruoyi.common.security.feign;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.constant.SecurityConstants;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import feign.RequestInterceptor;
|
import feign.RequestInterceptor;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContext;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Feign配置注册
|
* Feign配置注册
|
||||||
|
|
@ -10,11 +16,17 @@ import feign.RequestInterceptor;
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
**/
|
**/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class OAuth2FeignConfig
|
public class OAuth2FeignConfig {
|
||||||
{
|
|
||||||
@Bean
|
@Bean
|
||||||
public RequestInterceptor requestInterceptor()
|
public RequestInterceptor requestInterceptor() {
|
||||||
{
|
return (requestTemplate) -> {
|
||||||
return new OAuth2FeignRequestInterceptor();
|
SecurityContext securityContext = SecurityContextHolder.getContext();
|
||||||
|
Authentication authentication = securityContext.getAuthentication();
|
||||||
|
if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {
|
||||||
|
OAuth2AuthenticationDetails dateils = (OAuth2AuthenticationDetails) authentication.getDetails();
|
||||||
|
requestTemplate.header(HttpHeaders.AUTHORIZATION,
|
||||||
|
String.format("%s %s", SecurityConstants.BEARER_TOKEN_TYPE, dateils.getTokenValue()));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
package com.ruoyi.common.security.feign;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.security.core.Authentication;
|
|
||||||
import org.springframework.security.core.context.SecurityContext;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
|
||||||
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import com.ruoyi.common.core.constant.SecurityConstants;
|
|
||||||
import feign.RequestInterceptor;
|
|
||||||
import feign.RequestTemplate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* feign 请求拦截器
|
|
||||||
*
|
|
||||||
* @author ruoyi
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class OAuth2FeignRequestInterceptor implements RequestInterceptor
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void apply(RequestTemplate requestTemplate)
|
|
||||||
{
|
|
||||||
SecurityContext securityContext = SecurityContextHolder.getContext();
|
|
||||||
Authentication authentication = securityContext.getAuthentication();
|
|
||||||
if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails)
|
|
||||||
{
|
|
||||||
OAuth2AuthenticationDetails dateils = (OAuth2AuthenticationDetails) authentication.getDetails();
|
|
||||||
requestTemplate.header(HttpHeaders.AUTHORIZATION,
|
|
||||||
String.format("%s %s", SecurityConstants.BEARER_TOKEN_TYPE, dateils.getTokenValue()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -9,6 +9,10 @@ import java.lang.annotation.Target;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import com.ruoyi.common.swagger.config.SwaggerAutoConfiguration;
|
import com.ruoyi.common.swagger.config.SwaggerAutoConfiguration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 这个注解应该说没啥用处,因为在引入这个starter以后,就已经会去加载swagger
|
||||||
|
* 再多写一个注解根本就没用,只能通过配置文件设置是否使用swagger
|
||||||
|
*/
|
||||||
@Target({ ElementType.TYPE })
|
@Target({ ElementType.TYPE })
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue