1:Spring boot 项目默认的web服务器为tomcat,故排除tomcat依赖,加入undertow依赖,请看下图:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-undertow
server:
address: 192.168.1.119
port: 7201
context-path: /note1
undertow:
accesslog:
enabled: false
ssl:
key-store: D:/rsakey/loveshare.keystore
key-store-password: 123456
http:
port: 5201
package me.loveshare.note1.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "http")
public class HttpProperties {
private Integer port;
}
package me.loveshare.note1.configuration;
import io.undertow.Undertow;
import lombok.extern.slf4j.Slf4j;
import me.loveshare.note1.properties.HttpProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
@EnableConfigurationProperties(HttpProperties.class)
public class HttpConfiguration {
@Autowired
private HttpProperties properties;
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {
UndertowEmbeddedServletContainerFactory undertow = new UndertowEmbeddedServletContainerFactory();
undertow.addBuilderCustomizers((Undertow.Builder builder) -> {
builder.addHttpListener(properties.getPort(), "0.0.0.0");
});
log.info("\n*** Undertow http setting successful." + properties.getPort());
return undertow;
}
}
package me.loveshare.note1.api;
import lombok.extern.slf4j.Slf4j;
import me.loveshare.note1.data.entity.bo.common.JsonResult;
import me.loveshare.note1.data.entity.bo.common.JsonResultMethod;
import me.loveshare.note1.data.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@Slf4j
@RestController
public class TestDataApi extends BaseApi {
/**
* 协议测试
*/
@ResponseBody
@RequestMapping(value = "test", produces = "application/json; charset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})
public JsonResult initDbDatasC() {
return JsonResultMethod.code_200("The request completed successfully.", DateUtils.timestamp(new Date()));
}
}
package me.loveshare.note1.api;
import lombok.extern.slf4j.Slf4j;
import me.loveshare.note1.data.utils.NetworkUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import javax.servlet.http.HttpServletRequest;
@Slf4j
public class BaseApi {
@ModelAttribute
public void firstMC(HttpServletRequest request, @RequestHeader(value = "uAT", required = false) String uAT) {
request.setAttribute("uAT", uAT); //此处可转换用户的id
//打印请求日志
printAccess(request);
}
private final void printAccess(HttpServletRequest request) {
StringBuilder su = new StringBuilder();
su.append("\nUser-Access-Args:").append("{");
su.append("\"protocol\":\"").append(request.getProtocol() + "(" + request.getScheme()).append(")\",");
su.append("\"ip\":\"").append(NetworkUtils.getIpAddr(request)).append("\",");
su.append("\"port\":\"").append(NetworkUtils.getPort(request)).append("\",");
su.append("\"method\":\"").append(request.getMethod()).append("\",");
su.append("\"url\":\"").append(NetworkUtils.getCurrentURL(request)).append("\",");
su.append("\"user-agent\":\"").append(NetworkUtils.getUserAgent(request)).append("\",");
su.append("\"uAT\":\"").append(request.getAttribute("uAT")).append("\"}");
log.info(su.toString());
}
}
http://192.168.1.119:5201/note1/test.json
https://192.168.1.119:7201/note1/test.json
User-Access-Args:{"protocol":"HTTP/1.1(http)","ip":"192.168.1.119","port":"52188","method":"GET","url":"/note1/test.json","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36","uAT":"null"}
User-Access-Args:{"protocol":"HTTP/1.1(https)","ip":"192.168.1.119","port":"52197","method":"GET","url":"/note1/test.json","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36","uAT":"null"}
GitHub全源代码: https://github.com/typa01/spring-boot/tree/master/spring-boot-note1