| | |
| | | package com.doumee.config.Jwt; |
| | | |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.biz.zbom.model.zhongtai.ZTConstants; |
| | | import com.doumee.config.annotation.LoginRequired; |
| | | import com.doumee.config.annotation.UserLoginRequired; |
| | | import com.doumee.core.constants.Constants; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.utils.Constants; |
| | | import com.doumee.dao.business.model.Member; |
| | | import com.doumee.dao.business.model.Users; |
| | | import io.jsonwebtoken.JwtException; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.jdbc.core.BeanPropertyRowMapper; |
| | | import org.springframework.jdbc.core.JdbcTemplate; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.web.method.HandlerMethod; |
| | | import org.springframework.web.servlet.HandlerInterceptor; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.Objects; |
| | | |
| | | @Configuration |
| | | public class WebMvcConfig implements WebMvcConfigurer { |
| | | @Autowired |
| | | private JdbcTemplate dao; |
| | | |
| | | @Resource |
| | | private JwtTokenUtil jwtTokenUtil; |
| | | @Autowired |
| | | private SystemDictDataBiz systemDictDataBiz; |
| | | |
| | |
| | | if (handlerMethod.hasMethodAnnotation(LoginRequired.class)) { |
| | | checkFlag = checkLogin(request,response,token); |
| | | } |
| | | if(!checkFlag && handlerMethod.hasMethodAnnotation(UserLoginRequired.class)){ |
| | | checkFlag = checkPersonnelLogin(request,response,token); |
| | | } |
| | | if(!checkFlag){ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"æªç»å½"); |
| | | } |
| | |
| | | |
| | | public Boolean checkLogin(HttpServletRequest request, HttpServletResponse response,String token){ |
| | | try { |
| | | String tokenRedis = (String) redisTemplate.opsForValue().get(Constants.REDIS_TOKEN_KEY+"_"+token); |
| | | if(StringUtils.isBlank(tokenRedis)){ |
| | | Member member = jwtTokenUtil.getUserInfoByToken(token); |
| | | if(member == null){ |
| | | return false; |
| | | } |
| | | Long memberId = getTokenId(token); |
| | | Member member = dao.queryForObject(" select * from `member` where id = ? limit 1 ", new BeanPropertyRowMapper<>(Member.class),memberId); |
| | | if(Objects.isNull(member)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | if(Objects.isNull(member.getOpenid())){ |
| | | throw new BusinessException(ResponseStatus.USER_DISABLE_TIME.getCode(),"ç¨æ·å·²æ³¨é,è¯·éæ°ç»å½"); |
| | | } |
| | | if(Constants.equalsInteger(member.getIsdeleted(),Constants.ONE)){ |
| | | throw new BusinessException(ResponseStatus.USER_DISABLE_TIME.getCode(),"ç¨æ·å·²å é¤,请è系管çå"); |
| | | } |
| | | if(!Constants.equalsInteger(member.getStatus(),Constants.ZERO)){ |
| | | throw new BusinessException(ResponseStatus.USER_DISABLE_TIME.getCode(),"ç¨æ·å·²ç¦ç¨,请è系管çå"); |
| | | } |
| | | request.setAttribute(JwtTokenUtil.UserId_Name, memberId); |
| | | request.setAttribute(JwtTokenUtil.HEADER_KEY, MEMBERTOKEM"_"+token); |
| | | request.setAttribute(JwtTokenUtil.UserInfo, tokenRedis); |
| | | return true; |
| | | } catch (IllegalArgumentException | JwtException e) { |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"æªç»å½"); |
| | | } |
| | | } |
| | | |
| | | public Boolean checkPersonnelLogin(HttpServletRequest request, HttpServletResponse response,String token){ |
| | | try { |
| | | String tokenRedis = (String) redisTemplate.opsForValue().get(ZTConstants.BUSINESS+"_"+token); |
| | | if(StringUtils.isBlank(tokenRedis)){ |
| | | return false; |
| | | } |
| | | Long userId = getTokenId(token); |
| | | Users users = dao.queryForObject(" select * from `users` where id = ? limit 1 ", new BeanPropertyRowMapper<>(Users.class),userId); |
| | | if(Objects.isNull(users)){ |
| | | throw new BusinessException(ResponseStatus.DATA_EMPTY); |
| | | } |
| | | if(Constants.equalsInteger(users.getIsdeleted(),Constants.ONE)){ |
| | | throw new BusinessException(ResponseStatus.USER_DISABLE_TIME.getCode(),"ç¨æ·å·²å é¤,请è系管çå"); |
| | | } |
| | | if(!StringUtils.equals(users.getStatus(),Constants.ONE+"")){ |
| | | throw new BusinessException(ResponseStatus.USER_DISABLE_TIME.getCode(),"ç¨æ·ç¶æå¼å¸¸,è¯·éæ°ç»å½"); |
| | | } |
| | | request.setAttribute(JwtTokenUtil.UserId_Name, userId); |
| | | request.setAttribute(JwtTokenUtil.UserType, ZTConstants.BUSINESS); |
| | | request.setAttribute(JwtTokenUtil.HEADER_KEY, ZTConstants.CUSTOMER+"_"+token); |
| | | request.setAttribute(JwtTokenUtil.UserInfo, tokenRedis); |
| | | return true; |
| | | } catch (IllegalArgumentException | JwtException e) { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"æªç»å½"); |
| | | } |
| | | } |
| | | |
| | | @Bean |
| | | public RestTemplate getRestTemplate(){ |
| | | return new RestTemplate(); |
| | | } |
| | | |
| | | public Long getTokenId(String token){ |
| | | try { |
| | | Integer lastIndex = token.lastIndexOf("_")+1; |
| | | Long tokenId = Long.valueOf(token.substring(lastIndex)); |
| | | return tokenId; |
| | | }catch (Exception e){ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"æªç»å½"); |
| | | } |
| | | } |
| | | |
| | | } |