java - How Spring Boot redirect http to https with HTTP 301? -
in spring boot project,i add https support,when curl -i http://127.0.0.1:80
,i got http/1.1 302
,how can redirect http 301. config,in code has comment maybe useful. can me?very thankful!
@configuration public class webconfig { @bean public embeddedservletcontainerfactory servletcontainerfactory() { tomcatembeddedservletcontainerfactory factory = new tomcatembeddedservletcontainerfactory() { @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); } }; factory.addadditionaltomcatconnectors(createhttpconnector()); return factory; } private connector createhttpconnector() { //org.apache.catalina.connector.connector,in source code saw method named createresponse(), //this method return org.apache.catalina.connector.response, //in response has method sendredirect(),there constant set httpstatus 302 connector connector = new connector("org.apache.coyote.http11.http11nioprotocol"); connector.setscheme("http"); connector.setsecure(false); connector.setport(80); connector.setredirectport(443); return connector; } @bean public embeddedservletcontainercustomizer containercustomizer() { return container -> { ssl ssl = new ssl(); ssl.setkeystore("classpath:***.jks"); ssl.setkeystorepassword("***"); container.setssl(ssl); container.setport(443); }; } }
Comments
Post a Comment