Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring @PreAuthorize如何检查角色?_Spring_Spring Security - Fatal编程技术网

Spring @PreAuthorize如何检查角色?

Spring @PreAuthorize如何检查角色?,spring,spring-security,Spring,Spring Security,我有几个RESTAPI 我想把安全性放在所有这些之上,以便只允许某些角色使用这些功能 我将@EnableGlobalMethodSecurity(preprestenabled=true)放在配置类中 我有一个数据库和一个存储角色的表 @RequestMapping(path = "/error", method = RequestMethod.GET) @PreAuthorize("hasRole('ROLE_ADMIN')") public ResponseEntity<Respon

我有几个RESTAPI

我想把安全性放在所有这些之上,以便只允许某些角色使用这些功能

我将@EnableGlobalMethodSecurity(preprestenabled=true)放在配置类中

我有一个数据库和一个存储角色的表

@RequestMapping(path = "/error", method = RequestMethod.GET)
@PreAuthorize("hasRole('ROLE_ADMIN')") 
public ResponseEntity<ResponseWrapper> getError(...


INSERT INTO table USER_ROLE VALUES ('ADMIN','PASSWORD','ROLE_ADMIN');
@RequestMapping(path=“/error”,method=RequestMethod.GET)
@预授权(“hasRole('ROLE_ADMIN'))
公共响应性getError(。。。
在表中插入用户角色值('ADMIN'、'PASSWORD'、'ROLE\u ADMIN');
方法“hasRole”如何查询表用户角色?
为了与该表通信,我是否必须为Spring提供任何设置?

确保在配置中添加以下内容:

注释样式:

@EnableGlobalMethodSecurity(prePostEnabled = true)
XML样式:

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

使用
@Pre
@Post
注释要求您首先在项目中设置spring安全性。用户角色在标准实现的身份验证过程中加载

在检查注释时,它不会检查您的表(默认情况下),它只是检查内存中的
主体
(用户)对象,以查看已分配的
授权
(角色)列表

仔细看

看看这个界面,它返回了一个(主体)的实例。这就是我们从数据库中获取“用户”的地方


UserDetails
接口定义了一个名为
getAuthorities()的方法
返回一个
集合基本上,我想了解@PreAuthorize如何与存储多个角色的表进行通信。我将注释放在配置文件中。我不知道该角色是用户的参数,还是请求的参数,或者我是否必须设置一些东西来安排通信在“hasrole”方法和表之间的关系…您必须设置类似于我现在在答案中解释的内容。