我使用JEE7标准API创建了一个WebSocket。我有一个用@ServerEndPoint注释的类和三个用@ OnOpen,@ OnClose和OnMessage注释的方法。我在Glassfish v4中部署应用程序并且它可以工作,但是当我从某个Session实例调用getUserPrincipal()方法时,它返回null。这是我要测试它的代码:
@ServerEndpoint(value = "/chat")
public class ChatServerEndpoint {
private static final Logger LOG = LoggerFactory.getLogger(ChatServerEndpoint.class);
@OnOpen
public void onOpen(Session session) {
LOG.debug("Opening a session with id " + session.getId() + " for " + session.getUserPrincipal());
}
@OnClose
public void onClose(Session session) {
LOG.debug("Closing the session " + session.getId() + " associated with " + session.getUserPrincipal());
}
@OnMessage
public void onMessage(Session session, String msg) {
LOG.debug("There are " + session.getOpenSessions().size() + " opened sessions");
for (Session sess : session.getOpenSessions()) {
LOG.debug("Session " + sess.getId() + " associated with " + sess.getUserPrincipal());
try {
sess.getBasicRemote().sendText(msg);
} catch (IOException e) {
LOG.error("Error sending message", e);
}
}
}
}
此代码尽可能简单地缩小问题范围。这是日志输出:
22:22:48.638 [http-listener-1(1)] DEBUG e.p.server.rs.ChatServerEndpoint - Opening a session with id 437a9b4d-c694-4a19-b912-47b88da4eb9a for null
22:22:54.350 [http-listener-1(5)] DEBUG e.p.server.rs.ChatServerEndpoint - There are 1 opened sessions
22:22:54.351 [http-listener-1(5)] DEBUG e.p.server.rs.ChatServerEndpoint - Session 437a9b4d-c694-4a19-b912-47b88da4eb9a associated with null
22:23:58.888 [http-listener-1(2)] DEBUG e.p.server.rs.ChatServerEndpoint - Closing the session 437a9b4d-c694-4a19-b912-47b88da4eb9a associated with null
我尝试通过将websocket路径包含在JAAS配置的安全性约束的web-resource-collection(web.xml文件)中来使其工作,但在这种情况下,从客户端访问websocket失败了一段时间后java.util.concurrent.TimeoutException。
这是我的web-app.xml文件:
<web-app 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"
version="3.0">
<welcome-file-list>
<welcome-file>Ps.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>es.ps.server.file.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>psRealm</realm-name>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/login.html?error=true</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure Pages</web-resource-name>
<description/>
<url-pattern>*.html</url-pattern>
<url-pattern>/Ps/*</url-pattern>
<url-pattern>/rs/*</url-pattern>
<url-pattern>/chat</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>ADMINISTRATOR</role-name>
</auth-constraint>
</security-constraint>
</web-app>
有人遇到过同样的问题吗?
提前致谢。