在我的网络应用程序中,我希望用户(角色USER)拥有他们只能看到自己的私人页面。我还想拥有一个超级管理员(角色ADMIN),它可以看到所有用户的所有私人页面。
所以我按照以下方式对网址进行建模:
/user/{userId}/...
现在除了ADMIN之外,只有具有userid的USER应该能够调用此页面。用户可以动态添加,其用户ID将是生成的序列号。
我开始像这样建模:
<http pattern="/user/**" use-expressions="true" >
...
<intercept-url pattern="/user/**" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" />
...
</http>
但这样做会使用userid 2启用USER以调用/user/1/...
这样做的正确方法是什么?有人可以举个例子吗?
答案 0 :(得分:2)
/user/{userId}/
中的userId仅在用户具有ADMIN角色时才有意义。对于其他用户,只需忽略该参数并使用
UserDetails userDetails =
(UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if('ROLE_ADMIN' in userDetails.getAuthorities() ){
//get user indentified by userId
}
else{
//get current user
}
获取当前用户相关信息。