1. Certificate generation reference: http://huangyongxing310.iteye.com/blog/2352693
application.properties
#Server server.port=8090 #LOGGING logging.pattern.level=INFO #server.port:8443 #server.ssl.key-store: classpath:keystore.p12 ##server.ssl.key-store-password: aqjcpt #server.ssl.key-store-password:123456 #server.ssl.keyStoreType: PKCS12 #server.ssl.keyAlias: tomcat
package com.cesmart; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; @EnableAutoConfiguration @ComponentScan(basePackages = "com.cesmart") // scan those packages for beans.@ComponentScan({"com.teradata.notification","com.teradata.dal"}) //@EnableSwagger2 //Start swagger annotation public class Application { public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(Application.class, args); } }
package com.cesmart.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.google.common.base.Predicates; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration//Defined as the configuration file of spring boot @EnableSwagger2//Start swagger annotation public class Swagger2 { public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.cesmart.controller"; @Bean(value="createRestApi") public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("test1") .pathMapping("/") .apiInfo (apiInfo ()) .select() .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE)) .paths(Predicates.or(PathSelectors.regex("/*/.*"))) .build(); //groupName, group name //pathMapping, mapping path (will be added to the front of the URL to form a new path, such as: "/xing/WebTest/webTest",(pathMapping("/xing"))) //apiInfo, API information description //select, selecting those paths and api will generate document //apis, scan those packages, RequestHandlerSelectors.any() means to monitor all apis //paths, match those paths, PathSelectors.any() means all paths, } @Bean(value="createRestApi2") public Docket createRestApi2() { return new Docket(DocumentationType.SWAGGER_2) .groupName("test2") .pathMapping("/") .apiInfo (apiInfo2 ()) .select() .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE)) .paths(Predicates.or(PathSelectors.regex("/*/.*"))) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Building RESTful APIs with Swagger2 in Spring Boot") .description("For more Spring Boot related articles, please pay attention: http://blog.didispace.com/") .termsOfServiceUrl("http://blog.didispace2.com/") .contact("Program Ape DD") .version("1.0") .license("license") .licenseUrl("licenseUrl") .build(); //title, title, displayed at the top of the page //description, description, displayed at the top of the page //termsOfServiceUrl, //contact, showing "Created by + contact", at the top of the page //version, API version, displayed at the top of the page //license, copyright } private ApiInfo apiInfo2() { return new ApiInfoBuilder() .title("Building RESTful APIs with Swagger2 in Spring Boot") .description("For more Spring Boot related articles, please pay attention: http://blog.didispace.com/") .termsOfServiceUrl("http://blog.didispace2.com/") .contact("Program Ape DD") .version("1.0") .license("license") .licenseUrl("licenseUrl") .build(); } }
package com.cesmart.config; import java.io.File; import org.apache.catalina.Context; import org.apache.catalina.connector.Connector; import org.apache.tomcat.util.descriptor.web.SecurityCollection; import org.apache.tomcat.util.descriptor.web.SecurityConstraint; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.Ssl; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import com.google.common.base.Predicates; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration // Defined as the configuration file of spring boot public class WebConfig { // Http access URL private static final String HTTP_URL_PATTERNS[] = { "/HttpTest/*" }; // URL accessed by Https private static final String HTTPS_URL_PATTERNS[] = { "/HttpsTest/*" }; @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { Ssl ssl = new Ssl(); // Server.jks contains the server private key and certificate ssl.setKeyStore("classpath:keystore.p12"); ssl.setKeyStorePassword("123456"); container.setSsl(ssl); container.setPort(8443); } }; } @Bean public EmbeddedServletContainerFactory servletContainerFactory() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { // SecurityConstraint must exist, through which different redirection strategies can be set for different URLs. SecurityConstraint securityConstraint = new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); for (String pattern : HTTPS_URL_PATTERNS) { collection.addPattern(pattern); } securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); // ============== SecurityConstraint securityConstraintHttp = new SecurityConstraint(); securityConstraintHttp.setUserConstraint("NONE"); SecurityCollection collectionHttp = new SecurityCollection(); for (String pattern : HTTP_URL_PATTERNS) { collectionHttp.addPattern(pattern); } securityConstraintHttp.addCollection(collectionHttp); context.addConstraint(securityConstraintHttp); } }; factory.addAdditionalTomcatConnectors(createHttpConnector()); return factory; } private Connector createHttpConnector() { Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setSecure(false); connector.setPort(8090); connector.setRedirectPort (8443); return connector; } }
package com.cesmart.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @RestController @Api(value = "HttpsTest", description = "About HttpsTest operation") @RequestMapping(value = "/HttpsTest") // Used on a class to describe the role of the class // value, the description to display in the class // description, the description in the class // Display form: "value: description", as shown above as "WebTest: about Swagger2 operations" public class HttpsTest { @ApiOperation(value = "Interface Description", notes = "Interface Release Notes", response = String.class) // used in the method to explain the function of the method // Displayed in the method description, showing notes // response, the interface returns the parameter type // value = "Interface Description", // notes = "Interface Release Notes" @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", required = true, name = "test", dataType = "String", value = "456"), @ApiImplicitParam(paramType = "path", required = true, name = "test2", dataType = "String", value = "789") }) // @ApiImplicitParam, represents the description of a parameter, which is related to the request parameter // paramType, where to put the parameter // required, whether the parameter must be passed // name, parameter name // dataType, parameter type (description) // value, the meaning of the parameter (description) @ApiParam @RequestMapping(value = "/HttpsTestGet/{test}/{test2}", produces = "text/plain;charset=UTF-8", method = RequestMethod.GET) public String HttpsTestGet(@PathVariable("test") String test, @PathVariable("test2") String test2) { System.out.println("HttpTest"); System.out.println("test == " + test); System.out.println("test2 == " + test2); return "HttpTest"; } @ApiOperation(value = "Interface Description", notes = "Interface Release Notes", response = String.class) @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", required = true, name = "test", dataType = "String", value = "456", defaultValue = "test"), @ApiImplicitParam(paramType = "query", required = true, name = "test2", dataType = "String", value = "789", defaultValue = "test2") }) @RequestMapping(value = "/HttpsTestPost", produces = "text/plain;charset=UTF-8", method = RequestMethod.POST) public String HttpsTestPost(String test, String test2) { System.out.println("webTest"); System.out.println("test == " + test); System.out.println("test2 == " + test2); return "webTest"; } }
package com.cesmart.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @RestController @Api(value = "HttpTest", description = "About HttpTest operation") @RequestMapping(value = "/HttpTest") // Used on a class to describe the role of the class // value, the description to display in the class // description, the description in the class // Display form: "value: description", as shown above as "WebTest: about Swagger2 operations" public class HttpTest { @ApiOperation(value = "Interface Description", notes = "Interface Release Notes", response = String.class) // used in the method to explain the function of the method // Displayed in the method description, showing notes // response, the interface returns the parameter type // value = "Interface Description", // notes = "Interface Release Notes" @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", required = true, name = "test", dataType = "String", value = "456"), @ApiImplicitParam(paramType = "path", required = true, name = "test2", dataType = "String", value = "789") }) // @ApiImplicitParam, represents the description of a parameter, which is related to the request parameter // paramType, where to put the parameter // required, whether the parameter must be passed // name, parameter name // dataType, parameter type (description) // value, the meaning of the parameter (description) @ApiParam @RequestMapping(value = "/HttpTestGet/{test}/{test2}", produces = "text/plain;charset=UTF-8", method = RequestMethod.GET) public String HttpTestGet(@PathVariable("test") String test, @PathVariable("test2") String test2) { System.out.println("HttpTest"); System.out.println("test == " + test); System.out.println("test2 == " + test2); return "HttpTest"; } @ApiOperation(value = "Interface Description", notes = "Interface Release Notes", response = String.class) @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", required = true, name = "test", dataType = "String", value = "456", defaultValue = "test"), @ApiImplicitParam(paramType = "query", required = true, name = "test2", dataType = "String", value = "789", defaultValue = "test2") }) @RequestMapping(value = "/HttpTestPost", produces = "text/plain;charset=UTF-8", method = RequestMethod.POST) public String HttpTestPost(String test, String test2) { System.out.println("webTest"); System.out.println("test == " + test); System.out.println("test2 == " + test2); return "webTest"; } }
Reference (using Https in Spring Boot (application reference this)): http://www.cnblogs.com/xinzhao/p/4952856.html
Reference (Spring Boot is based on Tomcat's HTTP and HTTPS protocol configuration, this is good): http ://kchu.me/2015/08/19/Spring-Boot%E5%9F%BA%E4%BA%8ETomcat%E7%9A%84HTTP%E5%92%8CHTTPS%E5%8D%8F%E8%AE %AE%E9%85%8D%E7%BD%AE/