我试过这个配置,但它对我不起作用。 Basic Authentication in Glassfish 我也试过这个指南 http://maksim.sorokin.dk/it/2010/10/13/basic-authentication-in-glassfish-3/但我也无法通过它询问用户密码。
这些是我采取的步骤:
1. 以管理员身份登录管理界面。
2. 转到安全->领域->文件
3. 在Assign Groups字段添加组名(Users)。
4. 打开页面顶部的管理用户。
5. 点击新建,添加一个用户(testuser)并输入密码。
6. 将(用户)添加到组列表。
7. 把这行放到 web.xml
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure Application</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>User</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>file</realm-name>
</login-config>
<security-role>
<role-name>User</role-name>
</security-role>
8.并将此行放入 sun-web.xml
<sun-web-app error-url="">
<security-role-mapping>
<role-name>User</role-name>
<group-name>Users</group-name>
</security-role-mapping>
</sun-web-app>
9.毕竟我启用了 Configurations->server-config->Security->Security Manager
我的配置是 Glassfish 3.1、sun java6 jdk、Debian lenny 和一个用于测试的简单“Hello World”页面。
这里缺少什么?
更新:
我发现它需要 xml header 。添加它们后,它开始工作。我的最终配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure Application</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>Users</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>file</realm-name>
</login-config>
<security-role>
<role-name>Users</role-name>
</security-role>
</web-app>
和
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
<security-role-mapping>
<role-name>Users</role-name>
<group-name>Users</group-name>
</security-role-mapping>
</sun-web-app>
最佳答案
您可以试试这个指南:http://download.oracle.com/docs/cd/E19798-01/821-1750/beaxw/index.html我听说 web.xml 有时不能正常工作。我有同样的问题,但现在无法测试。
关于java - 如何使用 Glassfish 实现基本身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5912577/