搜鞥都哦 发表于 2017-2-25 13:19:14

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代码  





[*]<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
[*]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
[*]<html>  
[*]<head>  
[*]<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
[*]<title>Login Page</title>  
[*]</head>  
[*]<body>  
[*]  <form id="loginForm" method="post" action="j_security_check">  
[*]    <input type="text" name="j_username" id="j_username"/>  
[*]    <input type="password" name="j_password" id="j_password"/>  
[*]    <input type="submit" value="Login"/>  
[*]  </form>  
[*]</body>  
[*]</html>  



index.jsp:

Html代码  





[*]<%@ page language="java" contentType="text/html; charset=UTF-8"  
[*]    pageEncoding="UTF-8"%>  
[*]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
[*]<html>  
[*]<head>  
[*]<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
[*]<title>Main Page</title>  
[*]</head>  
[*]<body>  
[*]Welcome to main page !!!  
[*]</body>  
[*]</html>  



web.xml:

Xml代码  





[*]<?xml version="1.0" encoding="UTF-8"?>  
[*]<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"  
[*]  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">  
[*]  <display-name>web-test</display-name>  
[*]  
[*]  <security-constraint>  
[*]    <web-resource-collection>  
[*]      <web-resource-name>Web Test Actions</web-resource-name>  
[*]      <url-pattern>/*</url-pattern>  
[*]      <!--<url-pattern>*.jsp</url-pattern>  -->  
[*]    </web-resource-collection>  
[*]    <auth-constraint>  
[*]      <role-name>jvwl</role-name>  
[*]    </auth-constraint>  
[*]  </security-constraint>  
[*]  <login-config>  
[*]    <auth-method>FORM</auth-method>  
[*]    <realm-name>java:/jaas/jvwl-sso</realm-name>  
[*]    <form-login-config>  
[*]      <form-login-page>/login.jsp</form-login-page>  
[*]      <form-error-page>/login.jsp</form-error-page>  
[*]    </form-login-config>  
[*]  </login-config>  
[*]  <security-role>  
[*]    <role-name>jvwl</role-name>  
[*]  </security-role>  
[*]  <welcome-file-list>  
[*]    <welcome-file>index.html</welcome-file>  
[*]    <welcome-file>index.htm</welcome-file>  
[*]    <welcome-file>index.jsp</welcome-file>  
[*]  </welcome-file-list>  
[*]</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.
 
页: [1]
查看完整版本: Jetty配置JAAS(jetty-maven-plugin)