如何检查用户是否有Spring Security的权限?

发布于 2024-12-09 09:38:02 字数 299 浏览 0 评论 0原文

我检查了这个问题,我知道如何在java中获取和检查用户角色。

但是,我想要的是,当用户单击页面中的某些链接时,如果用户没有权限,我如何获取它并在消息字段中返回一些消息,例如“无权限”?目前,会有一个例外。

我没有在页面中使用 spring 标签,我知道只有用户具有某种角色时显示此链接的标签。但由于某些原因我不想使用这些标签。

I checked this question, and I know how to get and check user role in java.

But, what I want is, when user clicks some links in page, if user has no right on that, how can I get it and return some message like "no rights" in a message fields? Currently, there will be an exception.

I didn't use spring tags in page, I know the tags that display this link if only user has some role. But I don't want to user those tags for some reasons.

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

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

发布评论

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

评论(1

享受孤独 2024-12-16 09:38:02

您可以使用 JavaScript,处理链接上的单击事件并查看服务器响应。如果不行 - 显示错误消息。如果没问题 - 重定向。

像这样的东西:(

$('#your-link-id').click(function(event) {
    event.preventDefault();
    var url = $(this).attr('href');

    $.ajax({
        url : url,
        success : function() {
            window.location = url;
        },
        error : function() {
            alert('You cannot go to this link.');
        }
    });
});

使用 jQuery 库)。

You can use JavaScript, handle the click event on the link and see the server response. If it's not ok - show error message. If it is OK - redirect.

Something like this:

$('#your-link-id').click(function(event) {
    event.preventDefault();
    var url = $(this).attr('href');

    $.ajax({
        url : url,
        success : function() {
            window.location = url;
        },
        error : function() {
            alert('You cannot go to this link.');
        }
    });
});

(jQuery library is used).

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