如何检查“hasRole”使用Spring Security的Java代码? [英] How to check "hasRole" in Java Code with Spring Security?

查看:1068
本文介绍了如何检查“hasRole”使用Spring Security的Java代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查Java代码中的用户权限或权限?例如 - 我想根据角色为用户显示或隐藏按钮。有以下注释:

How to check user authority or permission in Java Code ? For example - I want to show or hide button for user depending on role. There are annotations like:

@PreAuthorize("hasRole('ROLE_USER')")

如何在Java代码中创建它?类似于:

How to make it in Java code? Something like :

if(somethingHere.hasRole("ROLE_MANAGER")) {
   layout.addComponent(new Button("Edit users"));
}


推荐答案

Spring Security 3.0有这个API

Spring Security 3.0 has this API

SecurityContextHolderAwareRequestWrapper.isUserInRole(String role)

在使用它之前,你必须注入包装器。

You'll have to inject the wrapper, before you use it.

SecurityContextHolderAwareRequestWrapper

这篇关于如何检查“hasRole”使用Spring Security的Java代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆