diff --git a/jswitcher-sample/dependency-reduced-pom.xml b/jswitcher-sample/dependency-reduced-pom.xml index fb35e66..c17975d 100644 --- a/jswitcher-sample/dependency-reduced-pom.xml +++ b/jswitcher-sample/dependency-reduced-pom.xml @@ -92,6 +92,12 @@ 2.1.3.RELEASE provided + + org.springframework.boot + spring-boot-starter-web + 2.1.3.RELEASE + provided + diff --git a/jswitcher-sample/pom.xml b/jswitcher-sample/pom.xml index b91d9a2..df49656 100644 --- a/jswitcher-sample/pom.xml +++ b/jswitcher-sample/pom.xml @@ -32,6 +32,10 @@ org.springframework.boot spring-boot-starter + + org.springframework.boot + spring-boot-starter-web + diff --git a/jswitcher-sample/src/main/java/com/github/xincao9/jswitcher/sample/controller/RootController.java b/jswitcher-sample/src/main/java/com/github/xincao9/jswitcher/sample/controller/RootController.java new file mode 100644 index 0000000..75fbaf7 --- /dev/null +++ b/jswitcher-sample/src/main/java/com/github/xincao9/jswitcher/sample/controller/RootController.java @@ -0,0 +1,35 @@ +/* + * Copyright 2019 xincao9@gmail.com. + * + * 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 + * + * http://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 com.github.xincao9.jswitcher.sample.controller; + +import org.apache.commons.lang3.RandomStringUtils; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 输出随机字符串 + * + * @author xincao9@gmail.com + */ +@RestController +public class RootController { + + @GetMapping("/") + public ResponseEntity root () { + return ResponseEntity.ok(RandomStringUtils.randomAscii(128)); + } +} diff --git a/jswitcher-spring-boot-starter/src/main/java/com/github/xincao9/jswitcher/spring/boot/starter/UriInterceptor.java b/jswitcher-spring-boot-starter/src/main/java/com/github/xincao9/jswitcher/spring/boot/starter/UriInterceptor.java index 5465553..ed7dab8 100644 --- a/jswitcher-spring-boot-starter/src/main/java/com/github/xincao9/jswitcher/spring/boot/starter/UriInterceptor.java +++ b/jswitcher-spring-boot-starter/src/main/java/com/github/xincao9/jswitcher/spring/boot/starter/UriInterceptor.java @@ -25,6 +25,7 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; +import org.apache.commons.lang3.StringUtils; /** * Uri拦截器 @@ -60,13 +61,17 @@ public void init(FilterConfig filterConfig) throws ServletException { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hsr = (HttpServletRequest) request; - String path = hsr.getPathInfo(); + String path = hsr.getRequestURI(); String method = hsr.getMethod(); + if (StringUtils.isBlank(path)) { + path = "/"; + } String key = String.format("%s:%s", method, path); switcherService.register(key, Boolean.TRUE, key, QoS.API); if (switcherService.isOpen(key)) { chain.doFilter(request, response); + } else { + request.getRequestDispatcher("/404").forward(request, response); } } - } diff --git a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java index 21bedec..0ecea4f 100644 --- a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java +++ b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java @@ -26,6 +26,7 @@ import com.github.xincao9.jswitcher.api.service.SwitcherService; import com.github.xincao9.jswitcher.api.vo.Switcher; import java.util.ArrayList; +import java.util.Base64; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -303,6 +304,7 @@ private ResponseEntity cmd(String cmd, String host, Integer port, String key, Bo if (StringUtils.isBlank(host) || port == null || port <= 0 || port > 65535 || StringUtils.isBlank(key)) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } + key = new String(Base64.getDecoder().decode(key)); StringBuilder method = new StringBuilder(); method.append(SwitcherService.class.getTypeName()) .append('.') diff --git a/jswitcher-ui/src/main/resources/application.yml b/jswitcher-ui/src/main/resources/application.yml index 6b3bb52..1f24589 100644 --- a/jswitcher-ui/src/main/resources/application.yml +++ b/jswitcher-ui/src/main/resources/application.yml @@ -13,4 +13,4 @@ spring: restart: enabled: true server: - port: 8080 \ No newline at end of file + port: 8181 \ No newline at end of file diff --git a/jswitcher-ui/src/main/resources/templates/index.html b/jswitcher-ui/src/main/resources/templates/index.html index 9fa8176..ba7e9f3 100644 --- a/jswitcher-ui/src/main/resources/templates/index.html +++ b/jswitcher-ui/src/main/resources/templates/index.html @@ -120,7 +120,7 @@ }, methods: { change: function (key) { - var url = '/switcher/key/' + (key.open ? 'off' : 'on') + '/' + key.host + '/' + key.port + '/' + key.key; + var url = '/switcher/key/' + (key.open ? 'off' : 'on') + '/' + key.host + '/' + key.port + '/' + window.btoa(key.key); axios.get(url).then( (response) => { if (response.status !== 200) { @@ -131,7 +131,7 @@ }); }, persistence: function (key) { - var url = '/switcher/key/set/' + key.host + '/' + key.port + '/' + key.key + '/' + key.open; + var url = '/switcher/key/set/' + key.host + '/' + key.port + '/' + window.btoa(key.key) + '/' + key.open; axios.get(url).then( (response) => { if (response.status !== 200) {