1. 如何在同一浏览器的下一个标签页中找到您的网站正在受到攻击? 2. 如何防止网站在第二个标签页中被打开?
1. 如何在同一浏览器的下一个标签页中找到您的网站正在受到攻击? 2. 如何防止网站在第二个标签页中被打开?
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>Hello,
<c:choose>
<c:when test="${not empty param.name}">
<c:out value="${param.name}" />.
</c:when>
<c:otherwise>
stranger.
</c:otherwise>
</c:choose>
</p>
<form method="post">
<label>Please enter your name</label>
<input id="name" name="name" type="text"/>
<input id="marker" name="marker" type="hidden"
value="<c:out value="${sessionScope.marker}"/>"/>
<button type="submit">OK</button>
</form>
</body>
</html>
这个JSP正在请求一个名称。表单提交通过POST调用相同的JSP。隐藏字段填充了来自会话的值。
index.jsp
:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!--include the library-->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:redirect url="protected/MyJsp.jsp"/>
@WebFilter("/protected/*")
public class OneTabFilter implements Filter {
private static final String MARKER_NAME = "marker";
private static final String MARKER_VALUE = "4711*0815";
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request;
final HttpServletResponse rsp = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
if(session == null) {
session = req.getSession(true);
// Put the marker value into session so it is usable in JSP files.
session.setAttribute(MARKER_NAME, MARKER_VALUE);
// pass the request along the filter chain
chain.doFilter(request, response);
} else {
if(MARKER_VALUE.equals(req.getParameter(MARKER_NAME))) {
// pass the request along the filter chain
chain.doFilter(request, response);
} else {
// Redirect to the error page.
// The error page itself is not affected by this filter.
rsp.sendRedirect(req.getServletContext().getContextPath() + "/error.jsp");
}
}
}
// ...
}
自己动手试试吧!