I am working on Spring Project and am I am trying to prevent URL Parameters for Session Tracking programmatically. This is my code
import org.auctions.Config.MvcConfig;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import web.SessionListenerWithMetrics;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.SessionTrackingMode;
import java.util.EnumSet;
public class SecurityWebApplicationInitializer implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(MvcConfig.class);
servletContext.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));
servletContext.addListener(SessionListenerWithMetrics.class);
rootContext.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher =
servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
**My question** is, are there any other approaches to do this programmatically. I am not sure if this a proper way,
Can someone help me to put this line of code in the right place
servletContext.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));
If you change your mind about having to be programmatically, in application.properties:
server.servlet.session.tracking-modes=cookie
Done.
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