简体   繁体   English

对Glassfish上的订阅者进行身份验证的JMS主题

[英]JMS Topic with authentication for subscribers on Glassfish

I have a JMS Topic configured on my Glassfish Server, and I implemented a client to subscribe the Topic and print the messages it receives. 我在Glassfish服务器上配置了一个JMS主题,并且实现了一个客户端来订阅主题并打印它收到的消息。 This is working fine. 一切正常。

Here is my client. 这是我的客户。 You can see that I opted to use a kind of 'direct connection' instead of using JNDI lookup. 可以看到,我选择使用一种“直接连接”而不是使用JNDI查找。

com.sun.messaging.ConnectionFactory connFactory = new com.sun.messaging.ConnectionFactory();
connFactory.setProperty(com.sun.messaging.ConnectionConfiguration.imqAddressList, "mq://localhost:7676/");
TopicConnection connection = connFactory.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("myTopic");
TopicSubscriber subscriber = session.createSubscriber(topic);
subscriber.setMessageListener(this);
connection.start();

In this way any client can subscribe my Topic. 这样,任何客户都可以订阅我的主题。 What I want now is to find a way to force the client to authenticate before it start receiving messages. 我现在想要的是找到一种方法来强制客户端在开始接收消息之前进行身份验证。 Is that possible on Glassfish? Glassfish有可能吗?

So far I've tried changing the 'default JMS host' credentials on Glassfish admin page and passing the new credentials I've set on connection creation: 到目前为止,我已经尝试在Glassfish管理页面上更改“默认JMS主机”凭据,并传递在连接创建时设置的新凭据:

TopicConnection connection = connFactory.createTopicConnection("myuser", "mypass");

But this didn't work. 但这没有用。 It works if I pass the default credentials: 如果我通过默认凭据,它将起作用:

TopicConnection connection = connFactory.createTopicConnection("admin", "admin");

I think I must have to change the credentials somewhere else, but I don't know where. 我认为我必须在其他地方更改凭据,但是我不知道在哪里。 And even if it works, it will force the client to authenticate? 即使有效,也会强制客户端进行身份验证吗? I mean, there will be no other way for my client to subscribe my Topic without having credentials? 我的意思是,如果没有凭据,我的客户将没有其他方法来订阅我的主题吗?

Short answer: 简短答案:

1 - Create an user on imqbroker (glassfish3\\mq\\bin\\imqusermgr.exe). 1-在imqbroker(glassfish3 \\ mq \\ bin \\ imqusermgr.exe)上创建一个用户。

2 - Edit the accesscontrol.properties file (myDomain\\imq\\instances\\imqbroker\\etc) and set which user can consume which topic. 2-编辑accesscontrol.properties文件(myDomain \\ imq \\ instances \\ imqbroker \\ etc),并设置哪个用户可以使用哪个主题。

Long answer: 长答案:

1 - Execute via command prompt: 1-通过命令提示符执行:

\glassfish3\mq\bin\imqusermgr add -varhome c:\glassfish3\glassfish\domains\myDomain\imq -u myuser -p mypass

This will create an user on imqbroker indicated by varhome with specific username and password. 这将在varhome指示的imqbroker上创建一个具有特定用户名和密码的用户。

2 - In the accesscontrol.properties file (myDomain\\imq\\instances\\imqbroker\\etc), edit the section destination based access control to something like that: 2-在accesscontrol.properties文件(myDomain \\ imq \\ instances \\ imqbroker \\ etc)中,将destination based access control节目标的destination based access control编辑为类似以下内容:

topic.myTopic.consume.allow.user=myUser
topic.myTopic.consume.deny.user=*
topic.*.consume.allow.user=*

This will allow myUser consume myTopic and deny other users. 这将允许myUser使用myTopic并拒绝其他用户。 And the rest of the topics you have will continue to allow all users to consume them. 您拥有的其余主题将继续允许所有用户使用它们。 Note that topic.*.consume.allow.user=* does not replace the topic.myTopic.consume.deny.user=* . 请注意, topic.*.consume.allow.user=* topic.myTopic.consume.deny.user=*不会替代topic.myTopic.consume.deny.user=*

My code remained the same: 我的代码保持不变:

TopicConnection conn = connectionFactory.createTopicConnection("myuser", "mypass");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM