| | |
| | | package com.doumee.config.Jwt; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.config.annotation.ErpLoginRequired; |
| | | import com.doumee.config.annotation.UserLoginRequired; |
| | | import com.doumee.config.annotation.LoginRequired; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.utils.Constants; |
| | | import com.doumee.core.utils.redis.RedisUtil; |
| | | 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.boot.web.servlet.FilterRegistrationBean; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.lang.reflect.Method; |
| | | import java.util.Objects; |
| | | |
| | | @Configuration |
| | |
| | | |
| | | // Method method = handlerMethod.getMethod(); |
| | | |
| | | // 有 @LoginRequired 注解,需要登录认证 |
| | | // 有 @LoginRequired 注解,需要登录认证 客户端使用 |
| | | if (beanType.isAnnotationPresent(LoginRequired.class) || handlerMethod.hasMethodAnnotation(LoginRequired.class)) { |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | } else if(beanType.isAnnotationPresent(ErpLoginRequired.class) || handlerMethod.hasMethodAnnotation(ErpLoginRequired.class)){ |
| | | try { |
| | | // 有 @UserLoginRequired 注解,需要登录认证 内部人员使用 |
| | | } else if(beanType.isAnnotationPresent(UserLoginRequired.class) || handlerMethod.hasMethodAnnotation(UserLoginRequired.class)){ |
| | | //ERP 业务注解 |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); |
| | | String redisToken = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.ERP_TOKEN,String.class); |
| | | if(StringUtils.isBlank(redisToken)||!token.equals(redisToken)){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"授权已失效"); |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkPersonnelLogin(request,response); |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | request.setAttribute(JwtTokenUtil.HEADER_KEY,token); |
| | | } catch (IllegalArgumentException | JwtException e) { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"授权已失效"); |
| | | } |
| | | // request.setAttribute("token", token); |
| | | } |
| | | return true; |
| | | } |
| | | }; |
| | | registry.addInterceptor(TokenInterceptor).addPathPatterns("/web/**","/visitbiz/**"); |
| | | registry.addInterceptor(TokenInterceptor).addPathPatterns("/web/**"); |
| | | } |
| | | |
| | | |
| | | |
| | | public Boolean checkLogin(HttpServletRequest request, HttpServletResponse response){ |
| | |
| | | throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录"); |
| | | } |
| | | //获取账号ID |
| | | String memberId = JwtTokenUtil.getJwtPayLoad(token).getMemberId(); |
| | | Long memberId = JwtTokenUtil.getJwtPayLoad(token).getUserId(); |
| | | Integer userType = JwtTokenUtil.getJwtPayLoad(token).getUserType(); |
| | | if(!Constants.equalsInteger(userType,Constants.ZERO)){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"无访问权限"); |
| | | } |
| | | 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(member.getIsdeleted()== Constants.ONE){ |
| | | if(Constants.equalsInteger(member.getIsdeleted(),Constants.ONE)){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员"); |
| | | } |
| | | if(member.getStatus() != Constants.ZERO){ |
| | | if(!Constants.equalsInteger(member.getStatus(),Constants.ZERO)){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已禁用,请联系管理员"); |
| | | } |
| | | request.setAttribute(JwtTokenUtil.UserId_Name, memberId); |
| | | request.setAttribute(JwtTokenUtil.MEMBER, member); |
| | | request.setAttribute(JwtTokenUtil.UserType, Constants.CUSTOMER); |
| | | return true; |
| | | } catch (IllegalArgumentException | JwtException e) { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | } |
| | | |
| | | public Boolean checkPersonnelLogin(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 |
| | | Long userId = JwtTokenUtil.getJwtPayLoad(token).getUserId(); |
| | | Integer userType = JwtTokenUtil.getJwtPayLoad(token).getUserType(); |
| | | if(!Constants.equalsInteger(userType,Constants.ONE)){ |
| | | throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"无访问权限"); |
| | | } |
| | | 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.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员"); |
| | | } |
| | | if(!StringUtils.equals(users.getStatus(),Constants.ZERO+"")){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已禁用,请联系管理员"); |
| | | } |
| | | request.setAttribute(JwtTokenUtil.UserId_Name, userId); |
| | | request.setAttribute(JwtTokenUtil.UserType, Constants.BUSINESS); |
| | | return true; |
| | | } catch (IllegalArgumentException | JwtException e) { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |