in a springboot application with Basic Authentication I'm trying to authorize requests getting the sessionID from the url in the format ";jsessionid=xxx" I know that that's not a good practice to put session in URI but it's a requirement.
I'm using Spring-session-jdbc.
I tryed to make a filter named JSessionIDFilter
http
.addFilterBefore(new JSessionIDFilter(), UsernamePasswordAuthenticationFilter.class)
In the filter:
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
// Get sessionID from URI
String requestURI = request.getRequestURI();
String sessionID = "";
int index = requestURI.indexOf(";jsessionid=");
if (index >= 0)
{
sessionID = requestURI.substring(index+12);
//If session ID is valid I would like to retrive the Authentication by sessionid and associate to the current request
Authentication authentication = ....
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
After some research I found a solution to my problem I would share:
I haven't used filter but I create a different custom HttpSessionIdResolver in the SpringBoot configuration:
Here is the new class: