1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Java】axis2のSOAP StubにWS-SecurityのUsername Tokenを設定する実装方法メモ

Posted at

いろいろ探したけど具体的な実装方法に関するhintがStackOverFlow(しかも英語…)にしかなかったので備忘のためにメモ。

StackOverFlowの出展は以下
StackOverFlow:https://stackoverflow.com/questions/7910776/adding-user-password-to-soapheader-for-webservice-client-call-with-axis2
著者:https://stackoverflow.com/users/957076/shiv-gopal
(この人、自力で解決したらしい(I resolve the issue myself)、、、すげーなー)

ほとんどこの人の回答通りだがそのままやるとエラーになったので下記コメント部分の通りの追加修正を施している。


OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMElement omSecurityElement = omFactory.createOMElement(new QName( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "wsse"), null);

OMElement omusertoken = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "UsernameToken", "wsu"), null);
// createOMElementの引数QNameの第一引数にWS-SecurityのURIを指定
OMElement omuserName = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Username", "wsse"), null);
omuserName.setText("myusername");
// createOMElementの引数QNameの第一引数にWS-SecurityのURIを指定
OMElement omPassword = omFactory.createOMElement(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Password", "wsse"), null);
omPassword.addAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText",null );
omPassword.setText("mypassword");

omusertoken.addChild(omuserName);
omusertoken.addChild(omPassword);
omSecurityElement.addChild(omusertoken);
stub._getServiceClient().addHeader(omSecurityElement);

追記修正点として、StackOverFlowの回答では、QNameオブジェクトつくるときに第一引数に空文字を指定しているが、これだと実行時に「java.lang.IllegalArgumentException: Cannot create a prefixed element with an empty namespace」が発生する。
なので明示的にWS-SecurityのURIを指定させている。
だからたぶんコレ回答の誤記じゃないかと思うのだが…なんかやり方あるのかな。

また、omuserName.setText("myusername");とomPassword.setText("mypassword");に自身のユーザーとパスワードを設定する。

WS-Securityの「仕様」や「概念」に関しては、検索すればそこそこ出てくるのだが、具体的な実装方法があまり見つからず、仮に見つかってもaixsのバージョンが違うみたいで気軽にsetHeaderとかできなかったりして、結構困った。
aixs2のStubはServiceClient取り出す以外は全てprotectedになっていて、外からはほとんど何もできなく、axis1のStubに比べて自由度が非常に制限されている印象である。
今回のように、headerの設定方法にはなかなか苦労させられた。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?