4
回答
Tomcat 7 下对 JSP 进行预编译
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

问题:

有一个web应用,包含很多jsp,直接调用某些后台库的方法,但这些库通过 Java 的安全管理器运行在一个沙盒环境中,必须使用某个密钥签名后才能进行调用。

解决方案:

因此初步考虑使用以下步骤来解决这个问题:
1. 预编译 JSP 文件
2. 打包这些预编译后的 JSP 文件到一个 jar 包中
3. 使用给定的密钥对这个jar包进行签名
4. 将签名后的jar包放在 WEB-INF/lib 目录下
5. 移除 jsp 文件

详细处理:

我们需要使用 jspc-maven-plugin 这个 maven 的插件来对 jsp 进行预编译。

下面是 pom.xml 源码:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <groupid>org.wso2.appserver</groupid>
        <artifactid>wso2appserver-samples-parent</artifactid>
        <version>4.1.1</version>
        <relativepath>../../pom.xml</relativepath>
    </parent>

    <modelversion>4.0.0</modelversion>
    
    <artifactid>example</artifactid>
    <packaging>war</packaging>
    <name>WSO2 AS - Example webapp</name>

    <dependencies>
        <dependency>
            <groupid>org.wso2.carbon</groupid>
            <artifactid>org.wso2.carbon.tomcat</artifactid>
            <version>${carbon.platform.version}</version>
        </dependency>
        <dependency>
            <groupid>org.apache.axis2.wso2</groupid>
            <artifactid>axis2-client</artifactid>
            <version>${axis2.osgi.version}</version>
        </dependency>
        <dependency>
            <groupid>org.wso2.carbon</groupid>
            <artifactid>org.wso2.carbon.authenticator.proxy</artifactid>
            <version>${carbon.platform.version}</version>
        </dependency>
        <dependency>
            <groupid>org.wso2.carbon</groupid>
            <artifactid>org.wso2.carbon.authenticator.stub</artifactid>
            <version>${carbon.platform.version}</version>
        </dependency>
        <dependency>
            <groupid>org.wso2.carbon</groupid>
            <artifactid>org.wso2.carbon.core.common</artifactid>
            <version>${carbon.platform.version}</version>
        </dependency>
        <dependency>
            <groupid>org.wso2.carbon</groupid>
            <artifactid>org.wso2.carbon.core</artifactid>
        </dependency>

        <dependency>
            <groupid>org.apache.axis2.wso2</groupid>
            <artifactid>axis2</artifactid>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupid>org.codehaus.mojo</groupid>
                <artifactid>build-helper-maven-plugin</artifactid>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-code/src
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupid>org.codehaus.mojo.jspc</groupid>
                <artifactid>jspc-maven-plugin</artifactid>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <inputwebxml>${pom.basedir}/src/main/resources/WEB-INF/web.xml</inputwebxml>
                    <source>1.5
                    <target>1.5</target>
                    <sources>
                        <directory>${pom.basedir}/src/main/resources</directory>
                        <includes>
                            <include>**/*.jsp</include>
                        </includes>
                    </sources>
                </configuration>
                
                
                <dependencies>
                    <dependency>
                        <groupid>org.codehaus.mojo.jspc</groupid>
                        <artifactid>jspc-compiler-tomcat6</artifactid>
                        <version>2.0-alpha-3</version>
                        
                        <exclusions>
                            <exclusion>
                                <groupid>org.apache.tomcat</groupid>
                                <artifactid>jasper</artifactid>
                            </exclusion>
                            <exclusion>
                                <groupid>org.apache.tomcat</groupid>
                                <artifactid>jasper-el</artifactid>
                            </exclusion>
                            <exclusion>
                                <groupid>org.apache.tomcat</groupid>
                                <artifactid>jasper-jdt</artifactid>
                            </exclusion>
                            <exclusion>
                                <groupid>org.apache.tomcat</groupid>
                                <artifactid>servlet-api</artifactid>
                            </exclusion>
                            <exclusion>
                                <groupid>org.apache.tomcat</groupid>
                                <artifactid>jsp-api</artifactid>
                            </exclusion>
                            <exclusion>
                                <groupid>org.apache.tomcat</groupid>
                                <artifactid>el-api</artifactid>
                            </exclusion>
                            <exclusion>
                                <groupid>org.apache.tomcat</groupid>
                                <artifactid>annotations-api</artifactid>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    
                    <dependency>
                        <groupid>org.apache.tomcat</groupid>
                        <artifactid>tomcat-jasper</artifactid>
                        <version>7.0.12</version>
                    </dependency>
                    
                    <dependency>
                        <groupid>org.eclipse.jdt.core.compiler</groupid>
                        <artifactid>ecj</artifactid>
                        <version>3.5.1</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-compiler-plugin</artifactid>
                <configuration>
                    <source>1.5
                    <target>1.5</target>
                </configuration>
            </plugin>
            
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-war-plugin</artifactid>
                <version>2.1-beta-1</version>
                <configuration>
                    <warname>example</warname>
                    <packagingincludes>
                        WEB-INF/classes/**,
                        WEB-INF/*,
                        WEB-INF/jsp/*,
                        WEB-INF/jsp2/*,
                        WEB-INF/lib/jstl.jar,
                        WEB-INF/lib/standard.jar,
                        WEB-INF/lib/jsp.jar,
                        **/axis2-client*.jar,
                        **/org.wso2.carbon.authenticator.proxy*.jar,
                        **/org.wso2.carbon.authenticator.stub*.jar,
                        **/org.wso2.carbon.core.common*.jar,
                        **/*.java,
                        **/tags/**,
                        **/servlets/**,
                        **/carbon/**,
                        **/*.class,
                        **/*.html,
                        jsp/images/*
                    </packagingincludes>
                    <webresources>
                        <resource>
                            
                            <directory>src/main/resources</directory>
                        </resource>
                    </webresources>
                    <webxml>${pom.basedir}/target/jspweb.xml</webxml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

下面是关于该配置文件中的一些说明,请注意行号(没翻译,大家硬着头皮看看吧):

1. line 73 引入 jspc-maven-plugin 到 pom.xml
This will compile jsp files to servlets and then into .class files which include the byte code. These can be found under "target/jsp-source" once the maven build succeeds.

2. Note the 'Configuration' element from line 83 to 93:

    - inputwebxml: specify where the original web.xml file of the webapp resides. jspc-maven-plugin will detect that and update the servlet mappings according to the compiled jsp files and create a new file called "jspweb.xml" in the target folder, which you need to package with the webapp instead of the original web.xml.
    - sources: specify where the jsp files resides in your webapp .

3. Then comes the trick: there is no jspc-maven-compiler plugin for tomcat 7 yet. So we need to use tomcat 6 version of it by removing the incompatibilities. This is what is done from line 97 to line 147.

4. As in line 190, you need to specify the new web.xml created by this plugin, to be included in the .war file of the webapp.

5. Now the step 1 mentioned in the above 'solution' section is achieved and the files obtained by pre-compiling jsp files are available in 'target/jsp-source' folder.

Now, in order to complete steps from 2-4 in the above 'solution', you may need to write a ant build.xml file or can integrate those steps into the pom.xml file itself. I did it through a ant build.xml file.

You can use jarsigner tool that comes with JDK installation  to sign the jar file containing the compiled jsps, as described here.

Once you include the compiled jsps in a jar file in the webapp, you need to remove the original jsp files from the webapp or avoid packaging them in the .war file because if they are included, those will be compiled and used by the servlet container instead of the already pre-compiled ones.

In my case, I had to remove the jsp files packaged inside web-inf/classes/carbon
and web-inf/classes/jsp/carbon of the webapp.

Hope this helps...
举报
红薯
发帖于6年前 4回/2K+阅
顶部