Jetty配置JAAS(jetty-maven-plugin)

Jetty配置JAAS(jetty-maven-plugin)

>>首先建立JAAS的项目.

1,建立名为web-test的maven web项目,项目里放入index.jsp,login.jsp和web.xml.

在login.jsp中:

Html代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title>Login Page</title>  
  7. </head>  
  8. <body>  
  9.   <form id="loginForm" method="post" action="j_security_check">  
  10.     <input type="text" name="j_username" id="j_username"/>  
  11.     <input type="password" name="j_password" id="j_password"/>  
  12.     <input type="submit" value="Login"/>  
  13.   </form>  
  14. </body>  
  15. </html>  

index.jsp:

Html代码   收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Main Page</title>  
  8. </head>  
  9. <body>  
  10. Welcome to main page !!!  
  11. </body>  
  12. </html>  

web.xml:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  4.   <display-name>web-test</display-name>  
  5.   
  6.   <security-constraint>  
  7.     <web-resource-collection>  
  8.       <web-resource-name>Web Test Actions</web-resource-name>  
  9.       <url-pattern>/*</url-pattern>  
  10.       <!--<url-pattern>*.jsp</url-pattern>  -->  
  11.     </web-resource-collection>  
  12.     <auth-constraint>  
  13.       <role-name>jvwl</role-name>  
  14.     </auth-constraint>  
  15.   </security-constraint>  
  16.   <login-config>  
  17.     <auth-method>FORM</auth-method>  
  18.     <realm-name>java:/jaas/jvwl-sso</realm-name>  
  19.     <form-login-config>  
  20.       <form-login-page>/login.jsp</form-login-page>  
  21.       <form-error-page>/login.jsp</form-error-page>  
  22.     </form-login-config>  
  23.   </login-config>  
  24.   <security-role>  
  25.     <role-name>jvwl</role-name>  
  26.   </security-role>  
  27.   <welcome-file-list>  
  28.     <welcome-file>index.html</welcome-file>  
  29.     <welcome-file>index.htm</welcome-file>  
  30.     <welcome-file>index.jsp</welcome-file>  
  31.   </welcome-file-list>  
  32. </web-app>  

>>Jetty Maven Plugin配置JAAS方法A:

1,在pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>jvwl</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>jvwl Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>jvwl</finalName>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.10.v20130312</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
              <port>7080</port>
              <maxIdleTime>60000</maxIdleTime>
            </connector>
          </connectors>
          <useProvidedScope>true</useProvidedScope>
          <systemProperties>
            <systemProperty>
              <name>log4j.configuration</name>
              <value>file:src/main/resources/log4j.properties</value>
            </systemProperty>
          </systemProperties>

          <webApp>
            <contextPath>/web</contextPath>
          </webApp>

          <loginServices>
            <loginService implementation="org.eclipse.jetty.security.HashLoginService">
              <name>java:/jaas/jvwl-sso</name>
              <config>src/main/resources/realm.properties</config>
<refreshInterval>10</refreshInterval>
            </loginService>
          </loginServices>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2,在src/main/resources/realm.properties文件里:

#
# This file defines users passwords and roles for a HashUserRealm
#
# The format is
#  <username>: <password>[,<rolename> ...]
#
# Passwords may be clear text, obfuscated or checksummed.  The class 
# org.eclipse.util.Password should be used to generate obfuscated
# passwords or password checksums
#
# If DIGEST Authentication is used, the password must be in a recoverable
# format, either plain text or OBF:.
#
jetty: MD5:164c88b302622e17050af52c89945d44,user
admin: CRYPT:adpexzg3FUZAk,server-administrator,content-administrator,admin
other: OBF:1xmk1w261u9r1w1c1xmq,user
plain: plain,user
user: password,user
jerval: 111111,jvwl

# This entry is for digest auth.  The credential is a MD5 hash of username:realmname:password
digest: MD5:6e120743ad67abfbc385bc2bb754e297,user

3,使用jetty:run启动maven jetty,然后通过jerval/111111登录,一切OK.

 

>>Jetty Maven Plugin配置JAAS方法B:

1,修改pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>jvwl</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>jvwl Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>jvwl</finalName>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.10.v20130312</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
              <port>7080</port>
              <maxIdleTime>60000</maxIdleTime>
            </connector>
          </connectors>
          <useProvidedScope>true</useProvidedScope>
          <systemProperties>
            <systemProperty>
              <name>log4j.configuration</name>
              <value>file:src/main/resources/log4j.properties</value>
            </systemProperty>
            <systemProperty>
              <name>java.security.auth.login.config</name>
              <value>src/main/resources/realm.conf</value>
            </systemProperty>
          </systemProperties>
          <webApp>
            <contextPath>/web</contextPath>
          </webApp>
          <loginServices>
            <loginService implementation="org.eclipse.jetty.plus.jaas.JAASLoginService">
              <name>java:/jaas/jvwl-sso</name>
              <loginModuleName>jvwl-sso</loginModuleName>
            </loginService>
          </loginServices>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2,添加文件src/main/resources/realm.conf.

realm.conf:

jvwl-sso {
    org.eclipse.jetty.plus.jaas.spi.PropertyFileLoginModule required
    debug="true"
    file="src/main/resources/realm.properties";
};

3,使用jetty:run启动maven jetty,然后通过jerval/111111登录,一切OK.

 

>>Jetty Maven Plugin配置JAAS方法C(将JAAS信息放入jetty.xml中):

1,修改pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>jvwl</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>jvwl Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>jvwl</finalName>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.10.v20130312</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <jettyXml>src/main/resources/realm.xml</jettyXml>
          <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
              <port>7080</port>
              <maxIdleTime>60000</maxIdleTime>
            </connector>
          </connectors>
          <useProvidedScope>true</useProvidedScope>
          <systemProperties>
            <systemProperty>
              <name>log4j.configuration</name>
              <value>file:src/main/resources/log4j.properties</value>
            </systemProperty>
            <systemProperty>
              <name>java.security.auth.login.config</name>
              <value>src/main/resources/realm.conf</value>
            </systemProperty>
          </systemProperties>

          <webApp>
            <contextPath>/web</contextPath>
          </webApp>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2,添加文件src/main/resources/realm.xml.

realm.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call name="addBean">
    <Arg>
      <New class="org.eclipse.jetty.plus.jaas.JAASLoginService">
        <Set name="name">java:/jaas/jvwl-sso</Set>
        <Set name="loginModuleName">jvwl-sso</Set>
      </New>
    </Arg>
  </Call>
</Configure>

注:感觉这种写法需要使用自定义的jetty.xml,同时这个文件具有覆盖性,也就是说真正使用中还得把jetty.xml里的其它配置信息都要放里来.所以不推荐这种使用方案.当然,如果你正想要这样的功能,那就另说.

3,使用jetty:run启动maven jetty,然后通过jerval/111111登录,一切OK.

 

猜你喜欢

转载自jerval.iteye.com/blog/2090805