简体   繁体   English

在Spring Security中使用PreAuthorize注释中的permitAll()的目的

[英]Purpose of using permitAll() in PreAuthorize annotation in Spring Security

Being new to spring security framework, I wanted to know why do we use @PreAuthorize("permitAll()") with methods ? 作为Spring安全框架的新手,我想知道为什么我们使用@PreAuthorize("permitAll()")和方法? The documentation says that permitAll always evaluates to true. 文档说permitAll总是评估为true。 ( http://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html ) http://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html

Also, I have the below code change. 另外,我有以下代码更改。 The developer makes change from permitAll() to specific permission check.What is the implication here? 开发人员从permitAll()更改为特定权限检查。这里的含义是什么? Since I am not too sure about how permitAll() works, I am not able to judge the logic behind the code change. 由于我不太确定permitAll()如何工作,我无法判断代码更改背后的逻辑。 It seems to me that the developer adds specific permission checks and he passes null as the authentication object. 在我看来,开发人员添加了特定的权限检查,并将null作为身份验证对象传递。 Could someone explain what is the impact of explicitly passing null as the authentication object? 有人可以解释显式传递null作为身份验证对象的影响吗? Is it that users who are not authenticated will have access if they have this specific - 'LUONTI' permission on the target object - 'opetussuunnitelma' ? 未经身份验证的用户是否具有访问权限,如果他们对目标对象具有此特定的“LUONTI”权限 - 'opetussuunnitelma'?

-    @PreAuthorize("permitAll()")
+    @PreAuthorize("hasPermission(null, 'opetussuunnitelma', 'LUONTI')")
     OpetussuunnitelmaDto addOpetussuunnitelma(OpetussuunnitelmaDto opetussuunnitelmaDto);

Thanks. 谢谢。 Any help much appreciated! 任何帮助非常感谢!

permitAll() does exactly what it says. permitAll()完全按照它说的做。 It allows (permits) any user's (all) session to be authorized to execute that method. 它允许(允许)任何用户(所有)会话被授权执行该方法。

The way spring manages its authentication and authorization means that anyone accessing your site is provided with a session. spring管理其身份验证和授权的方式意味着访问您站点的任何人都会收到会话。 This session can be anonymous, or authenticated (user's provided some kind of credential and the system has accepted it). 此会话可以是匿名的,也可以是经过身份验证的(用户提供了某种凭据,系统已接受它)。 Alternatives to permitAll ( hasPermission() for example) will usually check the user's authentication to ensure they have some role or group assigned to them before allowing the annotated class/method to be invoked. permitAll替代permitAll (例如hasPermission() )通常会检查用户的身份验证,以确保在允许调用带注释的类/方法之前,他们已经为其分配了一些角色或组。

If permitAll() is used, it means to explicitly allow any session, anonymous or authenticated, to access the annotated method. 如果使用permitAll() ,则意味着明确允许任何匿名或经过身份验证的会话访问带注释的方法。

The code change the other developer has made has restricted the given method to something custom. 其他开发人员所做的代码更改已将给定方法限制为自定义。 Take a look at this Spring - Expression-Based Access Control 看看这个Spring - 基于表达式的访问控制

I feel like nobody really gave you what you really wanted, which is a use case for "permitAll()". 我觉得没有人真正给你你真正想要的东西,这是“permitAll()”的用例。

It can be used when you restrict your whole class or application with a certain permission, for example : @PreAuthorize("hasAuthority('USER')") 当您使用特定权限限制整个班级或应用程序时,可以使用它,例如: @PreAuthorize("hasAuthority('USER')")

Here, only the clients identified as what you defined to be a user can have access to the methods of your class. 在这里,只有标识为您定义为用户的客户端才能访问您的类的方法。

But at some point in your controller you want a certain method to be permissionless, so you'll add @PreAuthorize("permitAll()") to your method so that it override the global permission. 但是在控制器中的某个时刻,您希望某个方法是无权限的,因此您将@PreAuthorize("permitAll()")到您的方法中,以便它覆盖全局权限。

People will do this because it's safer to first secure everything with the highest permission lock and then poke holes in the net (eg, the application/class is locked to ADMIN but most methods are then authorized to USER) than the other way around. 人们会这样做,因为首先使用最高权限锁保护所有内容然后在网络中挖洞(例如,应用程序/类被锁定为ADMIN但大多数方法被授权给USER)比使用其他方式更安全。 Because if everything is unlocked by default, the day you forget to lock a controller you could have security problems. 因为如果默认情况下一切都已解锁,那么当您忘记锁定控制器时,您可能会遇到安全问题。

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

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