使用 Spring MVC 进行基于角色的访问控制

发布于 2024-12-01 04:12:46 字数 418 浏览 1 评论 0原文

我想知道 Spring 基于角色的访问控制的最佳实践。

我的要求是,

我将分配给用户一组角色,

user1=管理员,user2=专家

user1 的访问权限如下

/admin/会员管理

/admin/项目管理

......

对于 user2....

/myproject1/*

因此,如果 user2 尝试访问该网址

/admin/会员管理

将重定向到授权失败页面。

I would like to know the best practices for the role based access control with spring.

My requirements are,

I will have set of roles assigned to users say,

user1=admin, user2=expert

user1 will have the accesses write like

/admin/member-management

/admin/project-management

......

for user2....

/myproject1/*

so if user2 tries to access the url

/admin/member-management

will be redirect to authorization failure page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-12-08 04:12:46

与 Spring MVC 一起使用的标准框架是 Spring Security。虽然它可能非常复杂,但这是您需要的最小版本: 4.2.2 最小配置

在您的情况下,配置将如下所示:

<http auto-config='true'>
    <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
</http>

The standard framework to use with Spring MVC is Spring Security. While it can be very complex, here's a minimal version of what you need: 4.2.2 A Minimal Configuration

In your case, the config would be something like this:

<http auto-config='true'>
    <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
</http>
小红帽 2024-12-08 04:12:46

Spring Security 有角色的概念,但开箱即用,它没有权限的概念。它确实有一个 ACL 但此 ACL 比权限复杂得多,并且它们与对特定对象的操作相关,而不是一般授权操作。

看一下 Apache Shiro。它的角色和权限看起来与您给出的示例非常相似(使用通配符)。它也易于与 Spring 一起使用

Spring Security has the concept of roles but out of the box it does not have a concept of permissions. It does have a concept of ACLs but this ACLs are a lot more complicated than permissions, and they are tied to acting on specific objects, versus authorizing actions in general.

Take a look at Apache Shiro. It has roles and permissions that look very similar to what you gave as an example (using wildcards). It is also easy to use with Spring.

睡美人的小仙女 2024-12-08 04:12:46
public class DashBoardController {

@Autowired
UserService userService;

private static final Logger logger = LoggerFactory.getLogger(DashBoardController.class);

@SuppressWarnings("unchecked")
@RequestMapping(value = PathProxy.DashBoardUrls.SHOW_DASHBOARD, method = RequestMethod.GET)
public String role(Locale locale, Model model) {
    String userRole = null;
    logger.info("dashboard Controller");
    Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder
            .getContext().getAuthentication().getAuthorities();
    for (SimpleGrantedAuthority simpleGrantedAuthority : authorities) {
        userRole = simpleGrantedAuthority.toString();
    }

    switch (userRole) {

    case "ROLE_ADMIN":

        return "dashboard/admin";

    case "ROLE_HR_MANAGER":

        return "dashboard/hr_manager";

    case "ROLE_MANAGER":

        return "dashboard/manager";

    case "ROLE_EMPLOYEE":

        return "dashboard/employee";

    case "ROLE_COMPANY_ADMIN":

        return "dashboard/admin";

    default:

        break;
    }

    return userRole;

}

}

public class DashBoardController {

@Autowired
UserService userService;

private static final Logger logger = LoggerFactory.getLogger(DashBoardController.class);

@SuppressWarnings("unchecked")
@RequestMapping(value = PathProxy.DashBoardUrls.SHOW_DASHBOARD, method = RequestMethod.GET)
public String role(Locale locale, Model model) {
    String userRole = null;
    logger.info("dashboard Controller");
    Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder
            .getContext().getAuthentication().getAuthorities();
    for (SimpleGrantedAuthority simpleGrantedAuthority : authorities) {
        userRole = simpleGrantedAuthority.toString();
    }

    switch (userRole) {

    case "ROLE_ADMIN":

        return "dashboard/admin";

    case "ROLE_HR_MANAGER":

        return "dashboard/hr_manager";

    case "ROLE_MANAGER":

        return "dashboard/manager";

    case "ROLE_EMPLOYEE":

        return "dashboard/employee";

    case "ROLE_COMPANY_ADMIN":

        return "dashboard/admin";

    default:

        break;
    }

    return userRole;

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文