我正在开发Spring Project,我正在尝试以编程方式阻止会话跟踪的URL参数。这是我的代码
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("/");
}
}
**我的问题**是,还有其他方法可以通过编程方式执行此操作。我不确定这是否正确,
有人可以帮助我将这行代码放在正确的位置
servletContext.setSessionTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE));
答案 0 :(得分:0)
您可以在web.xml中指定会话跟踪模式
<web-app>
....other stuff
<session-config>
<tracking-mode>COOKIE</tracking-mode>
<session-timeout>20</session-timeout>
</session-config>
</web-app>
这将禁用服务器的URL重写
答案 1 :(得分:0)
如果您改变主意以编程方式进行操作,请访问application.properties:
server.servlet.session.tracking-modes=cookie
完成。