我正在实现身份验证过滤器,我也将它用于异常处理。
由于CQ5中没有web.xml,因此我们无法放置<exception-type>
等映射。
我的所有控制器(Servlet)都将Exception抛回给调用者。我有一个在我的每个Servlet之前调用的过滤器。所以我明确地抛出
new SerlvetException()
在我的一个servlet中。这是向呼叫者抛出的,即我的主要过滤器。在chain.doFilter()
正确发生异常,我在这里处理异常(在过滤器中)。但它只是没有处理为什么?
另外,这个Scope属性是什么SlingFilter我不明白SlingFilter SCOPE的这个REQUEST,ERROR,COMPONENT,INCLUDE属性
这是我的代码和堆栈跟踪。
public class AddNewUserController extends SlingAllMethodsServlet{
/**
* @author Oliver Kaunds
*/
private static final long serialVersionUID = 1L;
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Reference
protected AddNewUserService addNewUserService;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
log.info("VideoJet :: Do GET Called ");
String path =request.getRequestURI();
log.info("VIDEOJET PATH :: "+ path);
try{
if(path.equals("/services/videojet/v1/AddNewUserController/view"))
{
List<HRRepresentative> list =addNewUserService.getHRRepresentative();
request.setAttribute("hrList",list );
HttpSession session = request.getSession();
String userOperation =(String)session.getAttribute("userOp");
request.setAttribute("userOp", userOperation);
session.removeAttribute("userOp");
throw new Exception("Error Page Test"); // Manuly throw exception
}
我的主过滤器
package com.videojet.hiresite.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingFilter(order=1)
@Properties({
@Property(name="service.pid", value="com.videojet.hiresite.filters.HireSiteFilter",propertyPrivate=false),
@Property(name="service.description",value="Main Authentication Filter", propertyPrivate=false),
@Property(name="service.vendor",value="Zensar Tech", propertyPrivate=false),
@Property(name="pattern",value="/services/videojet/v1/.*", propertyPrivate=false)
})
public class HireSiteFilter implements Filter {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Authentication Filter for the whole application
log.info("HireSiteFilter Invoked");
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
String path =httpServletRequest.getRequestURI();
log.info("Request URI on AJAX ::"+path);
try{
if(path.contains("LoginController"))
{
log.info("HireSiteFilter :: Request to LoginController");
chain.doFilter(request, response);
}
else
{
HttpSession session = httpServletRequest.getSession(false);
if(session ==null || session.getAttribute("userId")==null)
{
log.info("HireSiteFilter :: Not Logged in");
HttpServletResponse httpResponse = (HttpServletResponse)response;
httpResponse.sendRedirect("/content/videojet/en/loginPage.html");
}
else
{
log.info("HireSiteFilter :: Logged in");
chain.doFilter(request, response); // This is within Try block---!!!!!!!!!!
}
}
}
catch(Exception exception)
{
// Internal Server Error Manul dispatch to Error Page
// /content/videojet/en/errorPage.html
log.info("********************* TEST ***********************************");
HttpServletResponse httpServletResponse =(HttpServletResponse)response;
httpServletResponse.sendRedirect("/content/videojet/en/errorPage.html");
}
}
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
}
}
异常发生正确,但不会被catch块捕获。堆栈跟踪在下面。
04.07.2014 11:46:26.395 *INFO* [0:0:0:0:0:0:0:1 [1404454586395] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter HireSiteFilter Invoked
04.07.2014 11:46:26.395 *INFO* [0:0:0:0:0:0:0:1 [1404454586395] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter Request URI on AJAX ::/services/videojet/v1/LoginController/validateUser
04.07.2014 11:46:26.395 *INFO* [0:0:0:0:0:0:0:1 [1404454586395] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter HireSiteFilter :: Request to LoginController
04.07.2014 11:46:26.399 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController Do Post **************
04.07.2014 11:46:26.399 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController The Username :::::foster
04.07.2014 11:46:26.399 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController The Password ::::::test
04.07.2014 11:46:26.399 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.daos.EmployeeDaoImp JCR QUERY 1::SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('/content/videojetdata/HR_EMPLOYEE') AND NAME(s) = 'foster'
04.07.2014 11:46:26.864 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.daos.EmployeeDaoImp Query Check :: Presenttest
04.07.2014 11:46:26.866 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.services.login.LoginServiceImp The Passwrod from the daatabase+++++++test**********
04.07.2014 11:46:26.866 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController Username::foster
04.07.2014 11:46:26.866 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.controllers.LoginController Password::test
04.07.2014 11:46:26.869 *INFO* [0:0:0:0:0:0:0:1 [1404454586398] POST /services/videojet/v1/LoginController/validateUser HTTP/1.1] com.videojet.hiresite.daos.EmployeeDaoImp JCR Query::SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('/content/videojetdata/HR_EMPLOYEE') AND NAME(s) = 'foster'
04.07.2014 11:46:26.943 *INFO* [0:0:0:0:0:0:0:1 [1404454586943] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter HireSiteFilter Invoked
04.07.2014 11:46:26.943 *INFO* [0:0:0:0:0:0:0:1 [1404454586943] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter Request URI on AJAX ::/services/videojet/v1/AddNewUserController/view
04.07.2014 11:46:26.943 *INFO* [0:0:0:0:0:0:0:1 [1404454586943] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.filters.HireSiteFilter HireSiteFilter :: Logged in
04.07.2014 11:46:26.945 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.controllers.AddNewUserController VideoJet :: Do GET Called
04.07.2014 11:46:26.945 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.controllers.AddNewUserController VIDEOJET PATH :: /services/videojet/v1/AddNewUserController/view
04.07.2014 11:46:26.945 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.daos.HRRepresentativeDaoImp THE JCR QUERY ::SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE('/content/videojetdata/HR_REP')
04.07.2014 11:46:27.740 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] org.apache.jackrabbit.core.query.lucene.DocNumberCache size=715/1024, #accesses=1001, #hits=256, #misses=745, cacheRatio=26%
04.07.2014 11:46:29.642 *INFO* [pool-6-thread-24-com/day/cq/replication/job/publish(com/day/cq/replication/job/publish)] com.day.cq.replication.impl.AgentManagerImpl Processing job for agent publish
04.07.2014 11:46:29.643 *INFO* [pool-6-thread-24-com/day/cq/replication/job/publish(com/day/cq/replication/job/publish)] com.day.cq.replication.Agent.publish Sending POST request to http://localhost:4503/bin/receive?sling:authRequestLogin=1
04.07.2014 11:46:29.893 *INFO* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] org.apache.jackrabbit.core.query.lucene.DocNumberCache size=121/1024, #accesses=1001, #hits=520, #misses=481, cacheRatio=52%
04.07.2014 11:46:30.604 *ERROR* [0:0:0:0:0:0:0:1 [1404454586944] GET /services/videojet/v1/AddNewUserController/view HTTP/1.1] com.videojet.hiresite.controllers.AddNewUserController VIDEJET EXCEPTION:: java.lang.Exception: Error Page Test
at com.videojet.hiresite.controllers.AddNewUserController.doGet(com.videojet.hiresite.controllers.AddNewUserController.java:75)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:268)
at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:139)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:500)
at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:147)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:308)
at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:141)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:257)
at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
at com.day.cq.wcm.core.impl.warp.TimeWarpFilter.doFilter(TimeWarpFilter.java:106)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter.doFilter(RedirectFilter.java:296)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter.doFilter(RequestProgressTrackerLogFilter.java:59)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet.doFilter(FormsHandlingServlet.java:220)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.theme.impl.ThemeResolverFilter.doFilter(ThemeResolverFilter.java:76)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:117)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at com.day.cq.wcm.core.impl.WCMRequestFilter.doFilter(WCMRequestFilter.java:89)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.rewriter.impl.RewriterFilter.doFilter(RewriterFilter.java:83)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.bgservlets.impl.BackgroundServletStarterFilter.doFilter(BackgroundServletStarterFilter.java:135)
at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processRequest(SlingRequestProcessorImpl.java:153)
at org.apache.sling.engine.impl.SlingMainServlet.service(SlingMainServlet.java:206)
at org.apache.felix.http.base.internal.handler.ServletHandler.doHandle(ServletHandler.java:96)
at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:79)
at org.apache.felix.http.base.internal.dispatch.ServletPipeline.handle(ServletPipeline.java:42)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:49)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:117)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.security.impl.ReferrerFilter.doFilter(ReferrerFilter.java:238)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at com.adobe.granite.license.impl.LicenseCheckFilter.doFilter(LicenseCheckFilter.java:179)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at com.videojet.hiresite.filters.HireSiteFilter.doFilter(com.videojet.hiresite.filters.HireSiteFilter.java:65)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:78)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.sling.engine.impl.log.RequestLoggerFilter.doFilter(RequestLoggerFilter.java:75)
at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
at org.apache.felix.http.base.internal.dispatch.FilterPipeline.dispatch(FilterPipeline.java:48)
at org.apache.felix.http.base.internal.dispatch.Dispatcher.dispatch(Dispatcher.java:39)
at org.apache.felix.http.base.internal.DispatcherServlet.service(DispatcherServlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at com.day.j2ee.servletengine.ServletRuntimeEnvironment.service(ServletRuntimeEnvironment.java:250)
at com.day.j2ee.servletengine.RequestDispatcherImpl.doFilter(RequestDispatcherImpl.java:315)
at com.day.j2ee.servletengine.RequestDispatcherImpl.service(RequestDispatcherImpl.java:334)
at com.day.j2ee.servletengine.RequestDispatcherImpl.service(RequestDispatcherImpl.java:377)
at com.day.j2ee.servletengine.ServletHandlerImpl.process(ServletHandlerImpl.java:351)
at com.day.j2ee.servletengine.HttpListener$Worker.run(HttpListener.java:625)
at java.lang.Thread.run(Unknown Source
答案 0 :(得分:1)
答案 1 :(得分:1)
尝试使用范围作为 @SlingFilter(scope = SlingFilterScope.COMPONENT,order = -10000,metatype = 假) 我认为在你的情况下它应该是SlingFilterScope.ERROR
sling.filter.scope - 表示过滤器应该具有的过滤器链 成为其中的一部分。需要!如果没有,吊索将不会拿起过滤器 集。
另外,在第一个位置检查过滤条件,如下所示
public void doFilter(ServletRequest pRequest, ServletResponse pResponse,
FilterChain pChain) throws IOException, ServletException {
if (!(pRequest instanceof SlingHttpServletRequest)) {
throw new ServletException("Request is not a Sling HTTP request.");
}
if (isFilterEnabled(slingRequest)) {
// Implement Filter-Logic
} else {
// continue with filter chaining
pChain.doFilter(pRequest, pResponse);
}
}
答案 2 :(得分:1)
什么servlet过滤器可用于? 1.基于用户身份的身份验证阻止请求 2.日志记录和审计 - 跟踪Web应用程序的使用 3.图像转换 - 缩放地图等 4.数据压缩 - 使下载量更小 5.本地化 - 在特定区域设置中定位请求和响应 6. XML内容的XSL / T转换
我不确定,但我认为它不支持异常处理。做一次检查。