2017-09-24 7 views
1

소스 코드 대신 자세한 설명을 입력하지 않으면이 질문을 게시 할 수 없습니다. 그래서 나는 더 적은 글을 쓰고 있습니다.ActiveMQ extends SimpleAuthenticationPlugin은 NullPointerException을 발생합니다.

다음과 같은 등급이 있습니다. 내 목표는이 플러그인을 설정하여 BrokerService를 보호하는 것입니다.

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을 확보하는 것입니다 - : 나는 NullPointerException이 예외를받을 경우 다음과 같이

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); 
     } 
    } 

나중에 나는 위의 클래스를 사용하려고 맞습니다. 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); 
      } 
    }