Weblogic Message Driven Bean reading from a secured queue @RunAs does not work

杀马特。学长 韩版系。学妹 提交于 2019-12-04 20:28:22

If you annotate your MDB as follows it should work:

@MessageDriven(name = "MdbReceiver", mappedName = "DistributedQueueTest")
@DeclareRoles({"Loan"})
@RolesAllowed("Loan")
public class MdbReceiver implements MessageListener {
...
}
Cris

I answer to my question cause i found a solution : The problem is that annotations or are buggy in weblogic or are not implemented as I expected.

Solution is to do this without annotations in old style

so MDB is:

public class MdbReceiver implements MessageListener ,MessageDrivenBean{

    MessageDrivenContext mdc;

    @Override
    public void onMessage(Message inMessage) {
        TextMessage msg = null;
        try {
            msg = (TextMessage) inMessage;
            System.out.println("qwerty1");
            System.out.println("Test MdbReceiver Message received : " + msg.getText());
        } catch (JMSException e) {
            e.printStackTrace();
            mdc.setRollbackOnly();
        }
    }
    @Override
    public void ejbRemove() throws EJBException {
        // TODO Auto-generated method stub

    }
    @Override
    public void setMessageDrivenContext(MessageDrivenContext mdc) throws EJBException {
        this.mdc = mdc;

    }
}

Then we need two other files: ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.0" 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/ejb-jar_3_0.xsd">
    <enterprise-beans>
        <message-driven>
            <ejb-name>MdbReceiver</ejb-name>
            <ejb-class>mdb.receiver.MdbReceiver</ejb-class>
            <transaction-type>Container</transaction-type>
            <message-destination-type>javax.jms.Queue</message-destination-type>
            <security-identity>
                <run-as>
                    <role-name>Loan</role-name>
                </run-as>
            </security-identity>
        </message-driven>
    </enterprise-beans>
    <assembly-descriptor>       
         <security-role>
          <role-name>Loan</role-name>
         </security-role>       
        <container-transaction>
            <method>
                <ejb-name>MdbReceiver</ejb-name>
                <method-name>onMessage()</method-name>
            </method>
            <trans-attribute>Required</trans-attribute>
        </container-transaction>
    </assembly-descriptor>

</ejb-jar>

and

weblogic-ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-ejb-jar version="wls_10.3"
    xmlns="http://www.bea.com/ns/weblogic/weblogic-ejb-jar" xmlns:j2ee="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-ejb-jar http://www.bea.com/ns/weblogic/weblogic-ejb-jar.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
    <weblogic-enterprise-bean>
        <ejb-name>MdbReceiver</ejb-name>
        <message-driven-descriptor>
            <destination-jndi-name>DistributedQueueTest</destination-jndi-name>
        </message-driven-descriptor>
    </weblogic-enterprise-bean>
    <security-role-assignment>
        <role-name>Loan</role-name>     
        <principal-name>test1234</principal-name>
    </security-role-assignment>

</weblogic-ejb-jar>

The role name does not matter as i can see the principal name is important. It needs to have the rights to read from the secured queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!