gpt4 book ai didi

java - 在 Spring MVC 中成功登录后,如果按下浏览器后退按钮,如何防止返回登录表单?

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:02 26 4
gpt4 key购买 nike

Controller 类:

package com.ym.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class CustomerController {

@RequestMapping(value="/home")
public String goHome(){
return "home";
}

@RequestMapping(value="/first")
public String first(HttpSession session){
if(session.getAttribute("visited") != null){
return "home";
}
else{
return "form";
}
}

@RequestMapping(value="/second", method=RequestMethod.POST)
public String second(HttpSession session){
session.setAttribute("visited", true);
return "home";
}
}

表单.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">@import url("<c:url value="/css/main.css"/>");
</style>

<title>Login Form</title>
</head>

<body>
<form:form action="second" method="post">

<p>
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5">
</p>
</form:form>

</body>
</html>

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<a href="<c:url value="/first"/>">Go to form page</a>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


</web-app>

springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.ym.controller"/>
<context:component-scan base-package="com.ym.service"/>

<mvc:annotation-driven/>
<mvc:resources mapping="/css/**" location="/css/"/>


<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

所以流程是,首先我们进入“/home”。在home.jsp中,有一个指向“/first”请求处理方法的链接,该方法会检查session的“visited”属性,如果不为null则返回form.jsp,其中我们将session的“visited”属性设置为true,否则返回home.jsp。问题是,即使将 session 属性“visited”设置为 true,我仍然可以通过单击浏览器中的后退按钮返回到 form.jsp,但如果我在 URL 栏中输入 http://localhost:9001/ExperimentProject1/first ,我将被重定向到 home.jsp,因为确实“visited”属性已设置为 true。有人可以解释一下这里发生了什么吗?为什么单击后退按钮与手动输入 URI 具有不同的效果?这只是登录机制的类比。

最佳答案

使用“redirect:home”而不是“home”。这可能是最著名的方法。

关于java - 在 Spring MVC 中成功登录后,如果按下浏览器后退按钮,如何防止返回登录表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46477636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com