Storing keys in servlets - servlets

My servlet generates pair of keys(public and private) when the client makes a request for keys and sends the public key to the client using ObjectOutputStream, which is working fine.
The client then encrypts data using the public key and sends it the servlet again.
so again the servlets executes from the beginning and a new set of keys are generated which I don't want.
I want to be able to use the old private key to decrypt the message?
what should i do?

Create the keys only once when the servlet itself is created, not on every request. You can create the public and private key pair in the servlet's init method and then store it in an instance variable of the servlet.
There is a nice description here.

Related

.NET Core 6 + Blazor: How to call server while only passing data in the uri

My question is about how to do the following with .NET Core 6 Blazor. If I knew how to simply make Blazor send some URI to the server, I would just do so, but I don't. I have googled for this quite extensively, but not found an answer.
I want to modify a single property of a record stored in a table. The server should load the data record with the given id, increment or decrement a counter stored in it, and save the record. There is no need to send the data to the client, have it modify the data, and have it send the modified data back to the server. Basically I am trying to model kind of RPC with Blazor.
So I want to make a call to my .NET Core 6 Blazor server, only passing a parameter in the URI (the ID of the database record to be modified).
Is there a method I could call that doesn't require a content parameter like PostAsync() and PutAsync() do? Right now I am doing this:
public static async Task<bool> AddReference (Guid ID)
{
HttpResponseMessage response =
await httpClient.PostAsync(Routes.WatermarkApi + $"AddReference/{ID}", null);
return (response.StatusCode == System.Net.HttpStatusCode.OK);
}
passing null for the content parameter, but this looks a little awkward to me.

Passing cookies from jax-rx rest api end point to ejb

I am looking for a solution related to passing cookies values from JAX-RX REST API endpoint to ejb layer.
I tried searching the solution and found some of them are using thread local. but and thread local supposed to be not working with executor services.
Is there any another solution that I can use to pass cookies values from web/rest later to ejb layer.
Note: I do not want to pass it as method parameter from rest layer to down the ejb layer. I would like to access the cookies values directly in ejb layer
Thanks
Lets say you have a class MyEJB, you can inject HttpServletRequest object to you ejb as follows:
public class MyEJB{
#Context
private HttpServletRequest httpRequest;
}
Now from this request object you can access your required cookies. As follows:
String rawCookie = request.getHeader("Cookie");
String[] rawCookieParams = rawCookie.split(";");
for(String rawCookieNameAndValue :rawCookieParams)
{
String[] rawCookieNameAndValuePair = rawCookieNameAndValue.split("=");
}

Cannot reconfigure DbContext more than once

I have a .Net Core 2.1 Web API talking to a MySQL database, using the Pomelo provider (version 2.1.0-rc1-final). Because this is a multi-tenant application, the API needs to change databases depending on which tenant is connecting. The front end web app tells the API which tenant is doing the request by including a TenantId header in the HTPP request.
When the API receives an HTTP request from the front end, I have a service in the API's pipeline that reads the TenantId from the request, and then this is used to determine which database the API must connect to.
To connect to various databases, I change the connection string of the DbContext. I do this in the OnConfiguring event:
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
string connectionString = CreateConnectionString();
optionsBuilder.UseMySql(connectionString);
base.OnConfiguring(optionsBuilder);
}
My problem is that this works the first time, but the second time this event fires, when it executes the optionsBuilder.UseMySql(connectionString); line, it throws this exception:
An item with the same key has already been added. Key: Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal.MySqlOptionsExtension
It's only letting me configure the DbContext once. I need to reconfigure it every time an API action endpoint gets called. Any ideas?
I'm not 100% sure what I did - but it's working now. The process is as follows: I created middleware to identify the tenant making the request (i.e. consuming the API). This is done by reading a "tenantId" that the client app sends in the header of the request, and then saving that tenantId into a global variable (I used HttpContext). Because middleware executes before pretty much most other things, by the time the OnConfiguring event of the dbContext fires, I already know which tenant this is So then, on the OnConfiguring event of the dbContext, I set the connectionString of the dbContext to whatever it should be (depending on the tenantId). I can't post the code in this comment because comments don't allow line breaks and they are too short, but If you want to, send me your email address and I'll email you the code.

Why do ArrayLists not initialize in Web Services?

The client has a very old .net 1.1 app they need to hit a service. I built a middle piece in 4.0 that hits the service and I am attempting to return the data. The classes I built use ArrayLists since the .net 1.1 won't have access to generic lists. When the client consumes my code, the ArtayLists come over as ArrayOfAnyType and each must be initialized on the CLIENT side, despite the fact that I initialized them in Sub New(). I don't wan tthe client to have to initialize every arraylist. Here is the unremarkable code...
<Serializable()> _
Public Class XApplication
Public Sub New()
_People = New ArrayList
_PhysicalHouseholds = New ArrayList
_TaxReturns = New ArrayList
End Sub
Public Property People() As ArrayList
Public Property PhysicalHouseholds() As ArrayList
Public Property TaxReturns() As ArrayList
End Class
Tried using the XmlInclude Attribute to define the class type but it had no effect. To be clear, I CAN NOT use WCF. I have to use web services.
When a client proxy is generated from a web reference, the proxy will only model the data. Implementation code (including the constructor) cannot be obtained from WSDL. As a result, when a new instance of the proxy class is created, properties will have the default value.
You always have to create the object on the client side.
You can't send an object though a web service. The message that is sent is plain text, so any object has to be sent as the text representation that it contains, and be recreated on the client side by parsing the text and putting the data into a new object.

Passing Auth to API calls with Web Service References

I am new to web services. The last time I dealt with SOAP was when I created a bunch of wrapper classes that sent requests and received responses back per some response objects/classes I had created. So I had an object to send certain API requests and likewise a set of objects to hold the response back as an object so I could utilize that 3rd party API.
Then someone came to me and said why not just use the wsdl and a web service. Ok, so today I went and created a "Service Reference". I see that this is what's called a "Proxy Class". You just instantiate an instance of this and then walla you have access to all the methods from the wsdl.
But this leaves me with auth questions. Back when I created my own classes manually, I had a class which exposed properties that I would set then access for things like signature, username, password that got sent along with the Http request that were required by whatever 3rd party API I was using to make API calls.
But then with using a Service Reference, how then would I pass this information just like I had done in my custom classes? For instance I'm going to be working with the PayPal API. It requires you to send a signature and a few other pieces of information like username and password.
// Determins if API call needs to use a session based URI
string requestURI = UseAuthURI == true ? _requestURIAuthBased + aSessionID : _requestURI;
byte[] data = XmlUtil.DocumentToBytes(doc);
// Create the atual Request instance
HttpWebRequest request = CreateWebRequest(requestURI, data.Length);
So how do I pass username, password, signature, etc. when using web service references for each method call? Is it as simple as specifying it as a param to the method or do you use the .Credentials and .URL methods of your proxy class object? It seems to me Credentials means windows credentials but I could be wrong. Is it limited to that or can you use that to specify those required header values that PayPal expects with each method call/API request?
Using Web Service or Web Service Reference

Resources