SSLConfig.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.rf.psychological.security;
  2. import org.apache.catalina.Context;
  3. import org.apache.catalina.connector.Connector;
  4. import org.apache.tomcat.util.descriptor.web.SecurityCollection;
  5. import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.Profile;
  11. /**
  12. * @author zzf
  13. * @description:
  14. * @date 2021/9/15 14:32
  15. */
  16. //@Configuration
  17. @Profile({"public"})
  18. public class SSLConfig {
  19. @Value("${spring.profiles.active}")
  20. private String profile;
  21. @Bean
  22. public TomcatServletWebServerFactory servletContainer() { //springboot2 新变化
  23. TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
  24. @Override
  25. protected void postProcessContext(Context context) {
  26. SecurityConstraint securityConstraint = new SecurityConstraint();
  27. securityConstraint.setUserConstraint("CONFIDENTIAL");
  28. SecurityCollection collection = new SecurityCollection();
  29. collection.addPattern("/*");
  30. securityConstraint.addCollection(collection);
  31. context.addConstraint(securityConstraint);
  32. }
  33. };
  34. tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
  35. return tomcat;
  36. }
  37. private Connector initiateHttpConnector() {
  38. Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  39. connector.setScheme("http");
  40. connector.setSecure(false);
  41. if ("prod".equals(profile)) {
  42. //112机构版正式
  43. connector.setPort(8082);
  44. connector.setRedirectPort(8848);
  45. } else if ("public".equals(profile)) {
  46. //101心理照相机公网版正式
  47. connector.setPort(8084);
  48. connector.setRedirectPort(8445);
  49. } else {
  50. //TODO 待定
  51. //112心理照相机
  52. connector.setPort(9528);
  53. connector.setRedirectPort(8849);
  54. }
  55. return connector;
  56. }
  57. }