2017-09-24 56 views
1

我不能發佈這個問題,除非我寫了更多的描述,而不是源代碼。這就是爲什麼我只寫了幾個文本。ActiveMQ擴展SimpleAuthenticationPlugin thoews NullPointerException

我有以下課程。我的目標是通過設置此插件來保護BrokerService。

public class MyAuthenticationPlugin extends SimpleAuthenticationPlugin { 
     private String username ="username"; 
     private String password ="password"; 

     public MyAuthenticationPlugin(){ 
      secureME(); 
     } 
     public void secureME(){ 
      Map<String, String> map = new HashMap<String, String>(); 
      map.put(username, password); 
      this.setUserPasswords(map); 
     } 
    } 

後來我嘗試使用上面的類如下,其中我得到一個NullPointerException異常: -

public class Server{ 
     private static int ackMode; 
     private static String messageQueueName; 
     private static String messageBrokerUrl; 

     private Session session; 
     private boolean transacted = false; 
     private MessageProducer replyProducer; 
     private MessageProtocol messageProtocol; 
     private String username ="username"; 
     private String password ="password"; 

     static { 
      messageBrokerUrl = "tcp://localhost:61616"; 
      messageQueueName = "client.messages"; 
      ackMode = Session.AUTO_ACKNOWLEDGE; 
     } 

     public Server() { 
      try { 
       //This message broker is embedded 
       BrokerService broker = new BrokerService(); 
       broker.setPersistent(false); 
       broker.setUseJmx(false); 
       broker.addConnector(messageBrokerUrl); 
       // here I'm add my class as plguing 
       MyAuthenticationPlugin[] myAuthenticationPlugin = new 
       MyAuthenticationPlugin[1]; 
       myAuthenticationPlugin[0] = new MyAuthenticationPlugin(); 

       broker.setPlugins(myAuthenticationPlugin); 
       broker.start(); 
      } catch (Exception e) { 
       System.out.println("Exception: "+e.getMessage()); 
       //Handle the exception appropriately 
      } 
     } 

     private void setupMessageQueueConsumer() { 
      ActiveMQConnectionFactory connectionFactory = new 
       ActiveMQConnectionFactory(messageBrokerUrl); 

      connectionFactory.setUserName(username); 
      connectionFactory.setPassword(password); 

      Connection connection; 
      try { 
       connection = connectionFactory.createConnection(username, password); 


       connection.start(); // This line thows exception 
       this.session = connection.createSession(this.transacted, ackMode); 
       Destination adminQueue = this.session.createQueue(messageQueueName); 

       //Setup a message producer to respond to messages from clients, we will get the destination 
       //to send to from the JMSReplyTo header field from a Message 
       this.replyProducer = this.session.createProducer(null); 
       this.replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 

       //Set up a consumer to consume messages off of the admin queue 
       MessageConsumer consumer = this.session.createConsumer(adminQueue); 
       consumer.setMessageListener(this); 

       // new BufferedReader(new InputStreamReader(System.in)).readLine(); 

      } catch (JMSException e) { 
       System.out.println("Exception: "+e.getMessage()); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     public static void main(String[] args) { 
      new Server(); 
      System.out.println("I'm done. END"); 
     } 
    } 

主要是我的目標是通過設置用戶名,密碼,訪問組,以確保我的BrokerService安全權利等。請建議我如何在BrokerService中設置用戶名,密碼,訪問權限以使其更安全。

更新與堆棧跟蹤: -

result = {StackTraceElement[7]@1015} 
    0 = {[email protected]} "org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:49)" 
     declaringClass = "org.apache.activemq.util.JMSExceptionSupport" 
     methodName = "create" 
     fileName = "JMSExceptionSupport.java" 
     lineNumber = 49 
    1 = {[email protected]} "org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1377)" 
     declaringClass = "org.apache.activemq.ActiveMQConnection" 
     methodName = "syncSendPacket" 
     fileName = "ActiveMQConnection.java" 
     lineNumber = 1377 
    2 = {[email protected]} "org.apache.activemq.ActiveMQConnection.ensureConnectionInfoSent(ActiveMQConnection.java:1481)" 
     declaringClass = "org.apache.activemq.ActiveMQConnection" 
     methodName = "ensureConnectionInfoSent" 
     fileName = "ActiveMQConnection.java" 
     lineNumber = 1481 
    3 = {[email protected]} "org.apache.activemq.ActiveMQConnection.start(ActiveMQConnection.java:516)" 
     declaringClass = "org.apache.activemq.ActiveMQConnection" 
     methodName = "start" 
     fileName = "ActiveMQConnection.java" 
     lineNumber = 516 
    4 = {[email protected]} "com.ma.home.Server.setupMessageQueueConsumer(Server.java:57)" 
     declaringClass = "com.ma.home.Server" 
     methodName = "setupMessageQueueConsumer" 
     fileName = "Server.java" 
     lineNumber = 57 
    5 = {[email protected]} "com.ma.home.Server.<init>(Server.java:46)" 
     declaringClass = "com.ma.home.Server" 
     methodName = "<init>" 
     fileName = "Server.java" 
     lineNumber = 46 
    6 = {[email protected]} "com.ma.home.Server.main(Server.java:84)" 
     declaringClass = "com.ma.home.Server" 
     methodName = "main" 
     fileName = "Server.java" 
     lineNumber = 84 
+0

您是否有堆棧跟蹤? – Logan

+0

請檢查更新問題中的堆棧跟蹤。還要重現異常,您可以執行給定的代碼。謝謝! – masiboo

回答

0

我必須與所有三個正確的參數如用戶名,密碼,組添加AuthenticationUser。我以前沒有在我的代碼中提供組。所以遵循的代碼將修復。此外,它工作正常。

 public class MyAuthenticationPlugin extends SimpleAuthenticationPlugin { 
      private String username ="username"; 
      private String password ="password"; 
      private String groups = "groups"; 

      Map<String, String> userPasswords = new HashMap<String, String>(); 
      List<AuthenticationUser> authenticationUserList = new ArrayList(); 

      public MyAuthenticationPlugin(){ 
       secureME(); 
      } 
      public void secureME(){ 
       userPasswords.put(username, password); 
       authenticationUserList.add(new AuthenticationUser(username,password, groups)); 
       this.setUserPasswords(userPasswords); 
       this.setUsers(authenticationUserList); 
      } 
    }