Как выполнить объединение JSP во время загрузки с AspectJ 1.6 и Tomcat 6?

Вот моя ситуация:

У меня есть веб-приложение в Eclipse. На данный момент это веб-приложение AspectJ.

У меня есть аспект в моей папке "src" под названием JSPCSRFTokenInjection.aj, в котором есть точки для захвата метода JspWriter.write и некоторых других вещей. Это выглядит так:

package com.aspects; 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import org.apache.log4j.Logger;

import com.thesis.aop.util.StopWatch;

public aspect JSPCSRFTokenInjection{ 
Logger logger; 
StopWatch watch;

private String currentCSRFToken = null;

//Constuctor for the Aspect. I do some init of loggers and
//such here.
public JSPCSRFTokenInjection(){ 
    //PropertyConfigurator.configure("log4j.properties"); 
    logger = Logger.getLogger("csrfMitigationLogger"); 
    logger.info("CSRF Injection Aspect Created"); 
    watch = new StopWatch(); 
} 


//Capturing the CSRF Token from the request by intercepting the 
//_jspService method inside of the JSP
public pointcut csrf_jspServiceIntercept(HttpServletRequest req, 
    HttpServletResponse resp) : 
    call(public void _jspService(HttpServletRequest, HttpServletResponse)) 
    && args(req, resp);


before(HttpServletRequest req, HttpServletResponse resp) : 
    csrf_jspServiceIntercept(req, resp){
    currentCSRFToken = (String) req.getParameter("csrfSalt");
    logger.info("Got CSRF Token from request: " + currentCSRFToken);
}

//Pointcut and advice for capturing the writing into a JSP.
public pointcut csrf_captureFormWriting(String msg, JspWriter writer) :
    call(public void JspWriter.write(String)) 
    && args(msg) 
    && target(writer)
    && if(msg.toLowerCase().contains("</form>"));

before(String msg, JspWriter writer) : csrf_captureFormWriting(msg, writer){
    try{
        logger.info("WRITING TO JSP");
        writer.write("TEST_CSRF");
        writer.write("<input type='hidden' name='csrfSalt' value='" +     currentCSRFToken + "'/>");
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

} 

У меня также есть файл aop.xml в каталоге WebApp/WebContent/META-INF/. Для справки, мой файл web.xml находится в каталоге WebApp/WebContent/WEB-INF/.

aop.xml выглядит так:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj       /dtd/aspectj.dtd">
<aspectj>
<weaver options="-showWeaveInfo -verbose -debug -Xset:weaveJavaPackages=true">
    <!-- Weave types that are within the javax.* or org.aspectj.*
    packages. Also weave all types in the foo package that do
    not have the @NoWeave annotation. -->
    <include within="javax.*"/>
    <include within="com.*"/>
    <include within="org.*"/>
    <include within="org.aspectj.*"/>
</weaver>
<aspects>
    <!-- declare two existing aspects to the weaver -->
    <aspect name="com.aspects.JSPCSRFTokenInjection"/>
    <aspect name="com.aspects.MitigateCSRFAspect"/>
    <!-- Of the set of aspects declared to the weaver
    use aspects matching the type pattern "com..*" for weaving. -->
    <include within="com.*"/>
    <include within="org.*"/>
    <!-- Of the set of aspects declared to the weaver
    do not use any aspects with the @CoolAspect annotation for weaving -->
</aspects>
</aspectj>

Я также добавляю -javaagent:C:/aspectj1.6/lib/aspectjweaver.jar к своим параметрам JVM в Tomcat.

Если это поможет, я использую плагин SysDeo для tomcat. Кроме того, переплетение времени компиляции отлично работает в других частях приложения, однако я не могу переплести ни один из моих аспектов, влияющих на JSP.


person bsimic    schedule 10.08.2012    source источник
comment
Не совсем уверен, но ваша подпись может быть немного неправильной, она должна быть call(public void *._jspService(HttpServletRequest, HttpServletResponse) с подстановочным знаком для представления любого типа.   -  person Biju Kunjummen    schedule 11.08.2012
comment
Это тоже правда. Однако я понял свою настоящую проблему, мой aop.xml был не в том месте!   -  person bsimic    schedule 13.08.2012


Ответы (1)


Я понял проблему. Я помещал свой файл aop.xml в неправильный каталог. Очень глупо с моей стороны.

Предполагается, что он пойдет в

<ProjectRoot>/WebContent/WEB-INF/classes/META-INF/aop-ajc.xml

каталог. Однако я помещал его непосредственно в WEB-INF.

person bsimic    schedule 13.08.2012