| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.doumee.config.annotation.CloudRequiredPermission; |
| | | import com.doumee.config.annotation.LoginNoRequired; |
| | | import com.doumee.config.jwt.JwtProperties; |
| | | import com.doumee.config.jwt.JwtTokenUtil; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.core.model.LoginUserInfo; |
| | | import com.doumee.service.business.third.model.LoginUserInfo; |
| | | import com.doumee.core.utils.Constants; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.shiro.authz.UnauthorizedException; |
| | | import org.apache.shiro.authz.annotation.RequiresPermissions; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.web.method.HandlerMethod; |
| | | import org.springframework.web.servlet.HandlerInterceptor; |
| | | import org.springframework.web.util.ContentCachingRequestWrapper; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.ServletInputStream; |
| | | import javax.servlet.http.Cookie; |
| | | import javax.servlet.http.HttpServletRequest; |
| | |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Slf4j |
| | | public class LoginHandlerInterceptor implements HandlerInterceptor { |
| | | |
| | | private RedisTemplate<String,Object> stringRedisTemplate; |
| | | private long expireTime; |
| | | |
| | | public long getExpireTime() { |
| | | return expireTime; |
| | | } |
| | | |
| | | public void setExpireTime(long expireTime) { |
| | | this.expireTime = expireTime; |
| | | } |
| | | |
| | | // 由于该类未交给spring管理,因此不能使用自动装配的方式获取RedisTemplate对象 |
| | | public LoginHandlerInterceptor(RedisTemplate<String,Object> stringRedisTemplate) { |
| | | public LoginHandlerInterceptor(RedisTemplate<String,Object> stringRedisTemplate,long expireTime) { |
| | | this.expireTime = expireTime; |
| | | this.stringRedisTemplate = stringRedisTemplate; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | } |
| | | /*try { |
| | | /* try { |
| | | CustomHttpServletRequestWrapper requestWrapper = (CustomHttpServletRequestWrapper)request; |
| | | String body = requestWrapper.getBody(); |
| | | JSONObject object = JSONObject.parseObject(body); |
| | |
| | | requestWrapper.setBody( JSONObject.toJSONString(object)); |
| | | } |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | }*/ |
| | | } else { |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),request.getRequestURI()+"未登录,TOKEN缺失"); |
| | | } |
| | | } |
| | | }else{ |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); |
| | | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),request.getRequestURI()+"未登录"); |
| | | } |
| | | |
| | | return true; |
| | |
| | | if(user ==null ){ |
| | | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户登陆已失效,请重新登陆!"); |
| | | } |
| | | log.error("=========开始刷新token有效期:"+token+"======time:"+this.getExpireTime()); |
| | | stringRedisTemplate.expire(Constants.REDIS_TOKEN_KEY+token,this.getExpireTime(), TimeUnit.MILLISECONDS); |
| | | //权限判断------------ |
| | | return user; |
| | | } |