Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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
Java Spring安全控制访问-同一页面,但参数id不同_Java_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Java Spring安全控制访问-同一页面,但参数id不同

Java Spring安全控制访问-同一页面,但参数id不同,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,假设我有两名员工,他们的信息不同,url看起来像 http://localhost:8080/springtest/{empId} 并使用empInfo.jsp页面 id为1的员工登录系统时,他可以访问,但不能访问,反之亦然 使用Spring实现这一目标的最佳实践应该是什么?两名员工的角色都是“用户”您可以创建一个具有角色的组。然后将用户添加到具有不同角色的不同组 角色:R 组别:G 用户:U m2m:多对多关系 R m2m G G m2m U 基本上,在角色之后再添加一层最好的方法是在jav

假设我有两名员工,他们的信息不同,url看起来像

http://localhost:8080/springtest/{empId}
并使用empInfo.jsp页面

id为1的员工登录系统时,他可以访问,但不能访问,反之亦然


使用Spring实现这一目标的最佳实践应该是什么?两名员工的角色都是“用户”

您可以创建一个具有角色的组。然后将用户添加到具有不同角色的不同组

角色:R 组别:G 用户:U m2m:多对多关系

R m2m G

G m2m U


基本上,在角色之后再添加一层

最好的方法是在java控制器中添加一些安全控件,我给您一个算法示例:

If the current user id equals to the parameter id 
    then redirect to the good page
    else redirect to 403 Forbidden page or Another error page

Spring Security为您提供了一种检索主体(即当前登录的用户)的方法

User principal = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
现在,您可以基于这些属性执行所有安全检查,这些属性描述了用户之间的关系。比如一个用户ID,或者其他什么

在您的情况下,您可以执行以下操作

    if(principal.getId()==empId)
        //PROCEED...
    else
        //STOP! GO TO 403!

谢谢你的帮助:)