| | |
| | | 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.SysDoctor; |
| | | import com.kidgrow.common.model.SysOrganization; |
| | | import com.kidgrow.common.model.SysUser; |
| | | import com.kidgrow.common.utils.AddrUtil; |
| | | import com.kidgrow.zuul.feign.SysDoctorService; |
| | | 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.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants; |
| | | import org.springframework.security.authentication.AnonymousAuthenticationToken; |
| | | import org.springframework.security.core.Authentication; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.net.URLEncoder; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.FORM_BODY_WRAPPER_FILTER_ORDER; |
| | | import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER; |
| | | |
| | | /** |
| | | * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br> |
| | | * |
| | | * @Description: 将认证用户的相关信息放入header中, 后端服务可以直接读取使用<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 UserInfoHeaderFilter extends ZuulFilter { |
| | | @Autowired |
| | | private SysDoctorService sysDoctorService; |
| | | @Override |
| | | public String filterType() { |
| | | return FilterConstants.PRE_TYPE; |
| | |
| | | return true; |
| | | } |
| | | |
| | | |
| | | |
| | | @SneakyThrows |
| | | @Override |
| | | public Object run() { |
| | | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | |
| | | RequestContext ctx = RequestContext.getCurrentContext(); |
| | | HttpServletRequest req = ctx.getRequest(); |
| | | UserAgent userAgent = UserAgent.parseUserAgentString(req.getHeader("User-Agent")); |
| | | String tenantId=""; |
| | | String tenantId = ""; |
| | | //客户端模式只返回一个clientId |
| | | if (principal instanceof SysUser) { |
| | | SysUser user = (SysUser)authentication.getPrincipal(); |
| | | |
| | | SysUser user = (SysUser) authentication.getPrincipal(); |
| | | Map<String,Object> map=new HashMap<>(); |
| | | map.put("user_id",user.getId()); |
| | | ResultBody listByMap = sysDoctorService.getListByMap(map); |
| | | |
| | | List<SysDoctor> sysDoctors = JSON.parseArray(JSON.toJSONString(listByMap.getData()), SysDoctor.class); |
| | | String str=""; |
| | | Long hospital=-1L; |
| | | if(sysDoctors!=null&&sysDoctors.size()>0){ |
| | | str= sysDoctors.get(0).getDoctorName(); |
| | | hospital=sysDoctors.get(0).getHospitalId(); |
| | | } |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_ID_HEADER, String.valueOf(user.getId())); |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_HEADER, user.getUsername()); |
| | | tenantId=String.valueOf(user.getId()); |
| | | // log.error("用户ID写入Header成功,用户ID为:"+String.valueOf(user.getId())); |
| | | |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_REAL_NAME,URLEncoder.encode(str,"UTF-8"));//待完善 |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_HOSPITAL_ID_HEADER,hospital.toString()); |
| | | tenantId=hospital.toString(); |
| | | List<SysOrganization> organizations = (List<SysOrganization>) user.getOrganizations(); |
| | | //将组织数据 存到header 里面 |
| | | organizations.sort((e1,e2)->e1.getOrgLevel().compareTo(e2.getOrgLevel())); |
| | | if (organizations != null&&organizations.size()>0) { |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_ORG_ID_HEADER,String.valueOf(organizations.get(0).getId())); |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_ORG_NAME_HEADER, URLEncoder.encode(organizations.get(0).getOrgName(),"UTF-8")); |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_DEP_ID_HEADER,String.valueOf(organizations.get(organizations.size()-1).getId())); |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_DEP_NAME_HEADER,URLEncoder.encode(organizations.get(organizations.size()-1).getOrgName(),"UTF-8")); |
| | | ctx.addZuulRequestHeader(SecurityConstants.USER_ORGS_HEADER,JSON.toJSONString(organizations)); |
| | | } |
| | | //将角色放到header |
| | | ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, JSON.toJSONString(user.getRoles())); |
| | | |
| | | } |
| | | |
| | | |
| | | OAuth2Authentication oauth2Authentication = (OAuth2Authentication)authentication; |
| | | OAuth2Authentication oauth2Authentication = (OAuth2Authentication) authentication; |
| | | String clientId = oauth2Authentication.getOAuth2Request().getClientId(); |
| | | |
| | | //保存租户id,租户id根据业务尽进行替换 |
| | | switch (clientId){ |
| | | switch (clientId) { |
| | | case "hospital": |
| | | tenantId=tenantId; |
| | | tenantId = tenantId; |
| | | break; |
| | | case "webApp": |
| | | tenantId="webApp"; |
| | | tenantId = "webApp"; |
| | | break; |
| | | default: |
| | | tenantId=clientId; |
| | | tenantId = clientId; |
| | | break; |
| | | } |
| | | ctx.addZuulRequestHeader(CommonConstant.USER_AGENT_BROWSER, userAgent.getBrowser().toString()); |
| | |
| | | |
| | | ctx.addZuulRequestHeader(SecurityConstants.TENANT_HEADER, tenantId); |
| | | ctx.addZuulRequestHeader(SecurityConstants.CLIENT_HEADER, clientId); |
| | | ctx.addZuulRequestHeader(SecurityConstants.ROLE_HEADER, CollectionUtil.join(authentication.getAuthorities(), ",")); |
| | | |
| | | } |
| | | return null; |
| | | } |