@Component
public class LoginFilterTest extends ZuulFilter {
private static final Logger LOG = LoggerFactory.getLogger(LoginFilterTest.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 2;//int值来定义过滤器的执行顺序,数值越小优先级越高
}
@Override
public boolean shouldFilter() {// 该过滤器需要执行
return true;
}
@Override
public Object run() {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletResponse response = requestContext.getResponse();
HttpServletRequest request = requestContext.getRequest();
//取出头部信息Authorization
String authorization = request.getHeader("Authorization");
if(StringUtils.isEmpty(authorization)){
requestContext.setSendZuulResponse(false);// 拒绝访问
requestContext.setResponseStatusCode(200);// 设置响应状态码
ResponseResult unauthenticated = new ResponseResult(CommonCode.UNAUTHENTICATED);
String jsonString = JSON.toJSONString(unauthenticated);
requestContext.setResponseBody(jsonString);
requestContext.getResponse().setContentType("application/json;charset=UTF‐8");
return null;
}
return null;
}
}
{
"code": 10001,
"message": "此操作需要登陆系统!",
"success": false
}
spring:
application:
name: xc‐govern‐gateway
redis:
host: ${REDIS_HOST:127.0.0.1}
port: ${REDIS_PORT:6379}
timeout: 5000 #连接超时 毫秒
jedis:
pool:
maxActive: 3
maxIdle: 3
minIdle: 1
maxWait: ‐1 #连接池最大等行时间 ‐1没有限制
@Service
public class AuthService {
@Autowired
StringRedisTemplate stringRedisTemplate;
//查询身份令牌
public String getTokenFromCookie(HttpServletRequest request){
Map<String, String> cookieMap = CookieUtil.readCookie(request, "uid");
String access_token = cookieMap.get("uid");
if(StringUtils.isEmpty(access_token)){
return null;
}
return access_token;
}
//从header中查询jwt令牌
public String getJwtFromHeader(HttpServletRequest request){
String authorization = request.getHeader("Authorization");
if(StringUtils.isEmpty(authorization)){
//拒绝访问
return null;
}
if(!authorization.startsWith("Bearer ")){
//拒绝访问
return null;
}
return authorization;
}
//查询令牌的有效期
public long getExpire(String access_token) {
//token在redis中的key
String key = "user_token:"+access_token;
Long expire = stringRedisTemplate.getExpire(key);
return expire;
}
}
@Component
public class LoginFilter extends ZuulFilter {
private static final Logger LOGG = LoggerFactory.getLogger(LoginFilter.class);
@Autowired
AuthService authService;
@Override
public String filterType() {
//四种类型:pre、routing、post、error
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
//上下文对象
RequestContext requestContext = RequestContext.getCurrentContext();
//请求对象
HttpServletRequest request = requestContext.getRequest();
//查询身份令牌
String access_token = authService.getTokenFromCookie(request);
if(access_token == null){
//拒绝访问
access_denied();
}
//从redis中校验身份令牌是否过期
long expire = authService.getExpire(access_token);
if(expire<=0){
//拒绝访问
access_denied();
}
//查询jwt令牌
String jwt = authService.getJwtFromHeader(request);
if(jwt == null){
//拒绝访问
access_denied();
}
return null;
}
//拒绝访问
private void access_denied(){
//上下文对象
RequestContext requestContext = RequestContext.getCurrentContext();
requestContext.setSendZuulResponse(false);//拒绝访问
//设置响应内容
ResponseResult responseResult =new ResponseResult(CommonCode.UNAUTHENTICATED);
String responseResultString = JSON.toJSONString(responseResult);
requestContext.setResponseBody(responseResultString);
//设置状态码
requestContext.setResponseStatusCode(200);
HttpServletResponse response = requestContext.getResponse();
response.setContentType("application/json;charset=utf‐8");
}
}
#微服务网关
upstream api_server_pool{
server 127.0.0.1:50201 weight=10;
}
#微服务网关
location /api {
proxy_pass http://api_server_pool;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |