假设我有两个拥有不同信息的员工,url看起来就像
http://localhost:8080/springtest/{empId}并使用empInfo.jsp页面。
使用id 1登录到系统时,他可以访问http://localhost:8080/springtest/1,但不能访问http://localhost:8080/springtest/2,反之亦然。
用Spring实现这一目标的最佳实践应该是什么?这两个雇员的角色都是“用户”。
发布于 2016-02-19 15:49:55
Security为您提供了检索主体(即当前登录的用户)的方法。
User principal = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();现在,您可以根据用户之间的属性进行所有的安全检查。例如,用户标识,或者其他什么。
在您的例子中,您可以执行以下操作
if(principal.getId()==empId)
//PROCEED...
else
//STOP! GO TO 403!发布于 2016-02-19 15:39:39
您可以创建一个具有角色的组。而不是将用户添加到具有不同角色的不同组。
角色:r组:G用户:u m2m :多到多关系
R m2m G
G m2m U
基本上,多添加一层又一层的角色。
发布于 2016-02-19 15:40:02
最好的方法是在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 pagehttps://stackoverflow.com/questions/35509026
复制相似问题