| | |
| | | package com.doumee.config.Jwt; |
| | | |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.config.annotation.ErpLoginRequired; |
| | | 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 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 org.springframework.jdbc.core.JdbcTemplate; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.springframework.web.method.HandlerMethod; |
| | |
| | | |
| | | @Autowired |
| | | private JdbcTemplate dao; |
| | | |
| | | @Autowired |
| | | private SystemDictDataBiz systemDictDataBiz; |
| | | |
| | | @Autowired |
| | | private RedisTemplate<String,Object> redisTemplate; |
| | | |
| | | /** |
| | | * 添加拦截器 |
| | |
| | | // Method method = handlerMethod.getMethod(); |
| | | |
| | | // 有 @LoginRequired 注解,需要登录认证 |
| | | if (beanType.isAnnotationPresent(LoginRequired.class)) { |
| | | if (beanType.isAnnotationPresent(LoginRequired.class) || handlerMethod.hasMethodAnnotation(LoginRequired.class)) { |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | } |
| | | }else if (handlerMethod.hasMethodAnnotation(LoginRequired.class)){ |
| | | //获取token |
| | | String token = request.getHeader(JwtTokenUtil.HEADER_KEY); // 从 http 请求头中取出 token |
| | | if (StringUtils.isNotBlank(token)) { |
| | | checkLogin(request,response); |
| | | } else { |
| | | } else if(beanType.isAnnotationPresent(ErpLoginRequired.class) || handlerMethod.hasMethodAnnotation(ErpLoginRequired.class)){ |
| | | try { |
| | | //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(),"未登录"); |
| | | } |
| | | 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/**"); |
| | | registry.addInterceptor(TokenInterceptor).addPathPatterns("/web/**","/visitbiz/**"); |
| | | } |
| | | |
| | | |
| | |
| | | throw new BusinessException(ResponseStatus.TOKEN_EXCEED_TIME.getCode(),"长时间未操作,请重新登录"); |
| | | } |
| | | //获取账号ID |
| | | Integer memberId = JwtTokenUtil.getJwtPayLoad(token).getMemberId(); |
| | | String memberId = JwtTokenUtil.getJwtPayLoad(token).getMemberId(); |
| | | Integer isDeleted = dao.queryForObject(" select COALESCE(DELETED,1) from `system_user` where id = ?", Integer.class, memberId); |
| | | if(isDeleted== Constants.ONE){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户已删除,请联系管理员"); |