New file |
| | |
| | | package com.kidgrow.zuul.filter; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.kidgrow.common.constant.CommonConstant; |
| | | import com.kidgrow.common.constant.SecurityConstants; |
| | | import com.kidgrow.common.model.ResultBody; |
| | | import com.kidgrow.common.model.SysOrganization; |
| | | import com.kidgrow.common.model.SysUser; |
| | | import com.kidgrow.common.model.SysUserOrg; |
| | | import com.kidgrow.common.utils.AddrUtil; |
| | | import com.kidgrow.redis.util.RedisConstant; |
| | | import com.kidgrow.redis.util.RedisUtils; |
| | | import com.kidgrow.zuul.feign.SysOrganizationService; |
| | | import com.kidgrow.zuul.feign.SysUserOrgService; |
| | | import com.netflix.zuul.ZuulFilter; |
| | | import com.netflix.zuul.context.RequestContext; |
| | | import eu.bitwalker.useragentutils.UserAgent; |
| | | import lombok.SneakyThrows; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.hibernate.validator.constraints.NotBlank; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.security.oauth2.provider.OAuth2Authentication; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER; |
| | | |
| | | /** |
| | | * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br> |
| | | * |
| | | * @Description: 将认证用户的相关信息放入header中, 后端服务可以直接读取使用 包含了----组织的拦截---<br> |
| | | * @Project: <br> |
| | | * @CreateDate: Created in 2020/2/21 10:12 <br> |
| | | * @Author: <a href="4345453@kidgrow.com">liuke</a> |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class OrganizationFilter extends ZuulFilter { |
| | | @Override |
| | | public String filterType() { |
| | | return FilterConstants.PRE_TYPE; |
| | | } |
| | | |
| | | @Override |
| | | public int filterOrder() { |
| | | return PRE_DECORATION_FILTER_ORDER; |
| | | } |
| | | |
| | | @Override |
| | | public boolean shouldFilter() { |
| | | return true; |
| | | } |
| | | |
| | | @Autowired |
| | | RedisUtils redisUtils; |
| | | @Autowired |
| | | private SysUserOrgService sysUserOrgService; |
| | | @Autowired |
| | | private SysOrganizationService sysOrganizationService; |
| | | |
| | | private final String CLIENTID = "webApp";//运营端 |
| | | |
| | | @SneakyThrows |
| | | @Override |
| | | public Object run() { |
| | | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) { |
| | | Object principal = authentication.getPrincipal(); |
| | | RequestContext ctx = RequestContext.getCurrentContext(); |
| | | if (principal instanceof SysUser) { |
| | | //运营端进行 |
| | | OAuth2Authentication oauth2Authentication = (OAuth2Authentication) authentication; |
| | | String clientId = oauth2Authentication.getOAuth2Request().getClientId(); |
| | | if (CLIENTID.equals(clientId)) { |
| | | SysUser user = (SysUser) authentication.getPrincipal(); |
| | | /** |
| | | * 将组织中为空的拦截 |
| | | */ |
| | | List<SysUserOrg> sysUserOrgs = getSysUserOrg(user.getId()); |
| | | if (sysUserOrgs == null || sysUserOrgs.isEmpty()) { |
| | | ctx.setSendZuulResponse(false); |
| | | ctx.setResponseBody(JSON.toJSONString(ResultBody.fail(1000, "您的组织已经被禁用,请联系管理员"))); |
| | | } |
| | | // else { |
| | | // List<Long> collect = sysUserOrgs.stream().map(e -> e.getOrgId()).collect(Collectors.toList()); |
| | | // List<SysOrganization> sysOrganizations = getSysOrganization(); |
| | | // List<Long> orgIds = sysOrganizations.stream().filter(e -> e.getEnabled() == true && collect.contains(e.getId())).map(e -> e.getId()).collect(Collectors.toList()); |
| | | // if (orgIds == null || orgIds.size() <= 0) { |
| | | // ctx.setSendZuulResponse(false); |
| | | // ctx.setResponseBody(JSON.toJSONString(ResultBody.fail(1000, "您的组织已经被禁用,请联系管理员"))); |
| | | // } |
| | | // } |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 通过userID 获取组织的关系 |
| | | * |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public List<SysUserOrg> getSysUserOrg(Long str) { |
| | | //获取组织 Redis中获取 |
| | | List<SysUserOrg> sysUserOrgs = JSON.parseArray(JSON.toJSONString(redisUtils.hget(RedisConstant.USER_ORGANIZATION, str.toString())), SysUserOrg.class); |
| | | if (sysUserOrgs == null || sysUserOrgs.size() <= 0) { |
| | | //在数据库查询 |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("user_id", str); |
| | | sysUserOrgs = sysUserOrgService.getListByMap(map); |
| | | redisUtils.hset(RedisConstant.USER_ORGANIZATION, str.toString(), sysUserOrgs); |
| | | } |
| | | return sysUserOrgs; |
| | | } |
| | | |
| | | public List<SysOrganization> getSysOrganization() { |
| | | Map<Object, Object> hmget = redisUtils.hmget(RedisConstant.ORGANIZATION); |
| | | List<SysOrganization> sysOrganizations = new ArrayList<>(); |
| | | if (hmget == null || hmget.size() <= 0) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("is_del", 0); |
| | | sysOrganizations = JSON.parseArray(JSON.toJSONString(sysOrganizationService.getListByMap(map).getData()), SysOrganization.class); |
| | | sysOrganizations.forEach(e -> { |
| | | redisUtils.hset(RedisConstant.ORGANIZATION, e.getId().toString(), e); |
| | | }); |
| | | |
| | | } else { |
| | | Set<Object> objects = hmget.keySet(); |
| | | Iterator<Object> iterator = objects.iterator(); |
| | | while (iterator.hasNext()) { |
| | | sysOrganizations.add((SysOrganization) hmget.get(iterator.next())); |
| | | } |
| | | } |
| | | return sysOrganizations; |
| | | } |
| | | } |
| | | |