无法使用jdbc realm和Glassfish设置基本身份验证

时间:2011-10-07 15:48:47

标签: glassfish jax-rs basic-authentication jdbcrealm

我正在使用Glassfish v3和一个非常简单的JAX-RS服务(在http://www.vogella.de/articles/REST/article.html采取

我想要实现的是为所有资源设置基本的http身份验证。如果希望在用户请求http://localhost:8080/de.vogella.jersey.first/rest/hello时,浏览器会弹出提示输入用户名+密码的提示。

我决定去JDBCRealm身份验证。我使用PostgreSQL 8.4和正确的JDBC驱动程序。

我的问题是身份验证似乎没有活动(HTTP标头非常简单:

Request URL:http://localhost:8080/de.vogella.jersey.first/rest/hello

Request Method:GET
Status Code:200 OK
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Cookie:JSESSIONID=ec48541dc7654806fcf022d79e66; treeForm:tree-hi=treeForm:tree:configuration:loggerSetting
Host:localhost:8080
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.187 Safari/535.1

Response Headers
Content-Type:text/html
Date:Fri, 07 Oct 2011 14:26:26 GMT
Server:GlassFish Server Open Source Edition 3.0.1
Transfer-Encoding:chunked
X-Powered-By:Servlet/3.0

验证已设置但似乎无效。那时,我需要你的技能来分析我做错了什么。

到目前为止我做了什么:

1 /部署了JAX-RS服务(这个工作正常,请参阅上一个URL)  2 /创建了一个数据库,创建了两个表:

CREATE TABLE groups
(
  username character varying(16) NOT NULL,
  id character varying(20) NOT NULL,
  CONSTRAINT groups_pkey PRIMARY KEY (username, id),
  CONSTRAINT fk_username FOREIGN KEY (username)
      REFERENCES users (username) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
);

CREATE TABLE users
(
  username character varying(16) NOT NULL,
  "password" character varying(32) NOT NULL,
  CONSTRAINT users_pkey PRIMARY KEY (username)
);

并填写如下:

表“群组” - > “WEBUSER”; “雇员”

表“用户” - > “WEBUSER”; “WEBUSER”

3 /在Glassfish管理GUI中,我添加了一个名为“postgres”的连接池,如下所示: http://puu.sh/6DZV 其他属性如下所示: http://puu.sh/6E05

4 /我随后创建了一个名为“postgres”的JDVC资源,如下所示: http://puu.sh/6E0H

5 /在Glassfish管理GUI中,我添加了一个名为“postgres”的安全域,如下所示: http://puu.sh/6E1b

6 /我还在“Logger Settings”中将javax.enterprise.system.core.security的日志级别设置为“FINEST”。 从浏览器请求Web服务后,我在server.log文件中获得以下内容:

[#|2011-10-07T16:26:26.410+0200|FINE|glassfish3.0.1|javax.enterprise.system.core.security|_ThreadID=67;_ThreadName=Thread-1;ClassName=com.sun.enterprise.security.web.integration.WebSecurityManager;MethodName=setPolicyContext;|[Web-Security] Policy Context ID was: de.vogella.jersey.first/de_vogella_jersey_first|#]

[#|2011-10-07T16:26:26.412+0200|FINE|glassfish3.0.1|javax.enterprise.system.core.security.com.sun.enterprise.security|_ThreadID=67;_ThreadName=Thread-1;ClassName=com.sun.enterprise.security.SecurityContext;MethodName=setCurrentSecurityContext;|SecurityContext: setCurrentSecurityContext method called|#]

7 /关于代码,我没有添加任何注释,但我更新了我的web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>de.vogella.jersey.first</display-name>
  <servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>de.vogella.jersey.first</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>customer creation</web-resource-name>
      <url-pattern>/rest/*</url-pattern>
       <http-method>GET</http-method>
       <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
      <role-name>webusers</role-name>
    </auth-constraint>
  </security-constraint>

  <login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>postgres</realm-name>
  </login-config>

  <security-role>
    <role-name>webusers</role-name>
  </security-role>

</web-app>

sun-web.xml如下:

<?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="">
  <context-root>/de.vogella.jersey.first</context-root>
  <security-role-mapping>
    <role-name>webusers</role-name>
    <group-name>webusers</group-name>
  </security-role-mapping>
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class java code.</description>
    </property>
  </jsp-config>
</sun-web-app>

这有什么问题?我忘了什么吗?

感谢您的时间!

1 个答案:

答案 0 :(得分:0)

我发现我必须禁用“在部署之间保持会话”。通过双击部署服务器并取消选中该框,可以在服务器中找到此选项。