|
|
@ -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,47 +82,33 @@ public class CustomAccessControlFilter extends AccessControlFilter { |
|
|
|
// 指定处理该请求的处理器
|
|
|
|
// 指定处理该请求的处理器
|
|
|
|
request.getRequestDispatcher(Constant.ERROR_CONTROLLER_PATH).forward(request, servletResponse); |
|
|
|
request.getRequestDispatcher(Constant.ERROR_CONTROLLER_PATH).forward(request, servletResponse); |
|
|
|
} |
|
|
|
} |
|
|
|
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(token, token); |
|
|
|
// 校验并解析token,如果token过期或者篡改,则会返回null
|
|
|
|
getSubject(servletRequest, servletResponse).login(usernamePasswordToken); |
|
|
|
Claims claims = checkJWT(token); |
|
|
|
/*} catch (BusinessException exception) { |
|
|
|
if (null == claims) { |
|
|
|
if (HttpContextUtils.isAjaxRequest(request)) { |
|
|
|
request.setAttribute("filterError", new BusinessException(BaseResponseCode.TOKEN_ERROR)); |
|
|
|
customResponse(exception.getBaseResponseCode().getCode(), exception.getMessage(), servletResponse); |
|
|
|
// 指定处理该请求的处理器
|
|
|
|
} else if (exception.getBaseResponseCode().getCode() == BaseResponseCode.TOKEN_ERROR.getCode()) { |
|
|
|
request.getRequestDispatcher(Constant.ERROR_CONTROLLER_PATH).forward(request, servletResponse); |
|
|
|
servletRequest.getRequestDispatcher("/index/login").forward(servletRequest, servletResponse); |
|
|
|
}else { |
|
|
|
} else if (exception.getBaseResponseCode().getCode() == BaseResponseCode.UNAUTHORIZED_ERROR.getCode()) { |
|
|
|
UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(token, token); |
|
|
|
servletRequest.getRequestDispatcher("/index/403").forward(servletRequest, servletResponse); |
|
|
|
getSubject(servletRequest, servletResponse).login(usernamePasswordToken); |
|
|
|
} else { |
|
|
|
|
|
|
|
servletRequest.getRequestDispatcher("/index/500").forward(servletRequest, servletResponse); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} 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; |
|
|
|
|
|
|
|
} catch (Exception 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/500").forward(servletRequest, servletResponse); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
}*/ |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 校验token |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Claims checkJWT(String token) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
final Claims claims = Jwts.parser().setSigningKey(APP_SECRET). |
|
|
|
|
|
|
|
parseClaimsJws(token).getBody(); |
|
|
|
|
|
|
|
return claims; |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void customResponse(int code, String msg, ServletResponse response) { |
|
|
|
private void customResponse(int code, String msg, ServletResponse response) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
R result = R.getResult(code, msg); |
|
|
|
R result = R.getResult(code, msg); |
|
|
|