GVKun编程网logo

在spring@Secured与@RolesAllowed之间的区别?以及基于角色的安全性的概念?(spring role)

14

在本文中,我们将给您介绍关于在spring@Secured与@RolesAllowed之间的区别?以及基于角色的安全性的概念?的详细内容,并且为您解答springrole的相关问题,此外,我们还将为您

在本文中,我们将给您介绍关于在spring@Secured与@RolesAllowed之间的区别?以及基于角色的安全性的概念?的详细内容,并且为您解答spring role的相关问题,此外,我们还将为您提供关于@RolesAllowed与@PreAuthorize与@Secured、@Secured()、 @PreAuthorize() 、 @RolesAllowed()、Color.red和Color.RED之间的区别、java – *和*之间的区别?在Spring @Scheduled(cron =“…”)的知识。

本文目录一览:

在spring@Secured与@RolesAllowed之间的区别?以及基于角色的安全性的概念?(spring role)

在spring@Secured与@RolesAllowed之间的区别?以及基于角色的安全性的概念?(spring role)

我正在研究Spring Security,并且对使用 @Secured 注释和 @RolesAllowed 注释之间的区别存在以下疑问。

我知道两者都必须在 方法级别使用 ,在我的学习资料中,我发现了以下两个示例:

  • @RolesAllowed 批注:
import javax.annotation.security.RolesAllowed;    public class ItemManager {@RolesAllowed("ROLE_MEMBER")public Item findItem(long itemNumber) {    ...}}
  • @安全 注释:
import org.springframework.security.annotation.Secured;public class ItemManager {@Secured("ROLE_MEMBER")public Item findItem(long itemNumber) {    ...}}

在我看来,这两个注释的工作方式相同。有什么区别?我想念什么?

我的另一个疑问是: ROLE_MEMBER 到底代表什么?

我认为这类似于 基于角色的安全性 ,因此它的含义可能类似于: 仅当用户是成员时,它才能访问带注释的资源
(对吗?)。但是,在何处以及如何定义用户设置此角色(它是成员)的事实呢?究竟如何运作?

特纳克斯

答案1

小编典典

@Secured@RolesAllowed一样。他们在Spring中执行相同的操作。

  • @RolesAllowed -Java的标准注释。

Java已经定义了Java Specification Request,基本上改变了对Java语言,库和其他组件的请求。 为了开发注释,他们提供了JSR
250 @RolesAllowed。此链接包含JSR
250中的更多信息

  • @Secured -Spring安全注释

ROLE_MEMBER 是设置为安全用户详细信息的角色。

请从我当前的项目中参考此示例。在这里,我正在使用用户数据对象并将分配给用户的角色映射到安全用户详细信息。

public class CustomUserDetails implements UserDetails {.........    @Override    public Collection<? extends GrantedAuthority> getAuthorities() {        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();        for (Role role : this.user.getRoles()){            grantedAuthorities.add(new SimpleGrantedAuthority(role.getRole()));        }        return grantedAuthorities;    }}

然后使用@Secured@RolesAllowed@PreAuthorize("hasRole(''ROLE_USER'')")方法为安全批准设置这些角色。

通过设计,最好将安全性放在服务层中。因此,当我保护我的服务动作时,我将检查角色,而不是用户。

这样,我们可以通过称为角色的小型安全单元将精力集中在业务逻辑和业务逻辑的安全性上。

然后,我将角色分配给用户。用户可以具有多个角色。因此,您必须在这里查看关系。为用户分配了角色。并授予角色访问业务逻辑的权限。通过角色向用户授予对业务逻辑的访问权限。这个概念称为基于角色的访问控制。

在复杂的情况下,我们还可以管理分层角色。
其中一个角色还有许多其他角色。但是在UserDetails中,我们必须展平角色层次结构,并将角色列表提供给Spring框架进行处理。

@RolesAllowed与@PreAuthorize与@Secured

@RolesAllowed与@PreAuthorize与@Secured

我有一个基本的SpringBoot应用程序。使用Spring
Initializer,嵌入式Tomcat,Thymeleaf模板引擎以及作为可执行JAR文件的软件包。

我想保护控制器:

@Controller@RequestMapping("/company")@RolesAllowed({"ROLE_ADMIN"})@PreAuthorize("hasRole(''ADMIN'')")@Secured("ADMIN")public class CompanyController {}

我知道有不同的选择,但我真的不知道应该使用哪个选项

答案1

小编典典

安全注释

所有的@PreAuthorize@RolesAllowed@Secured有注释,允许配置 方法的安全性
。它们既可以应用于单个方法,也可以应用于类级别,在后一种情况下,安全性约束将应用于类中的所有方法。

使用Spring
AOP代理可以实现方法级的安全性。

@PreAuthorize

@PreAuthorize 注解允许使用 Spring Expression Language(SpEL)
指定对方法的访问约束。这些约束是在执行方法之前进行评估的,如果约束不满足,可能会导致方法的执行被拒绝。该@PreAuthorize注释是Spring安全框架的一部分。

为了能够使用@PreAuthorize,注释中的 prePostEnabled 属性
@EnableGlobalMethodSecurity需要设置为true

@EnableGlobalMethodSecurity(prePostEnabled=true)

@RolesAllowed

@RolesAllowed 注解起源于JSR-250
Java安全标准。此注释比注释 更受限制@PreAuthorize因为它 仅支持基于角色的安全性

为了使用@RolesAllowed注释,包含该注释的库必须在类路径中,因为它不是Spring Security的一部分。另外,注释的
jsr250Enabled 属性@EnableGlobalMethodSecurity需要设置为true

@EnableGlobalMethodSecurity(jsr250Enabled=true)

@Secured

@Secured 注解是 旧版Spring Security 2注解
,可用于配置方法安全性。它不仅支持基于角色的安全性,而且不支持使用Spring Expression
Language(SpEL)指定安全性约束。建议@PreAuthorize在新应用程序中使用该注释之上的注释。

为支持@Secured需要在明确启用注释 @EnableGlobalMethodSecurity使用注释 securedEnabled
属性:

@EnableGlobalMethodSecurity(securedEnabled=true)

哪些安全注释允许使用SpEL

下表显示了可与Spring Security 5一起使用的安全注释中对Spring Expression Language的支持:

╔═════════════════════╦═══════════════════╗║ Security Annotation ║ Has SpEL Support? ║╠═════════════════════╬═══════════════════╣║  @PreAuthorize      ║        yes        ║╠═════════════════════╬═══════════════════╣║  @PostAuthorize     ║        yes        ║╠═════════════════════╬═══════════════════╣║  @PreFilter         ║        yes        ║╠═════════════════════╬═══════════════════╣║  @PostFilter        ║        yes        ║╠═════════════════════╬═══════════════════╣║  @Secured           ║        no         ║╠═════════════════════╬═══════════════════╣║  @RolesAllowed      ║        no         ║╚═════════════════════╩═══════════════════╝

@Secured()、 @PreAuthorize() 、 @RolesAllowed()

@Secured()、 @PreAuthorize() 、 @RolesAllowed()

在Spring security的使用中,为了对方法进行权限控制,通常采用的三个注解,就是@Secured()、@PreAuthorize()、@RolesAllowed()。

示例,修改用户密码必须是ADMIN权限,可以用三种方法:

@Secured({"ROLE_ADMIN"})
public void changePassword(String username, String password);

@RolesAllowed({"ROLE_ADMIN"})
public void changePassword(String username, String password);

@PreAuthorize("hasRole(‘ROLE_ADMIN‘)")
public void changePassword(String username, String password);

1. @Secured(): secured_annotation

使用前配置Spring Security (无论是xml方式,还是Spring boot注解方式,都需要指定secured-annotations)

XML: <global-method-security secured-annotations="enabled"/>

Spring boot: @EnableGlobalMethodSecurity(securedEnabled = true)

2. @RolesAllowed(): jsr250-annotations

使用前配置Spring Security (无论是xml方式,还是Spring boot注解方式,都需要指定jsr250-annotations)

XML: <global-method-security jsr250-annotations="enabled"/>

Spring boot: @EnableGlobalMethodSecurity(jsr250Enabled = true)

3. @PreAuthorize(): pre-post-annotations

使用前配置Spring Security (无论是xml方式,还是Spring boot注解方式,都需要指定pre-post-annotations)

XML: <global-method-security pre-post-annotations="enabled"/>

Spring boot: @EnableGlobalMethodSecurity(prePostEnabled = true)

 

Color.red和Color.RED之间的区别

Color.red和Color.RED之间的区别

什么是用于定义之间的真正区别setXxx(Color.red)setXxx(Color.RED)

我在网上找到了以下说明。是否所有有关命名约定的内容?

Java最初用小写字母定义了一些颜色常量名称,这违反了将大写字母用作常量的命名规则。它们在Java的所有版本中可用:Color.black,Color.darkGray,Color.gray,Color.lightGray,Color.white,Color.magenta,Color.red,Color.pink,Color.orange,Color.yellow,Color
.green,Color.cyan,Color.blue

Java
1.4为常量添加了适当的大写名称:Color.BLACK,Color.DARK_GRAY,Color.GRAY,Color.LIGHT_GRAY,Color.WHITE,Color.MAGENTA,Color.RED,Color.PINK,Color.ORANGE,Color.YELLOW,颜色:绿色,颜色青色,颜色蓝色

答案1

小编典典

有代码本身:

public final static Color red = new Color(255, 0, 0);public final static Color RED = red;

大写字母在JDK 1.4中引入(为了符合其命名约定,说明常量 必须 为大写)。

本质上,没有任何区别(字母大写除外)。


如果我真的很勇敢,Oracle可能会疯狂并删除小写的常量,但这会破坏在JDK
1.4之前编写的所有其他代码。您永远不会知道,我建议对常量使用大写字母。不过,它首先必须被弃用(如Andrew Thompson所述)。

java – *和*之间的区别?在Spring @Scheduled(cron =“…”)

java – *和*之间的区别?在Spring @Scheduled(cron =“…”)

我一直在查看 Spring Boot示例来安排任务( https://spring.io/guides/gs/scheduling-tasks/)并阅读一些文档( https://javahunter.wordpress.com/2011/05/05/cronscheduler-in-spring/),我看到了*和?几乎互换使用.

例如,行

@Scheduled(cron = "0 15 10 ? * *")

@Scheduled(cron = "0 15 10 * * ?")

做同样的事情.那么*和?之间有什么区别?

解决方法

asterix代表所有可能的值.问号应用于非特定值

*(“all values”) – used to select all values within a field. For example,“” in the minute field means *”every minute”.

? (“no specific value”) – useful when you need to specify something in
one of the two fields in which the character is allowed,but not the
other. For example,if I want my trigger to fire on a particular day
of the month (say,the 10th),but don’t care what day of the week that
happens to be,I would put “10” in the day-of-month field,and “?” in
the day-of-week field. See the examples below for clarification.

从tutorial复制

关于在spring@Secured与@RolesAllowed之间的区别?以及基于角色的安全性的概念?spring role的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于@RolesAllowed与@PreAuthorize与@Secured、@Secured()、 @PreAuthorize() 、 @RolesAllowed()、Color.red和Color.RED之间的区别、java – *和*之间的区别?在Spring @Scheduled(cron =“…”)等相关内容,可以在本站寻找。

本文标签: