需求描述如下:
2台机器部署了2个web应用,A应用需要访问B应用的URL。为了保证URL不会让任意用户随便粘贴就可以访问,需要在B应用上加上filter拦截请求,并进行权限校验。A应用的URL给用户看来是一个中间跳转页面的URL。在这个中间页面,添加hidden的value,在B应用的filter端进行value的校验。代码如下:
<%@ page language="java" contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function init(){
document.getElementById('myForm').action="http://localhost:8080/ext2.2/Filter.jsp"
document.getElementById('myForm').submit();
}
</script>
</head>
<body οnlοad="init()">
<form method="post" id="myForm">
<input type="hidden" name="key" id="key" value="MERKTLTTOR">
</form>
</body>
</html>
<%@ page language="java" contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function init (){
<%
String key=request.getParameter("key");
if(!"MERKTLTTOR".equals(key)){
%>
alert('不允许访问');
<%
}
%>
}
</script>
</head>
<body οnlοad="init()">
<form method="post" id="myForm">
<input type="hidden" name="key" id="key" value="MERKTLTTOR">
</form>
</body>
</html>
这是filter页面,实际中可以是真正的过滤器filter。
中间页面采用post提交,用户在url中看不到提交的hidden。
中间页面的form的action可以用request.getParamter()获取
当然value可以采用一些加密算法进行加密。