How to add security (username, password) to grails cxf web service - web-services

I created two grails project, one for server side cxf web service and other for cxf client for calling web service..
Everything works fine.
I can call web service from client code and get result.
Now I want to add security, then what will be the changes to server and client grails code?
I tried applying security as said by Christian Oestreich in his post.
http://www.christianoestreich.com/2012/04/grails-cxf-interceptor-injection/
(Grails Cxf Interceptor Injection 2.4.x-2.5.x)
and client code for applying security is as follows
ExampleService exampleService = new ExampleService()
def port = exampleService.exampleServicePort
Map ctx = ((BindingProvider)port).getRequestContext();
ctx.put("ws-security.username", "pankaj");
ctx.put("ws-security.password", "pankaj");
println ".......... " + port.sayHello("pankaj")
But I am getting error as follows
Error |
2014-11-06 18:33:15,411 [http-bio-8088-exec-4] ERROR errors.GrailsExceptionResolver - SoapFault occurred when processing request: [GET] /WSDLDemoClient/wsdldemo/index
An error was discovered processing the <wsse:Security> header.. Stacktrace follows:
Message: An error was discovered processing the <wsse:Security> header.
Line | Method
->> 75 | unmarshalFault in org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor

Instead of above mentioned client code, use following code to get it working.
Map<String, Object> req_ctx = ((BindingProvider)hello).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("pankaj"));
headers.put("Password", Collections.singletonList("pankaj"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

Related

FormatException: SyntaxError: Unexpected token < in JSON at position 1 in Flutter after changing proxy from HTTP to HTTPS

I have a project, backend with Django and frontend with Flutter framework. The project was working fine until I changed the proxy in my Nginx from HTTP to HTTPS. Still working in most cases without any Problem. just in some cases, where I have no Data in my Data Table, I want/have to return an empty list from the backend, I got an error:
GET https://servername/project-name/api/commitments/?email=emailaddress%40companyname.de&jahr=2022&kunde=adac-ev 500
FormatException: SyntaxError: Unexpected token < in JSON at position 1
code in flutter:
var uri = (APIPROTOCOL == 'http://')
? Uri.http(APIHOST, '/api/commitments/', uriQuery)
: Uri.https(APIHOST, '$APIHOST_PREFIX/api/commitments/', uriQuery);
try {
final response = await http.get(
uri,
headers: {"Authorization": "Bearer $authToken"},
);
And this is working on localhost and on a server with HTTP but the problem is only on a sever with HTTPS,
any idea? how can debug the code on the server? or how can I change my localhost from http:://localhost:protNumber/ to something like https://servername/project-name/. Any idea that could help me to debug the code with https?

Using Fiddler to troubleshoot classic asp file calling web service

I am having trouble debugging a classic asp file in which I call a .NET web service. I am hoping that I can use Fiddler to see why the HTTP request to the web service is returning a 'Bad Request' error. I have a console application to request the classic asp file. Within the classic asp file I call the web service. When I run Fiddler, I see the request to the asp file but not the request to the web service. There is just one line in Fiddler with the asp file request.
Any suggestions or advice to troubleshoot the asp file would be appreciated.
Below is the code to sends a GET request to the web service.
The response in the console application is:
Status: 200
Status text: OK
But the call to the web service does not appear in Fiddler, only the call to the asp file. The web service and asp file are on different servers.
Shouldn't I see this call to the web service on Fiddler?
I want to debug a POST request but first I must learn how to use Fiddler. :)
This is my console application that calls the asp page:
Uri aspPServiceUri = new Uri("http://<serverNameAndPath>/FirstPage.asp?<parameters>;");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(aspPServiceUri);
httpWebRequest.Method = "GET";
HttpWebResponse resp = (HttpWebResponse)httpWebRequest.GetResponse();
Stream resStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
string strResponse = reader.ReadToEnd();
Console.WriteLine(strResponse);
reader.Close();
This the asp file:
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
URL = "http://<serverName>:<port>/PService/PService.Paging.svc?WSDL"
httpRequest.Open "GET", URL, False
httpRequest.Send()
Dim strResult
Dim strStatusText
Response.ContentType = "text/xml"
strStatusText = "Status: " & httpRequest.status & vbCrLf & "Status text: " & httpRequest.statusText
Response.Write(vbCrLf & strStatusText & vbCrLf)
Assuming you are using IIS 7+ or a relatively recent version of IIS Express, add the following to your web.config file. If you don't already have a web.config, then add one.
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy autoDetect="False" bypassonlocal="False" proxyaddress="http://127.0.0.1:8888" usesystemdefault="False" />
</defaultProxy>
</system.net>
This configures Fiddler as a proxy, so all HTTP requests from the web app will be routed through Fiddler.

Elixir call Axis2 Java SOAP Web Service with detergentex and detergent

From Elixir, I am trying to call a SOAP Web Service with detergentex, which is a wrapper around the Erlang library detergent.
I can call the SOAP Web Service in the example on the detergent home page with no problems: http://www.webservicex.net/convertVolume.asmx?WSDL
Parameters:
wsdl_url = "http://www.webservicex.net/convertVolume.asmx?WSDL"
action = "ChangeVolumeUnit"
parameters = ["100","dry","centiliter"]
However when trying to call an Axis2 Java SOAP Web Service I am having some problems. Example: http://www.thomas-bayer.com/axis2/services/BLZService?wsdl
When calling "getBank" in the following way
wsdl_url = "http://www.thomas-bayer.com/axis2/services/BLZService?wsdl"
action = "getBank"
parameters = ["abc"]
wsdl = Detergentex.init_model(wsdl_url)
res = Detergentex.call(wsdl, action, parameters)
I get the following error message:
{:error, 'Struct doesn\'t match model: recordtype not expected: p:getBank'}
with the following stack trace
src/erlsom_write.erl:357: :erlsom_write.findAlternative/4
src/erlsom_write.erl:258: :erlsom_write.processSubType/5
src/erlsom_write.erl:241: :erlsom_write.processElementValues/7
src/erlsom_write.erl:132: :erlsom_write.struct2xml/6
src/erlsom_write.erl:323: :erlsom_write.processAlternativeValue/8
src/erlsom_write.erl:241: :erlsom_write.processElementValues/7
src/erlsom_write.erl:116: :erlsom_write.struct2xml/6
src/erlsom_write.erl:323: :erlsom_write.processAlternativeValue/8
src/erlsom_write.erl:241: :erlsom_write.processElementValues/7
src/erlsom_write.erl:45: :erlsom_write.write/2
src/detergent.erl:211: :detergent.call_attach/8
Any suggestions as to what I'm doing wrong?
According to the implementation a prefix is added on the init_model.
def init_model(wsdl_url, prefix \\ 'p') do
Detergentex.Client.init_model(wsdl_url, prefix)
end
I would suggest to add an empty prefix.

WSO2 API Manager 1.8.0 fails to import Swagger definition

I'm trying to import Swagger definition in WSO2 AM 1.8.0 while creating new API. But, the import fails with the exceptions in logs as below,
ERROR {JAGGERY.site.blocks.item-design.ajax.import:jag} - org.mozilla.javascript.WrappedException: Wrapped com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 27 (/publisher/site/blocks/item-design/ajax/import.jag#25)
Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 27
Caused by: com.google.gson.stream.MalformedJsonException: Expected EOF at line 1 column 27
I'm using below configurations:
Swagger-core_2.11-1.3.12
Swagger-jersey2-jaxrs_2.11-1.3.12
Swagger-annotations-1.3.12
Tomcat 7
JDK 1.7
WSO2 API Manager 1.8.0
I create two simple restful services and add Swagger annotations to them. I can see doc in Swagger UI and can invoke them as well. I verify generated JSON for listing and resources and find it valid. But, when I try to import that definition in WSO2AM, it fails to do so.
What Swagger specification does WSO2 AM 1.8.0 support?
Any help is highly appreciated. Thanks.
I'm able to overcome this problem. The file wso2am-1.8.0\repository\deployment\server\jaggeryapps\publisher\site\blocks\ite‌​m-design\ajax\import.jag has code which sends http requests and fetches json data in loop.
var xhr = new XMLHttpRequest();
for(var i = 0; i < swaggerJson.apis.length; i++){
xhr.open("GET", url + swaggerJson.apis[i].path);
For some reason, it's unable to reuse XMLHttpRequest instance to send subsequent requests. I moved XMLHttpRequest instance creation statement in a for loop and it worked.
//var xhr = new XMLHttpRequest();
for(var i = 0; i < swaggerJson.apis.length; i++){
var xhr = new XMLHttpRequest(); // moved instance creation here
xhr.open("GET", url + swaggerJson.apis[i].path);

JBoss EAP 5.1 Server not using Windows Network Setting

I'm trying to call a web service from my application. My system is protected with a Firewall and I'm using a Proxy to access any external URL/internet access. The application is running on JBoss EAP 5.1 server. The application fails to write to the service URL with IO Exception: 'Could not transmit message'.
However, when I'm trying to access the service URL with IE/Firefox, it's opening. Although the XML response I'm receiving from Browser states a generic error - 'invalid request parameters...', which is quite obvious. Because I'm not sending a proper request XML from Browser.
I'm really confused with this disparity. I used to believe that JBoss will pick up standard windows network settings, but in my case it is not.
My code is as follows:
String strUrl = "http://theurlgoeshere";
String requestXml = "<request></request>";
String wsResponse="";
SOAPConnection conn = null;
try {
MessageFactory msgFac = MessageFactory.newInstance();
MimeHeaders mh = new MimeHeaders();
mh.setHeader("Content-Type", "text/xml; charset=UTF-8");
log.info("Request Xml:" + requestXml );
InputStream is = new ByteArrayInputStream(requestXml.getBytes("UTF-8"));
SOAPMessage reqMsg = msgFac.createMessage(mh, is);
SOAPConnectionFactory conFac = SOAPConnectionFactory.newInstance();
conn = conFac.createConnection();
SOAPMessage repMsg = conn.call(reqMsg, strUrl);
ByteArrayOutputStream out = new ByteArrayOutputStream();
repMsg.writeTo(out);
wsResponse = new String(out.toByteArray());
}
catch (Exception e) {
e.printStackTrace();
}
Got it sorted few days back. Basically I am using HttpURLConnection now to add proxy setting in the java code itself while making the Webservice call. Just closing this question, since my query is solved.
Will update the new code, if anyone needs.