解决Tomcat+SpringMVC+Shiro redirect重定向失败问题

问题的起因:

生产环境项目编辑用户信息后,发现编辑完后没办法跳转页面,一直停留在当前页面,看代码发现是有通过 redirect: /xxx/xxx 重定向跳转页面,但明显不起作用了

排查问题的心路历程:

1、看了接口请求,是有生成Location的,但是明显使用的协议不一样,Request URL是https,而Location确实http 

6d634d2797a64f338a97aa5cc5b36989.png

2、查阅网上许多文章,都说是配置springMVC的属性redirectHttp10Compatible为false,默认true是使用HTTP 1.0协议,即http。

无奈水土不服,配置后还是不行,重定向链接依然是http

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/static/views/"/>
    <property name="suffix" value=".jsp"/>
    <property name="redirectHttp10Compatible" value="false"/>
</bean>

3、再去网上狂翻答案,拜读到一篇似乎可以解决的文章(链接:一个HTTPS转HTTP的Bug,他们竟然忍了2年?原谅我无法接受,加班改了! - 知乎),刚好也是用了Shiro的FormAuthenticationFilter,怀疑WebUtils的issueRedirect方法中设置http10Compatible为true,但应该影响登录的跳转,不影响其它业务才对,抱着疑问试了,也是解决不了。

4、最终debug代码,发现Location的协议是从request.getScheme()中获取的,于是又去网上翻答案,终于解决问题,感谢伟大的网友!(问题解决:通过https访问页面,request.getScheme()获取到的却是http_getscheme总是读取到http-CSDN博客

由于一开始解决问题的切入点关注在springMVC上面,忽略了线上用的是外部Tomcat,不同于springboot的集成Tomcat,因此线上环境Tomcat的server.xml配置还需要另外增加 scheme="https" secure="true" 配置

<Connector port="8080" protocol="HTTP/1.1" 
    connectionTimeout="20000"
    URIEncoding='UTF-8' 
    scheme="https" secure="true"/>

至此问题fix了,done!!!!!!

好的,以下是基于传统的TomcatSpring MVC和WebSocket的简单示例: 1. 首先,在pom.xml文件中添加以下依赖: ```xml <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- WebSocket --> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-websocket</artifactId> <version>9.0.30</version> </dependency> ``` 2. 接下来,创建一个WebSocket端点类,例如: ```java import javax.websocket.OnMessage; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; @ServerEndpoint("/websocket") public class MyWebSocketEndpoint { @OnMessage public void onMessage(String message, Session session) { try { session.getBasicRemote().sendText("Received: " + message); } catch (Exception e) { e.printStackTrace(); } } } ``` 在这个例子中,我们创建了一个名为`MyWebSocketEndpoint`的WebSocket端点,并将其映射到`/websocket`路径。`onMessage`方法用于处理接收到的消息并回传回去。 3. 然后,在Spring MVC控制器中,我们可以使用`WebSocketContainer`将WebSocket连接注入到请求处理方法中,例如: ```java import javax.servlet.http.HttpServletRequest; import javax.websocket.Session; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { private WebSocketContainer webSocketContainer; public MyController(WebSocketContainer webSocketContainer) { this.webSocketContainer = webSocketContainer; } @RequestMapping(value = "/websocket-test", method = RequestMethod.GET) @ResponseBody public String testWebSocket(HttpServletRequest request) { try { Session session = webSocketContainer.connectToServer(MyWebSocketEndpoint.class, request.getRequestURI()); session.getBasicRemote().sendText("Test message"); session.close(); return "Message sent"; } catch (Exception e) { e.printStackTrace(); return "Error"; } } } ``` 在这个例子中,我们注入了一个名为`webSocketContainer`的`WebSocketContainer`,并在请求处理方法中使用它来创建WebSocket连接并发送测试消息。 这就是一个简单的TomcatSpring MVC和WebSocket示例。它可以让你开始理解如何使用这些技术来开发WebSocket应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值