| | |
| | | package com.doumee.config.Jwt; |
| | | |
| | | import com.doumee.config.annotation.LoginRequired; |
| | | import com.doumee.config.annotation.LoginShopRequired; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.utils.Constants; |
| | |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkLogin(request,response); |
| | | checkMemberLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkLogin(request,response); |
| | | checkMemberLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | }else if (beanType.isAnnotationPresent(LoginShopRequired.class)) { |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkShopLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | }else if (handlerMethod.hasMethodAnnotation(LoginShopRequired.class)){ |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkShopLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | } |
| | | |
| | | |
| | | return true; |
| | | } |
| | | }; |
| | |
| | | |
| | | |
| | | |
| | | public Boolean checkLogin(HttpServletRequest request, HttpServletResponse response){ |
| | | public Boolean checkMemberLogin(HttpServletRequest request, HttpServletResponse response){ |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); |
| | | try { |
| | | //判断Token是否超时 |
| | |
| | | throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录"); |
| | | } |
| | | //获取账号ID |
| | | Integer memberId = JwtTokenUtil.getJwtPayLoad(token).getMemberId(); |
| | | String memberIdInfo = JwtTokenUtil.getJwtPayLoad(token).getMemberId(); |
| | | if(StringUtils.isBlank(memberIdInfo)||!memberIdInfo.startsWith(Constants.MEMBER_PREFIX)){ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"用户信息出错"); |
| | | } |
| | | Integer memberId = Integer.valueOf(memberIdInfo.replace(Constants.MEMBER_PREFIX,"")); |
| | | Integer isDeleted = dao.queryForObject(" select COALESCE(ISDELETED,0) from Member where id = ?", Integer.class, memberId); |
| | | if(isDeleted== Constants.ONE){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员"); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | public Boolean checkShopLogin(HttpServletRequest request, HttpServletResponse response){ |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); |
| | | try { |
| | | //判断Token是否超时 |
| | | boolean expiration = JwtTokenUtil.isTokenExpired(token); |
| | | if (expiration) { |
| | | throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录"); |
| | | } |
| | | //获取账号ID |
| | | String shopInfo = JwtTokenUtil.getJwtPayLoad(token).getMemberId(); |
| | | if(StringUtils.isBlank(shopInfo)||!shopInfo.startsWith(Constants.SHOP_PREFIX)){ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"用户信息出错"); |
| | | } |
| | | Integer shopId = Integer.valueOf(shopInfo.replace(Constants.SHOP_PREFIX,"")); |
| | | Integer isDeleted = dao.queryForObject(" select COALESCE(ISDELETED,0) from shop where id = ?", Integer.class, shopId); |
| | | if(isDeleted== Constants.ONE){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员"); |
| | | } |
| | | Integer isForbidden = dao.queryForObject(" select COALESCE(STATUS,0) from shop where id = ?", Integer.class, shopId); |
| | | if(isForbidden== Constants.ONE){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已禁用,请联系管理员"); |
| | | } |
| | | Integer count = dao.queryForObject("select count(1) from shop where id = ?", Integer.class, shopId); |
| | | if (count != null && count > 0) { |
| | | request.setAttribute(JwtTokenUtil.ShopId_Name, shopId); |
| | | return true; |
| | | }else{ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"用户信息出错"); |
| | | } |
| | | } catch (IllegalArgumentException | JwtException e) { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Bean |
| | | public RestTemplate getRestTemplate(){ |
| | | return new RestTemplate(); |