|
|
@ -6,6 +6,8 @@ import com.huoran.iasf.common.exception.code.BaseResponseCode; |
|
|
|
import com.huoran.iasf.common.utils.Constant; |
|
|
|
import com.huoran.iasf.common.utils.Constant; |
|
|
|
import com.huoran.iasf.common.utils.R; |
|
|
|
import com.huoran.iasf.common.utils.R; |
|
|
|
import com.huoran.iasf.common.utils.HttpContextUtils; |
|
|
|
import com.huoran.iasf.common.utils.HttpContextUtils; |
|
|
|
|
|
|
|
import io.jsonwebtoken.Claims; |
|
|
|
|
|
|
|
import io.jsonwebtoken.Jwts; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.shiro.authc.AuthenticationException; |
|
|
|
import org.apache.shiro.authc.AuthenticationException; |
|
|
|
import org.apache.shiro.authc.UsernamePasswordToken; |
|
|
|
import org.apache.shiro.authc.UsernamePasswordToken; |
|
|
@ -25,6 +27,7 @@ import java.io.IOException; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static com.huoran.iasf.service.HttpApiSessionService.*; |
|
|
|
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; |
|
|
|
import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -79,45 +82,31 @@ public class CustomAccessControlFilter extends AccessControlFilter { |
|
|
|
// 指定处理该请求的处理器
|
|
|
|
// 指定处理该请求的处理器
|
|
|
|
request.getRequestDispatcher(Constant.ERROR_CONTROLLER_PATH).forward(request, servletResponse); |
|
|
|
request.getRequestDispatcher(Constant.ERROR_CONTROLLER_PATH).forward(request, servletResponse); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 校验并解析token,如果token过期或者篡改,则会返回null
|
|
|
|
|
|
|
|
Claims claims = checkJWT(token); |
|
|
|
|
|
|
|
if (null == claims) { |
|
|
|
|
|
|
|
request.setAttribute("filterError", new BusinessException(BaseResponseCode.TOKEN_ERROR)); |
|
|
|
|
|
|
|
// 指定处理该请求的处理器
|
|
|
|
|
|
|
|
request.getRequestDispatcher(Constant.ERROR_CONTROLLER_PATH).forward(request, servletResponse); |
|
|
|
|
|
|
|
}else { |
|
|
|
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(token, token); |
|
|
|
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(token, token); |
|
|
|
getSubject(servletRequest, servletResponse).login(usernamePasswordToken); |
|
|
|
getSubject(servletRequest, servletResponse).login(usernamePasswordToken); |
|
|
|
/*} catch (BusinessException exception) { |
|
|
|
|
|
|
|
if (HttpContextUtils.isAjaxRequest(request)) { |
|
|
|
|
|
|
|
customResponse(exception.getBaseResponseCode().getCode(), exception.getMessage(), servletResponse); |
|
|
|
|
|
|
|
} else if (exception.getBaseResponseCode().getCode() == BaseResponseCode.TOKEN_ERROR.getCode()) { |
|
|
|
|
|
|
|
servletRequest.getRequestDispatcher("/index/login").forward(servletRequest, servletResponse); |
|
|
|
|
|
|
|
} else if (exception.getBaseResponseCode().getCode() == BaseResponseCode.UNAUTHORIZED_ERROR.getCode()) { |
|
|
|
|
|
|
|
servletRequest.getRequestDispatcher("/index/403").forward(servletRequest, servletResponse); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
servletRequest.getRequestDispatcher("/index/500").forward(servletRequest, servletResponse); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return true; |
|
|
|
} catch (AuthenticationException e) { |
|
|
|
|
|
|
|
if (HttpContextUtils.isAjaxRequest(request)) { |
|
|
|
|
|
|
|
if (e.getCause() instanceof BusinessException) { |
|
|
|
|
|
|
|
BusinessException exception = (BusinessException) e.getCause(); |
|
|
|
|
|
|
|
customResponse(exception.getBaseResponseCode().getCode(), exception.getMessage(), servletResponse); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
customResponse(BaseResponseCode.SYSTEM_BUSY.getCode(), BaseResponseCode.SYSTEM_BUSY.getMsg(), servletResponse); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
servletRequest.getRequestDispatcher("/index/403").forward(servletRequest, servletResponse); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 校验token |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Claims checkJWT(String token) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
final Claims claims = Jwts.parser().setSigningKey(APP_SECRET). |
|
|
|
|
|
|
|
parseClaimsJws(token).getBody(); |
|
|
|
|
|
|
|
return claims; |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
if (HttpContextUtils.isAjaxRequest(request)) { |
|
|
|
return null; |
|
|
|
if (e.getCause() instanceof BusinessException) { |
|
|
|
|
|
|
|
BusinessException exception = (BusinessException) e.getCause(); |
|
|
|
|
|
|
|
customResponse(exception.getBaseResponseCode().getCode(), exception.getMessage(), servletResponse); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
customResponse(BaseResponseCode.SYSTEM_BUSY.getCode(), BaseResponseCode.SYSTEM_BUSY.getMsg(), servletResponse); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
servletRequest.getRequestDispatcher("/index/500").forward(servletRequest, servletResponse); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void customResponse(int code, String msg, ServletResponse response) { |
|
|
|
private void customResponse(int code, String msg, ServletResponse response) { |
|
|
|