我在Spring Boot 2项目中添加了AJP连接器
@Bean public ServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() { @Override protected void postProcessContext(Context context) { SecurityConstraint securityConstraint = new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } }; tomcat.addAdditionalTomcatConnectors(redirectConnector()); return tomcat; } private Connector redirectConnector() { Connector connector = new Connector("AJP/1.3"); connector.setScheme("http"); connector.setPort(ajpPort); connector.setSecure(false); connector.setAllowTrace(false); return connector; }
这很好。现在,我可以通过apache网络服务器访问我的spring boot应用程序。但是现在,如果我运行Spring Boot应用程序,则无法直接访问Spring Boot应用程序。因此,该网址不再起作用
http:// localhost:13080 / online / showlogin?m = test
如果我禁用了AJP连接器,则URL将再次起作用。我尝试了以下
private Connector redirectConnector2() { Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL); connector.setScheme("http"); connector.setPort(13080); connector.setSecure(false); connector.setAllowTrace(false); return connector; } ... tomcat.addAdditionalTomcatConnectors(redirectConnector2()); ...
但这对我没有帮助。
这对我有用:
@Bean public WebServerFactoryCustomizer<TomcatServletWebServerFactory> servletContainer() { return server -> { if (server instanceof TomcatServletWebServerFactory) { ((TomcatServletWebServerFactory) server).addAdditionalTomcatConnectors(redirectConnector()); } }; } private Connector redirectConnector() { Connector connector = new Connector("AJP/1.3"); connector.setScheme("http"); connector.setPort(ajpPort); connector.setSecure(false); connector.setAllowTrace(false); return connector; }