koca使用宝兰德jar包验证
1.验证使用版本
koca使用版本 2.x.x-RELEASE(本次验证使用2.4.0版本),bes使用SpringBoot2.x版本对应的版本
2.将bes的jar包安装到本地
mvn install:install-file -Dfile=bes-lite-spring-boot-2.x-starter-9.5.2.jar -DgroupId=com.bes.appserver -DartifactId=bes-lite-spring-boot-starter-9.5.2 -Dversion=9.5.2 -Dpackaging=jar
mvn install:install-file -Dfile=bes-gmssl-9.5.2.jar -DgroupId=com.bes.appserver -DartifactId=bes-gmssl-9.5.2 -Dversion=9.5.2 -Dpackaging=jar
mvn install:install-file -Dfile=bes-jasper-9.5.2.jar -DgroupId=com.bes.appserver -DartifactId=bes-jasper-9.5.2 -Dversion=9.5.2 -Dpackaging=jar
mvn install:install-file -Dfile=bes-jdbcra-9.5.2.jar -DgroupId=com.bes.appserver -DartifactId=bes-jdbcra-9.5.2 -Dversion=9.5.2 -Dpackaging=jar
mvn install:install-file -Dfile=bes-actuator-spring-boot-2.x-starter-9.5.2.jar -DgroupId=com.bes.appserver -DartifactId=bes-actuator-spring-boot-2.x-starter-9.5.2 -Dversion=9.5.2 -Dpackaging=jar
mvn install:install-file -Dfile=bes-websocket-9.5.2.jar -DgroupId=com.bes.appserver -DartifactId=bes-websocket-9.5.2 -Dversion=9.5.2 -Dpackaging=jar
3.修改项目pom,去除tomcat依赖,增加bes依赖
<dependency>
<groupId>szkingdom.yf.koca.base</groupId>
<artifactId>koca-cloud-msa-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- bes -->
<dependency>
<groupId>com.bes.appserver</groupId>
<artifactId>bes-lite-spring-boot-starter-9.5.2</artifactId>
<version>9.5.2</version>
</dependency>
<dependency>
<groupId>com.bes.appserver</groupId>
<artifactId>bes-gmssl-9.5.2</artifactId>
<version>9.5.2</version>
</dependency>
<dependency>
<groupId>com.bes.appserver</groupId>
<artifactId>bes-jasper-9.5.2</artifactId>
<version>9.5.2</version>
</dependency>
<dependency>
<groupId>com.bes.appserver</groupId>
<artifactId>bes-jdbcra-9.5.2</artifactId>
<version>9.5.2</version>
</dependency>
<dependency>
<groupId>com.bes.appserver</groupId>
<artifactId>bes-actuator-spring-boot-2.x-starter-9.5.2</artifactId>
<version>9.5.2</version>
</dependency>
<dependency>
<groupId>com.bes.appserver</groupId>
<artifactId>bes-websocket-9.5.2</artifactId>
<version>9.5.2</version>
</dependency>
<!--bes-->
4.解决bes对web spring-socket兼容问题
启动过程中报错:
报错原因是因为bes的包对spring-websocket包兼容有问题,未依赖koca-websocket(里面有spring-websocket包)不会有该问题。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
可以通过重写spring原生类来解决该问题,在最外层application中添加org.springframework.web.socket.server.support.AbstractHandshakeHandler类和org.springframework.web.socket.server.standard.BesRequestUpgradeStrategy类来覆盖spring-websocket中的原生类,使原生类失效而添加的类生效,添加类代码结构如下:
修改的AbstractHandshakeHandler类,改动点如下:
新增的BesRequestUpgradeStrategy类,该类代码拷贝自TomcatRequestUpgradeStrategy类,修改点如下:
5. 服务正常启动
启动过程会输出如下内容:
如下调用普通api正常:
调用actuator接口正常:
6.完整类代码
类1:
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.standard;
import com.bes.enterprise.web.websocket.server.WsServerContainer;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Endpoint;
import javax.websocket.Extension;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.web.socket.server.HandshakeFailureException;
//import org.apache.tomcat.websocket.server.WsServerContainer;
/**
* A WebSocket {@code RequestUpgradeStrategy} for Apache Tomcat. Compatible with
* all versions of Tomcat that support JSR-356, i.e. Tomcat 7.0.47+ and higher.
*
* <p>To modify properties of the underlying {@link javax.websocket.server.ServerContainer}
* you can use {@link ServletServerContainerFactoryBean} in XML configuration or,
* when using Java configuration, access the container instance through the
* "javax.websocket.server.ServerContainer" ServletContext attribute.
*
* @author Rossen Stoyanchev
* @since 4.0
*/
public class BesRequestUpgradeStrategy extends AbstractStandardUpgradeStrategy {
@Override
public String[] getSupportedVersions() {
return new String[]{"13"};
}
@Override
public void upgradeInternal(ServerHttpRequest request, ServerHttpResponse response,
@Nullable String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint)
throws HandshakeFailureException {
HttpServletRequest servletRequest = getHttpServletRequest(request);
HttpServletResponse servletResponse = getHttpServletResponse(response);
StringBuffer requestUrl = servletRequest.getRequestURL();
String path = servletRequest.getRequestURI(); // shouldn't matter
Map<String, String> pathParams = Collections.<String, String>emptyMap();
ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint);
endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol));
endpointConfig.setExtensions(selectedExtensions);
try {
getContainer(servletRequest).doUpgrade(servletRequest, servletResponse, endpointConfig, pathParams);
} catch (ServletException ex) {
throw new HandshakeFailureException(
"Servlet request failed to upgrade to WebSocket: " + requestUrl, ex);
} catch (IOException ex) {
throw new HandshakeFailureException(
"Response update failed during upgrade to WebSocket: " + requestUrl, ex);
}
}
@Override
public WsServerContainer getContainer(HttpServletRequest request) {
return (WsServerContainer) super.getContainer(request);
}
}
类2:
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.support;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.Lifecycle;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.socket.SubProtocolCapable;
import org.springframework.web.socket.WebSocketExtension;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator;
import org.springframework.web.socket.server.HandshakeFailureException;
import org.springframework.web.socket.server.HandshakeHandler;
import org.springframework.web.socket.server.RequestUpgradeStrategy;
/**
* A base class for {@link HandshakeHandler} implementations, independent from the Servlet API.
*
* <p>Performs initial validation of the WebSocket handshake request - possibly rejecting it
* through the appropriate HTTP status code - while also allowing its subclasses to override
* various parts of the negotiation process (e.g. origin validation, sub-protocol negotiation,
* extensions negotiation, etc).
*
* <p>If the negotiation succeeds, the actual upgrade is delegated to a server-specific
* {@link org.springframework.web.socket.server.RequestUpgradeStrategy}, which will update
* the response as necessary and initialize the WebSocket. Currently supported servers are
* Jetty 9.0-9.3, Tomcat 7.0.47+ and 8.x, Undertow 1.0-1.3, GlassFish 4.1+, WebLogic 12.1.3+.
*
* @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 4.2
* @see org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy
* @see org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy
* @see org.springframework.web.socket.server.standard.UndertowRequestUpgradeStrategy
* @see org.springframework.web.socket.server.standard.GlassFishRequestUpgradeStrategy
* @see org.springframework.web.socket.server.standard.WebLogicRequestUpgradeStrategy
*/
public abstract class AbstractHandshakeHandler implements HandshakeHandler, Lifecycle {
private static final boolean jettyWsPresent;
private static final boolean tomcatWsPresent;
private static final boolean undertowWsPresent;
private static final boolean glassfishWsPresent;
private static final boolean weblogicWsPresent;
private static final boolean websphereWsPresent;
private static final boolean besWsPresent;
static {
ClassLoader classLoader = AbstractHandshakeHandler.class.getClassLoader();
jettyWsPresent = ClassUtils.isPresent(
"org.eclipse.jetty.websocket.server.WebSocketServerFactory", classLoader);
tomcatWsPresent = ClassUtils.isPresent(
"org.apache.tomcat.websocket.server.WsHttpUpgradeHandler", classLoader);
undertowWsPresent = ClassUtils.isPresent(
"io.undertow.websockets.jsr.ServerWebSocketContainer", classLoader);
glassfishWsPresent = ClassUtils.isPresent(
"org.glassfish.tyrus.servlet.TyrusHttpUpgradeHandler", classLoader);
weblogicWsPresent = ClassUtils.isPresent(
"weblogic.websocket.tyrus.TyrusServletWriter", classLoader);
websphereWsPresent = ClassUtils.isPresent(
"com.ibm.websphere.wsoc.WsWsocServerContainer", classLoader);
besWsPresent = ClassUtils.isPresent("com.bes.enterprise.web.websocket.server.WsHttpUpgradeHandler", classLoader);
}
protected final Log logger = LogFactory.getLog(getClass());
private final RequestUpgradeStrategy requestUpgradeStrategy;
private final List<String> supportedProtocols = new ArrayList<>();
private volatile boolean running = false;
/**
* Default constructor that auto-detects and instantiates a
* {@link RequestUpgradeStrategy} suitable for the runtime container.
* @throws IllegalStateException if no {@link RequestUpgradeStrategy} can be found.
*/
protected AbstractHandshakeHandler() {
this(initRequestUpgradeStrategy());
}
/**
* A constructor that accepts a runtime-specific {@link RequestUpgradeStrategy}.
* @param requestUpgradeStrategy the upgrade strategy to use
*/
protected AbstractHandshakeHandler(RequestUpgradeStrategy requestUpgradeStrategy) {
Assert.notNull(requestUpgradeStrategy, "RequestUpgradeStrategy must not be null");
this.requestUpgradeStrategy = requestUpgradeStrategy;
}
private static RequestUpgradeStrategy initRequestUpgradeStrategy() {
String className;
if (tomcatWsPresent) {
className = "org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy";
} else if (jettyWsPresent) {
className = "org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy";
} else if (undertowWsPresent) {
className = "org.springframework.web.socket.server.standard.UndertowRequestUpgradeStrategy";
} else if (glassfishWsPresent) {
className = "org.springframework.web.socket.server.standard.GlassFishRequestUpgradeStrategy";
} else if (weblogicWsPresent) {
className = "org.springframework.web.socket.server.standard.WebLogicRequestUpgradeStrategy";
} else if (websphereWsPresent) {
className = "org.springframework.web.socket.server.standard.WebSphereRequestUpgradeStrategy";
} else if (besWsPresent) {
className = "org.springframework.web.socket.server.standard.BesRequestUpgradeStrategy";
} else {
throw new IllegalStateException("No suitable default RequestUpgradeStrategy found");
}
try {
Class<?> clazz = ClassUtils.forName(className, AbstractHandshakeHandler.class.getClassLoader());
return (RequestUpgradeStrategy) ReflectionUtils.accessibleConstructor(clazz).newInstance();
} catch (Exception ex) {
throw new IllegalStateException(
"Failed to instantiate RequestUpgradeStrategy: " + className, ex);
}
}
/**
* Return the {@link RequestUpgradeStrategy} for WebSocket requests.
*/
public RequestUpgradeStrategy getRequestUpgradeStrategy() {
return this.requestUpgradeStrategy;
}
/**
* Use this property to configure the list of supported sub-protocols.
* The first configured sub-protocol that matches a client-requested sub-protocol
* is accepted. If there are no matches the response will not contain a
* {@literal Sec-WebSocket-Protocol} header.
* <p>Note that if the WebSocketHandler passed in at runtime is an instance of
* {@link SubProtocolCapable} then there is not need to explicitly configure
* this property. That is certainly the case with the built-in STOMP over
* WebSocket support. Therefore this property should be configured explicitly
* only if the WebSocketHandler does not implement {@code SubProtocolCapable}.
*/
public void setSupportedProtocols(String... protocols) {
this.supportedProtocols.clear();
for (String protocol : protocols) {
this.supportedProtocols.add(protocol.toLowerCase());
}
}
/**
* Return the list of supported sub-protocols.
*/
public String[] getSupportedProtocols() {
return StringUtils.toStringArray(this.supportedProtocols);
}
@Override
public void start() {
if (!isRunning()) {
this.running = true;
doStart();
}
}
protected void doStart() {
if (this.requestUpgradeStrategy instanceof Lifecycle) {
((Lifecycle) this.requestUpgradeStrategy).start();
}
}
@Override
public void stop() {
if (isRunning()) {
this.running = false;
doStop();
}
}
protected void doStop() {
if (this.requestUpgradeStrategy instanceof Lifecycle) {
((Lifecycle) this.requestUpgradeStrategy).stop();
}
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response,
WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException {
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHeaders());
if (logger.isTraceEnabled()) {
logger.trace("Processing request " + request.getURI() + " with headers=" + headers);
}
try {
if (HttpMethod.GET != request.getMethod()) {
response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET));
if (logger.isErrorEnabled()) {
logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod());
}
return false;
}
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
handleInvalidUpgradeHeader(request, response);
return false;
}
if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) {
handleInvalidConnectHeader(request, response);
return false;
}
if (!isWebSocketVersionSupported(headers)) {
handleWebSocketVersionNotSupported(request, response);
return false;
}
if (!isValidOrigin(request)) {
response.setStatusCode(HttpStatus.FORBIDDEN);
return false;
}
String wsKey = headers.getSecWebSocketKey();
if (wsKey == null) {
if (logger.isErrorEnabled()) {
logger.error("Missing \"Sec-WebSocket-Key\" header");
}
response.setStatusCode(HttpStatus.BAD_REQUEST);
return false;
}
} catch (IOException ex) {
throw new HandshakeFailureException(
"Response update failed during upgrade to WebSocket: " + request.getURI(), ex);
}
String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler);
List<WebSocketExtension> requested = headers.getSecWebSocketExtensions();
List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request);
List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported);
Principal user = determineUser(request, wsHandler, attributes);
if (logger.isTraceEnabled()) {
logger.trace("Upgrading to WebSocket, subProtocol=" + subProtocol + ", extensions=" + extensions);
}
this.requestUpgradeStrategy.upgrade(request, response, subProtocol, extensions, user, wsHandler, attributes);
return true;
}
protected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
if (logger.isErrorEnabled()) {
logger.error("Handshake failed due to invalid Upgrade header: " + request.getHeaders().getUpgrade());
}
response.setStatusCode(HttpStatus.BAD_REQUEST);
response.getBody().write("Can \"Upgrade\" only to \"WebSocket\".".getBytes(StandardCharsets.UTF_8));
}
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
if (logger.isErrorEnabled()) {
logger.error("Handshake failed due to invalid Connection header " + request.getHeaders().getConnection());
}
response.setStatusCode(HttpStatus.BAD_REQUEST);
response.getBody().write("\"Connection\" must be \"upgrade\".".getBytes(StandardCharsets.UTF_8));
}
protected boolean isWebSocketVersionSupported(WebSocketHttpHeaders httpHeaders) {
String version = httpHeaders.getSecWebSocketVersion();
String[] supportedVersions = getSupportedVersions();
for (String supportedVersion : supportedVersions) {
if (supportedVersion.trim().equals(version)) {
return true;
}
}
return false;
}
protected String[] getSupportedVersions() {
return this.requestUpgradeStrategy.getSupportedVersions();
}
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) {
if (logger.isErrorEnabled()) {
String version = request.getHeaders().getFirst("Sec-WebSocket-Version");
logger.error("Handshake failed due to unsupported WebSocket version: " + version +
". Supported versions: " + Arrays.toString(getSupportedVersions()));
}
response.setStatusCode(HttpStatus.UPGRADE_REQUIRED);
response.getHeaders().set(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION,
StringUtils.arrayToCommaDelimitedString(getSupportedVersions()));
}
/**
* Return whether the request {@code Origin} header value is valid or not.
* By default, all origins as considered as valid. Consider using an
* {@link OriginHandshakeInterceptor} for filtering origins if needed.
*/
protected boolean isValidOrigin(ServerHttpRequest request) {
return true;
}
/**
* Perform the sub-protocol negotiation based on requested and supported sub-protocols.
* For the list of supported sub-protocols, this method first checks if the target
* WebSocketHandler is a {@link SubProtocolCapable} and then also checks if any
* sub-protocols have been explicitly configured with
* {@link #setSupportedProtocols(String...)}.
* @param requestedProtocols the requested sub-protocols
* @param webSocketHandler the WebSocketHandler that will be used
* @return the selected protocols or {@code null}
* @see #determineHandlerSupportedProtocols(WebSocketHandler)
*/
@Nullable
protected String selectProtocol(List<String> requestedProtocols, WebSocketHandler webSocketHandler) {
List<String> handlerProtocols = determineHandlerSupportedProtocols(webSocketHandler);
for (String protocol : requestedProtocols) {
if (handlerProtocols.contains(protocol.toLowerCase())) {
return protocol;
}
if (this.supportedProtocols.contains(protocol.toLowerCase())) {
return protocol;
}
}
return null;
}
/**
* Determine the sub-protocols supported by the given WebSocketHandler by
* checking whether it is an instance of {@link SubProtocolCapable}.
* @param handler the handler to check
* @return a list of supported protocols, or an empty list if none available
*/
protected final List<String> determineHandlerSupportedProtocols(WebSocketHandler handler) {
WebSocketHandler handlerToCheck = WebSocketHandlerDecorator.unwrap(handler);
List<String> subProtocols = null;
if (handlerToCheck instanceof SubProtocolCapable) {
subProtocols = ((SubProtocolCapable) handlerToCheck).getSubProtocols();
}
return (subProtocols != null ? subProtocols : Collections.emptyList());
}
/**
* Filter the list of requested WebSocket extensions.
* <p>As of 4.1, the default implementation of this method filters the list to
* leave only extensions that are both requested and supported.
* @param request the current request
* @param requestedExtensions the list of extensions requested by the client
* @param supportedExtensions the list of extensions supported by the server
* @return the selected extensions or an empty list
*/
protected List<WebSocketExtension> filterRequestedExtensions(ServerHttpRequest request,
List<WebSocketExtension> requestedExtensions, List<WebSocketExtension> supportedExtensions) {
List<WebSocketExtension> result = new ArrayList<>(requestedExtensions.size());
for (WebSocketExtension extension : requestedExtensions) {
if (supportedExtensions.contains(extension)) {
result.add(extension);
}
}
return result;
}
/**
* A method that can be used to associate a user with the WebSocket session
* in the process of being established. The default implementation calls
* {@link ServerHttpRequest#getPrincipal()}
* <p>Subclasses can provide custom logic for associating a user with a session,
* for example for assigning a name to anonymous users (i.e. not fully authenticated).
* @param request the handshake request
* @param wsHandler the WebSocket handler that will handle messages
* @param attributes handshake attributes to pass to the WebSocket session
* @return the user for the WebSocket session, or {@code null} if not available
*/
@Nullable
protected Principal determineUser(
ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
return request.getPrincipal();
}
}