修改代码
This commit is contained in:
@ -122,7 +122,7 @@
|
|||||||
<artifactId>poi-ooxml-schemas</artifactId>
|
<artifactId>poi-ooxml-schemas</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--security-->
|
<!-- <!–security–>-->
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.springframework.cloud</groupId>-->
|
<!-- <groupId>org.springframework.cloud</groupId>-->
|
||||||
<!-- <artifactId>spring-cloud-security</artifactId>-->
|
<!-- <artifactId>spring-cloud-security</artifactId>-->
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.cloud.security.starter.filter;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class BearerTokenFilter implements Filter {
|
||||||
|
private static final String AUTHORIZATION_HEADER = "Authorization";
|
||||||
|
private static final String BEARER_PREFIX = "Bearer ";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||||
|
try {
|
||||||
|
if (request instanceof HttpServletRequest) {
|
||||||
|
HttpServletRequest httpRequest = (HttpServletRequest) request;
|
||||||
|
String authHeader = httpRequest.getHeader(AUTHORIZATION_HEADER);
|
||||||
|
if (authHeader != null && authHeader.startsWith(BEARER_PREFIX)) {
|
||||||
|
String token = authHeader.substring(BEARER_PREFIX.length());
|
||||||
|
BearerTokenHolder.setToken(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
} finally {
|
||||||
|
BearerTokenHolder.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.cloud.security.starter.filter;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class BearerTokenFilterConfig {
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean<BearerTokenFilter> bearerTokenFilterRegistration() {
|
||||||
|
FilterRegistrationBean<BearerTokenFilter> registration = new FilterRegistrationBean<>();
|
||||||
|
registration.setFilter(new BearerTokenFilter());
|
||||||
|
registration.addUrlPatterns("/*");
|
||||||
|
registration.setName("bearerTokenFilter");
|
||||||
|
registration.setOrder(-100); // 优先级高于 Spring Security
|
||||||
|
return registration;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.cloud.security.starter.filter;
|
||||||
|
|
||||||
|
public class BearerTokenHolder {
|
||||||
|
private static final ThreadLocal<String> TOKEN_HOLDER = new ThreadLocal<>();
|
||||||
|
|
||||||
|
public static void setToken(String token) {
|
||||||
|
TOKEN_HOLDER.set(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getToken() {
|
||||||
|
return TOKEN_HOLDER.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clear() {
|
||||||
|
TOKEN_HOLDER.remove();
|
||||||
|
}
|
||||||
|
}
|
@ -1,31 +1,35 @@
|
|||||||
|
|
||||||
package com.chinaunicom.mall.ebtp.cloud.userinfo.starter.service.impl;
|
package com.chinaunicom.mall.ebtp.cloud.userinfo.starter.service.impl;
|
||||||
|
|
||||||
|
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.HEADER_CHECK_TOKEN;
|
||||||
|
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.REDIS_USER_KEY;
|
||||||
|
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.REMOTE_ACCESS_FAILURE;
|
||||||
|
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.TOKEN_PREFIX;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.remoting.RemoteTimeoutException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.chinaunicom.mall.ebtp.cloud.security.starter.entity.AuthorityEntity;
|
import com.chinaunicom.mall.ebtp.cloud.security.starter.entity.AuthorityEntity;
|
||||||
import com.chinaunicom.mall.ebtp.cloud.security.starter.entity.SecurityEntity;
|
import com.chinaunicom.mall.ebtp.cloud.security.starter.entity.SecurityEntity;
|
||||||
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.client.EbtpUserInfoClient;
|
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.client.EbtpUserInfoClient;
|
||||||
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.client.UnifastOAuthClient;
|
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.client.UnifastOAuthClient;
|
||||||
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.entity.CacheRole;
|
|
||||||
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.entity.CacheUser;
|
|
||||||
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.entity.CheckTokenVo;
|
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.entity.CheckTokenVo;
|
||||||
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.service.UserInfoService;
|
import com.chinaunicom.mall.ebtp.cloud.userinfo.starter.service.UserInfoService;
|
||||||
import com.chinaunicom.mall.ebtp.common.base.entity.BaseCacheUser;
|
import com.chinaunicom.mall.ebtp.common.base.entity.BaseCacheUser;
|
||||||
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
|
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
|
||||||
import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.remoting.RemoteTimeoutException;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.*;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@ -4,8 +4,13 @@ import com.chinaunicom.mall.ebtp.common.base.entity.BaseCacheUser;
|
|||||||
import com.chinaunicom.mall.ebtp.common.base.service.IBaseCacheUserService;
|
import com.chinaunicom.mall.ebtp.common.base.service.IBaseCacheUserService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import com.chinaunicom.mall.ebtp.cloud.security.starter.filter.BearerTokenHolder;
|
||||||
|
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.REDIS_USER_KEY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存用户service实现层 获取缓存用户信息
|
* 缓存用户service实现层 获取缓存用户信息
|
||||||
@ -18,18 +23,29 @@ import org.springframework.stereotype.Service;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class BaseCacheUserServiceImpl implements IBaseCacheUserService {
|
public class BaseCacheUserServiceImpl implements IBaseCacheUserService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("userinfoRedisTemplate")
|
||||||
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BaseCacheUser getCacheUser() {
|
public BaseCacheUser getCacheUser() {
|
||||||
BaseCacheUser buser = new BaseCacheUser();
|
|
||||||
try {
|
try {
|
||||||
BeanUtils.copyProperties(SecurityContextHolder.getContext().getAuthentication().getPrincipal(), buser);
|
String token = BearerTokenHolder.getToken();
|
||||||
|
if (token == null || token.isEmpty()) {
|
||||||
|
log.warn("未获取到token");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Object o = redisTemplate.opsForValue().get(REDIS_USER_KEY + token);
|
||||||
|
if (o instanceof BaseCacheUser) {
|
||||||
|
return (BaseCacheUser) o;
|
||||||
|
} else {
|
||||||
|
log.warn("redis中未找到用户信息,token:{}", token);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage());
|
log.error("获取缓存用户信息异常", e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
log.debug("Current user principal: " + buser);
|
|
||||||
|
|
||||||
return buser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ spring:
|
|||||||
password: Unicom#135
|
password: Unicom#135
|
||||||
|
|
||||||
# 天宫Eureka配置
|
# 天宫Eureka配置
|
||||||
eureka:asdf
|
eureka:
|
||||||
client:
|
client:
|
||||||
service-url:
|
service-url:
|
||||||
defaultZone: http://10.242.37.148:5001/eureka
|
defaultZone: http://10.242.37.148:5001/eureka
|
||||||
|
Reference in New Issue
Block a user