springBoot項目還不是我搭建的,不過用了下感覺确實簡單的多,配置檔案真少。
這次的場景是手機端端請求如果一個公司被廢棄了,那麼公司下的客戶都要停止業務操作,是以想到的就是加攔截,判斷使用者所屬公司是否廢棄,然後提示,讓前端處理強制退出。
上重點
OrgScrapInterceptor,我增加的,這個需要實作HandlerInterceptor
public class OrgScrapInterceptor implements HandlerInterceptor {
private static final Logger logger = LoggerFactory.getLogger(OrgScrapInterceptor.class);
@Resource
private BasicCompanyOrgService orgService;
@Autowired
private RedisUtil redisUtil;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (orgService == null) {//解決service為null無法注入問題
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
orgService = (BasicCompanyOrgService) factory.getBean(BasicCompanyOrgService.class);
}
CompanyOrgDto org = null;
String custNo = getCurLoginCustNoByToken(request);
if(StringUtils.isBlank(custNo)) { //未登入
String uri = request.getRequestURI();
if(uri.contains("basic/company/org/info")) {
//傳參使用的是url上{}的方式,這裡取參數肯定是不能注解取,用下面的方式
NativeWebRequest webRequest = new ServletWebRequest(request);
Map<String, String> pararmMp = (Map<String, String>) webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
String orgToken = pararmMp.get("orgToken");
if(StringUtils.isNotBlank(orgToken)) {
org = orgService.getCompanyOrgByOrgToken(orgToken);
}
}else {
logger.info("--------當期登入使用者token為空,uri:" + uri);
}
}else {
org = orgService.getUserOrgInfo(custNo);
}
if(org != null) {
int orgStatus = org.getOrgStatus();
//公司機構已廢棄
if(orgStatus == 2) {
Result result = ResultGenerator.genBusinessFailResult("公司機構已廢棄", BusinessResultCode.ORG_SCRAP);
WebMvcConfigurer.responseResult(response, result);
return false;
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
//使用者token
public String getCurLoginCustNoByToken(HttpServletRequest request){
String token = request.getHeader("X-Token");
if(StringUtils.isBlank(token)){
//logger.info("--------------當期登入使用者token為空--------------------");
return null;
}
redis 緩存中判斷是否存在目前token
RedisKey custRedisKey = RedisKeyUtils.getLoginCustKey(token);
if (redisUtil == null) {//解決service為null無法注入問題
BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
redisUtil = factory.getBean(RedisUtil.class);
}
String custNo = (String) redisUtil.get(custRedisKey.getKey());
/判斷目前redis中是否存在目前token
if(StringUtils.isBlank(custNo)){
logger.info("--------------擷取當期登入使用者CustNo為空---------------------------");
return null;
}
return custNo;
}
}
//攔截器注冊
在WebMvcConfigurer extends WebMvcConfigurerAdapter的addInterceptors(InterceptorRegistry registry) 裡注冊
就一句話
registry.addInterceptor(new OrgScrapInterceptor());
然後就開始生效了,就是這麼簡單,感覺确實比自己搭建的springMVC的架構好用,都是是maven裡增加配置引入。
springBoot的樂趣還需要進一步學習挖掘。