最近,笔者学习了ssh之后瞬间觉得世界的美好,因为,对比之前简陋的servlet,ssh显然用的更为方便,功能也更为强大,但是,昨日,笔者遇到了一个问题,在利用ssh搭建一个User管理系统时,管理员界面是http://localhost:8080/SSH_03/User/list,能够在没有管理员登录的情况下,只向cherome输入以上地址就可以访问数据库,这是相当危险的!!因为遍历数据库采用的是struts的标签
<s:iterator>
进行遍历,但不知为何,在不触发list的方法下,值栈中并没有users,关于这一点我也很郁闷,所以之前一直使用
<a href="list">查阅</a>
将users导入进list.jsp导致了这一安全问题,关于这个问题我现在先对它进行详细的阐述,稍后再提出我自己的解决方案
<table border="1" width = "1000" cellspacing="0">
<tr><th>姓名</th><th>密码</th><th>id</th><th colspan="2" >操作</th></tr>
<s:iterator value = "users" id = "user">
<tr>
<td><s:property value = "#user.name"/></td>
<td><s:property value = "#user.pwd"/></td>
<td><s:property value = "#user.id"/></td>
<td><a href = "delete?id=<s:property value = "#user.id"/>">删除</a> </td>
<td><a href = "updateinput?id=<s:property value = "#user.id"/>">更新</a></td>
</tr>
</s:iterator>
</table>
这是我的list.jsp
public String list(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceInter usi = (UserServiceInter) ac.getBean("userService");
users = usi.list();
return SUCCESS;
}
这是UserAction中关于遍历的方法,其中
private List<User> users;
私以为在list.jsp应该会出现数据库中相应的数据的,然而,并没有,所以在前期我在list.jsp加入了一段代码<a href="list">查阅</a>
对应struts.xml
随后问题就出现了,如同我开始所述,最终解决的方案是在list.jsp创建一个隐藏表,输入id号,再在UserAction对id号进行校验,如果正确就遍历,这样就解决了问题
<action name="list" class = "com.bj.action.UserAction" method = "list">
<result>/User/list.jsp</result>
</action>
<form action="list" method = "post">
<input type = "hidden" value = "520" name = "id"/>
<input type = "submit" value = "查询"/>
</form>
对应的UserAction变为
` public String list(){
if (id==520){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceInter usi = (UserServiceInter) ac.getBean("userService");
users = usi.list();
return SUCCESS;
}else{
return "error";
}
}
`
问题解决,以上。
+++++++更新++++++++
今天看书才知道,其实可以将返回页面的jsp文件放在工程的web-inf目录下,就可以实现角本搜索禁止了T.T