如何在春天添加拦截器

时间:2011-11-07 05:41:51

标签: java spring spring-mvc spring-security

我正在使用Spring Security 3.0.3进行项目。我的用户信息是从数据库加载的。我有以下拦截器

<intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
<intercept-url pattern="/**" access="ROLE_USER"/>

我想在用户数据中添加拦截器。当我使用user1(请求者作为角色)进行日志记录时,他只能看到特定的user1数据。

http://localhost:7009/Test/requester//30351?menuId=app.requester.new

但是当我使用另一个user2(请求者作为角色)并在浏览器中输入上述URL时。

http://localhost:7009/Test/requester//30351?menuId=app.requester.new. 

他可以看到user1数据。

如何添加拦截器以限制其他用户数据。

以下是场景 1 GT; 用user1登录,他可以看到以下数据列表 101 102 103 当我点击102个数据时,它会打开102个数据的详细信息。

http://localhost:7009/Test/requester//102?menuId=app.requester.new. 

2 - ;用user2登录,他可以看到以下数据列表 104 105 106

当我点击105个数据时,它会打开105个数据的详细信息。

http://localhost:7009/Test/requester//105?menuId=app.requester.new.

但是当我复制user1链接

 http://localhost:7009/Test/requester//102?menuId=app.requester.new.  

并粘贴到浏览器中。 user2可以查看user1数据的详细信息。

2 个答案:

答案 0 :(得分:0)

您不会过滤Spring安全配置中的所有内容,而是在视图或控制器中进行过滤。这是一个非常好的教程,向您展示如何在您的案例中使用spring security。

http://static.springsource.org/spring-security/site/petclinic-tutorial.html

如果您使用JSP作为视图,则可以使用spring security taglib来处理用户自己的数据视图,对于其他用户的数据,您需要将过滤逻辑放在控制器中并将它们作为模型传递给视图。但是,确切的解决方案取决于您的应用程序。

答案 1 :(得分:0)

执行此操作的一个选项是使用Spring Expression Language和自定义权限评估程序。以下是我们为实施部门/子部门授权计划how to control the user authentication using spring mvc所做的工作。这将允许您创建自定义逻辑以识别允许用户1查看的内容等。

结帐http://static.springsource.org/spring-security/site/docs/3.0.x/reference/el-access.html。查看@PostFilter注释 - 如果您的数据集作为集合或数组返回,Spring Security可以根据您的自定义权限评估程序自动过滤掉数据元素。