简体   繁体   English

Spring MVC + Hibernate列表对象并检查权限

[英]Spring MVC + Hibernate list object and check permissions

I'm using Spring MVC and Hibernate. 我正在使用Spring MVC和Hibernate。

Let's say we have got controller action 假设我们有控制器动作

@GetMapping("/user/remove")
public String removeAction(@ModelAttribute(value = "user_id") User user, RedirectAttributes redirectAttributes) {
    ...
}

Is there possible to apply some rules that check if user can delete that user? 是否可以应用一些规则来检查用户是否可以删除该用户? For example user with role ROLE_ADMIN can delete all users, and user with role ROLE_USER can delete that users that were created by them self. 例如,具有角色ROLE_ADMIN用户可以删除所有用户,而具有角色ROLE_USER用户可以删除由他们自己创建的用户。

And second question when user with role ROLE_USER is listing all users it it possible to filter that list on Hibernate level without iterating over all users and checking who created that user? 还有第二个问题,当具有角色ROLE_USER用户列出所有用户时,可以在Hibernate级别上过滤该列表,而无需遍历所有用户并检查谁创建了该用户?

Last question. 最后一个问题。 To removeAction I'm passing user_id. 要删除动作,我要传递user_id。 It is possible to retrieve that User from DB without calling UserService, and if all permission are OK then would like that User object is available in that removeAction. 可以在不调用UserService的情况下从数据库检索该用户,并且如果所有权限都正确,则希望该User对象在该removeAction中可用。

Spring has a annotation type security that could be used for your first question, albeit that you have your users set up for roles, see a link for the method @Secured("ROLE_ADMIN") Annotation Type Secured Spring有一个注释类型的安全性,可以用于你的第一个问题,虽然你有你的用户设置了角色,看到一个链接@Secured方法(“ROLE_ADMIN”) 中的注释类型担保

You might need to set some additional logic to your database entries to show who created the users, you could have this done already this would allow you to separate out the list depending on who created it maybe username and roles could be option, this might help question 2. Question 3 you could use Jstl to connect to your database or even JQuery, tell us a little bit more about your environment set up, hopefully this gives you some direction to proceed. 您可能需要为数据库条目设置一些其他逻辑以显示谁创建了用户,您可能已经完成了此操作,这将允许您根据创建它的人员将列表分开,也许用户名和角色可以选择,这可能会有所帮助问题2。问题3您可以使用Jstl连接到数据库甚至JQuery,向我们介绍更多有关您的环境设置的信息,希望这可以为您提供一些指导。 Happy coding 快乐编码

One option would be to use the @RolesAllowed or the @Secured annotations. 一种选择是使用@RolesAllowed@Secured批注。 Both essentially do the same thing According to this post 这两个基本做同样的事情。根据这个帖子

They can be applied either at the class level or the method level. 它们可以在类级别或方法级别应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM