这有个 jaas tomcat的实例, 先看下吧: http://zhidao.baidu.com/question/31047013.html 准备文件和目录 创建文件login.jsp和error.jsp login.jsp的内容 <html> <head> <meta HTTP-EQUIV="Content-Type" Content="text-html; charset=gbk"> <title>login</title> </head> <body> <form method="POST" action="j_security_check"> 姓名:<input type="text" name="j_username"/> <br/> 密码:<input type="password" name="j_password"/> <br/> <input type="submit" value="提交"/> </form> </body> </html> 在根目录下创建目录web,在新建的目录下创建一个文件index.jsp,内容如下 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Apache-Axis</title> </head> <body bgcolor="#FFFFFF"> request.FORM_AUTH:<%=request.FORM_AUTH%><br/> request.getRemoteUser():<%=request.getRemoteUser()%><br/> </body> </html> 设置配置文件 在web.xml中添加以下内容 <web-app> ... <security-constraint> <!-- Sample Security Constraint --> <web-resource-collection> <!-- We're going to protect this resource and make it available only to users in "role1". --> <web-resource-name>protected-resources</web-resource-name> <url-pattern>/web/*</url-pattern> <http-method>HEAD</http-method> <http-method>GET</http-method> <http-method>POST</http-method> <http-method>PUT</http-method> <http-method>DELETE</http-method> </web-resource-collection> <!-- NOTE: This role names will be retrieved by Josso using the propper identity store. --> <auth-constraint> <role-name>role1</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/error.jsp</form-error-page> </form-login-config> </login-config> <security-role > <description>Role 1</description> <role-name>role1</role-name> </security-role> </web-app> 测试 打开tomcat目录下的conf/tomcat-users.xml文件,内容如下 <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="role1" password="tomcat" roles="role1"/> <user username="both" password="tomcat" roles="tomcat,role1"/> </tomcat-users> 启动tomcat,在浏览器中输入地址http://localhost:8080/hello/web/,显示的内容不是/web/index.html,而是login.jsp的内容,输入both或者role1的用户名和密码,将会看到web/index.html的内容,当然,如果输入错误,则会提示错误信息。验证通过后,我们可以看到如下内容: request.FORM_AUTH:FORM request.getRemoteUser():both? //用户名 sun公司也有提供了一个关于JAAS的实例,可供实际分析JAAS使用原理,本人在网上找了许久这方面的文章,希望对你有帮助。 1、首先下载实例代码 http://java./j2se/1.4.2/docs/guide/security/jaas/tutorials/sample_jaas.config http://java./j2se/1.4.2/docs/guide/security/jaas/tutorials/SampleAcn.java http://java./j2se/1.4.2/docs/guide/security/jaas/tutorials/SampleLoginModule.java http://java./j2se/1.4.2/docs/guide/security/jaas/tutorials/SamplePrincipal.java http://java./j2se/1.4.2/docs/guide/security/jaas/tutorials/sampleacn.policy 2、SampleAcn.java 放在 sample 目录中, SampleLoginModule.java 放在 sample/module 之下, 而 SamplePricipal 放在 sample/principal 之下。 3、将 config 和 policy 配置文件放到项目的根目录中,切记,否则无法找到此文件。 4、将所有文件编译后执行以下命令 java -Djava.security.auth.login.config==sample_jaas.config sample.SampleAcn 如果使用Eclipse则在Run... SampleAcn.java 类时把-Djava.security.auth.login.config==sample_jaas.config 参数填写入Arguments标签页面的VM arguments框中。 5、此时在控制台显示要求用户输入user name:和password:,分别输入testUser和testPassword即可验证通过。 至于Jaas原理,有时间可以研究一下源代码即可。 |
|
来自: whiskey1122 > 《JAAS》