I am using SpringMVC to receive HTTP requests from a machine we are trying to interface to. XML data from the machine is written in the HTTP request body. Basically,
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo version="2.0" xmlns="http://www.example.com/ns">
<Bar sessionId="2" />
<Baz quux="Monitor" seq="123">
...
</Baz>
</Foo>
The machine does not, and can not keep cookies. So I am unable to use session data over JSESSIONID. All I have is the sessionId found in Bar. This sessionId should be granted by my system on the first request. That is,
Step 1: Machine sends session request to me
Step 2: The web app creates a session and then sends a Session type response to the machine in which it then saves and uses in subsequent requests.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Foo version="2.0" xmlns="http://www.example.com/ns">
<Bar sessionId="2" />
<Session quux="Monitor" seq="123">
...
</Session>
</Foo>
Step 3: Communication between the machine and the web app now uses sessionId.
Questions:
By default, Spring Security will create a session when it needs one — this is “ifRequired“. For a more stateless application, the “never” option will ensure that Spring Security itself won't create any session. But if the application creates one, Spring Security will make use of it.
Spring Security is very mature and widely used security framework for Java based web applications. It works perfectly with minimal configuration and following successful login returns JSESSIONID cookie which allows to re-authenticate client's consecutive calls as long as session doesn't expire.
In Spring Security 5.7.0-M2 we deprecated the WebSecurityConfigurerAdapter , as we encourage users to move towards a component-based security configuration.
What you are looking for is certainly possible. The HTTP Session is simply a container for storing the Spring Security authentication token in between requests. What you are looking for is a place to store the token in between requests and being reliably able to retrieve the token for every request.
The component that holds the token in between requests is an implementation of org.springframework.security.web.context.SecurityContextRepository
. One of the out-of-box implementations provided by Spring Security uses the HTTP Session as the storage area for tokens.
Similarly, the component that checks the token on every request is an implementation of org.springframework.security.authentication.AuthenticationProvider
. At a bare minimum you need implementations for these two in order to enforce your custom strategy for storing and checking authentication tokens on every request, outside of the HTTP Session.
You can take a look at my sample app for a working example of this strategy for a REST based application. I will recommend that you pass the session information in HTTP headers instead of request body. It will reduce your implementation effort and simplify the solution significantly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With