首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何强制RequestMapping重定向遵守调用URL的协议。(HTTPS/HTTP)

如何强制RequestMapping重定向遵守调用URL的协议。(HTTPS/HTTP)
EN

Stack Overflow用户
提问于 2019-06-21 00:45:16
回答 1查看 294关注 0票数 0

场景:

我有一个swagger-ui页面的重定向,以使其更容易到达。(骆驼让我伤感)

当我在本地运行时,我使用HTTP。

当我在测试服务器中运行时,我使用HTTPS。

或者,我也可以通过配置camel流(xml)来更改swagger页面的默认url。

预期- (https重定向到https)和(http重定向到http):

http://localhost:8080/swagger-ui --> http://localhost:8080/webjars/swagger-ui/index.html?url=/api/swagger&validatorUrl= https://remote.com/swagger-ui --> https://remote.com/webjars/swagger-ui/index.html?url=/api/swagger&validatorUrl=

Actual - https & http重定向到http:

http://localhost:8080/swagger-ui --> http://localhost:8080/webjars/swagger-ui/index.html?url=/api/swagger&validatorUrl= https://remote.com/swagger-ui --> http://remote.com/webjars/swagger-ui/index.html?url=/api/swagger&validatorUrl=

代码:

代码语言:javascript
运行
复制
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SwaggerController {

    @RequestMapping("/swagger-ui")
    public String redirectToUi() {
        return "redirect:/webjars/swagger-ui/index.html?url=/api/swagger&validatorUrl=";
    }

    @RequestMapping("/swagger-ui.html")
    public String redirectToUi2() {
        return "redirect:/webjars/swagger-ui/index.html?url=/api/swagger&validatorUrl=";
    }
}
代码语言:javascript
运行
复制
        <restConfiguration
                component="servlet"
                apiContextPath="swagger"
                contextPath="api"
                enableCORS="true"
                bindingMode="json">
            <dataFormatProperty key="prettyPrint" value="true"/>

            <!-- setup swagger api description -->
            <apiProperty key="base.path" value="api"/>
            <apiProperty key="api.version" value=".0.0.1"/>
            <apiProperty key="api.title" value="Some Forms"/>
            <apiProperty key="api.description" value="Description Here"/>
            <apiProperty key="api.contact.name" value="Contact here"/>
        </restConfiguration>
EN

回答 1

Stack Overflow用户

发布于 2019-06-22 00:05:28

这是因为逻辑视图名称(例如" redirect :/myapp/some/resource")会将您的调用重定向到当前的Servlet上下文,在您的例子中,上下文的方案是HTTP。

您需要为这种情况实现不同的上下文(一个用于HTTP,另一个用于HTTPS),然后您可以使用camel-servlet代理调用

上下文1- http:

代码语言:javascript
运行
复制
from("servlet:myapp?matchOnUriPrefix=true")
.to("http://newURL?bridgeEndpoint=true&throwExceptionOnFailure=false")

上下文2- https:

代码语言:javascript
运行
复制
from("servlet:myapp?matchOnUriPrefix=true")
.to("https://newURL?bridgeEndpoint=true&throwExceptionOnFailure=false")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56690542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档