本文整理了Java中org.apache.shiro.subject.Subject.getPrincipal()
方法的一些代码示例,展示了Subject.getPrincipal()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Subject.getPrincipal()
方法的具体详情如下:
包路径:org.apache.shiro.subject.Subject
类名称:Subject
方法名:getPrincipal
[英]Returns this Subject's application-wide uniquely identifying principal, or null if this Subject is anonymous because it doesn't yet have any associated account data (for example, if they haven't logged in).
The term principal is just a fancy security term for any identifying attribute(s) of an application user, such as a username, or user id, or public key, or anything else you might use in your application to identify a user.
Although given names and family names (first/last) are technically considered principals as well, Shiro expects the object returned from this method to be an identifying attribute unique across your entire application.
This implies that things like given names and family names are usually poor candidates as return values since they are rarely guaranteed to be unique; Things often used for this value:
Most implementations will simply return #getPrincipals(). org.apache.shiro.subject.PrincipalCollection#getPrimaryPrincipal()
[中]返回此主题的应用程序范围唯一标识主体,如果此主题是匿名的,则返回null,因为它还没有任何关联的帐户数据(例如,如果他们尚未登录)。
术语principal只是一个花哨的安全术语,用于表示应用程序用户的任何标识属性,例如用户名、用户id或公钥,或者应用程序中用于标识用户的任何其他内容。
#####独特性
虽然从技术上讲,名字和姓氏(first/last)也被视为主体,但Shiro希望从该方法返回的对象是整个应用程序中唯一的标识属性。
这意味着,像名字和姓氏这样的东西作为返回值通常不太合适,因为它们很少保证是唯一的;通常用于此值的东西:
*一个长的RDBMS代理主键
*应用程序唯一的用户名
*爪哇。util。乌伊德
*LDAP唯一ID
或对您的应用程序有价值的任何其他类似的、合适的、独特的机制。
大多数实现只会返回#getPrincipals(). org.apache.shiro.subject.PrincipalCollection#getPrimaryPrincipal()
代码示例来源:origin: Graylog2/graylog2-server
@Override
public String getName() {
final Object principal = subject.getPrincipal();
return principal == null ? null : principal.toString();
}
代码示例来源:origin: Graylog2/graylog2-server
@Override
public boolean isUserInRole(String role) {
LOG.debug("Checking role {} for user {}.", role, subject.getPrincipal());
return subject.hasRole(role);
}
代码示例来源:origin: apache/shiro
/**
* Internal utility method to retrieve the username of the current authenticated user.
*
* @return The name.
*/
protected String getCurrentUsername() {
Subject subject = SecurityUtils.getSubject();
if (subject == null || subject.getPrincipal() == null || !subject.isAuthenticated()) {
throw new IllegalStateException("Unable to retrieve the current authenticated subject");
}
return SecurityUtils.getSubject().getPrincipal().toString();
}
}
代码示例来源:origin: apache/shiro
protected Object getSubjectPrincipal() {
Object userPrincipal = null;
Subject subject = getSubject();
if (subject != null) {
userPrincipal = subject.getPrincipal();
}
return userPrincipal;
}
代码示例来源:origin: Graylog2/graylog2-server
@Nullable
protected User getCurrentUser() {
final Object principal = getSubject().getPrincipal();
final User user = userService.load(principal.toString());
if (user == null) {
LOG.error("Loading the current user failed, this should not happen. Did you call this method in an unauthenticated REST resource?");
}
return user;
}
代码示例来源:origin: apache/usergrid
public static UserInfo getUser() {
Subject currentUser = getSubject();
if ( currentUser == null ) {
return null;
}
if ( !( currentUser.getPrincipal() instanceof UserPrincipal ) ) {
return null;
}
UserPrincipal principal = ( UserPrincipal ) currentUser.getPrincipal();
return principal.getUser();
}
代码示例来源:origin: apache/shiro
public int onDoStartTag() throws JspException {
if (getSubject() != null && getSubject().getPrincipal() != null) {
if (log.isTraceEnabled()) {
log.trace("Subject has known identity (aka 'principal'). " +
"Tag body will be evaluated.");
}
return EVAL_BODY_INCLUDE;
} else {
if (log.isTraceEnabled()) {
log.trace("Subject does not exist or have a known identity (aka 'principal'). " +
"Tag body will not be evaluated.");
}
return SKIP_BODY;
}
}
代码示例来源:origin: apache/shiro
public int onDoStartTag() throws JspException {
if (getSubject() == null || getSubject().getPrincipal() == null) {
if (log.isTraceEnabled()) {
log.trace("Subject does not exist or does not have a known identity (aka 'principal'). " +
"Tag body will be evaluated.");
}
return TagSupport.EVAL_BODY_INCLUDE;
} else {
if (log.isTraceEnabled()) {
log.trace("Subject exists or has a known identity (aka 'principal'). " +
"Tag body will not be evaluated.");
}
return TagSupport.SKIP_BODY;
}
}
代码示例来源:origin: Graylog2/graylog2-server
protected void checkAnyPermission(String permissions[], String instanceId) {
if (!isAnyPermitted(permissions, instanceId)) {
LOG.info("Not authorized to access resource id <{}>. User <{}> is missing permissions {} on instance <{}>",
instanceId, getSubject().getPrincipal(), Arrays.toString(permissions), instanceId);
throw new ForbiddenException("Not authorized to access resource id <" + instanceId + ">");
}
}
代码示例来源:origin: stylefeng/Guns
/**
* 输出当前用户信息,通常为登录帐号信息。
*
* @return 当前用户信息
*/
public static String principal() {
if (getSubject() != null) {
Object principal = getSubject().getPrincipal();
return principal.toString();
}
return "";
}
代码示例来源:origin: stylefeng/Guns
/**
* 输出当前用户信息,通常为登录帐号信息。
*
* @return 当前用户信息
*/
public String principal() {
if (getSubject() != null) {
Object principal = getSubject().getPrincipal();
return principal.toString();
}
return "";
}
代码示例来源:origin: Graylog2/graylog2-server
protected void checkPermission(String permission) {
if (!isPermitted(permission)) {
LOG.info("Not authorized. User <{}> is missing permission <{}>", getSubject().getPrincipal(), permission);
throw new ForbiddenException("Not authorized");
}
}
代码示例来源:origin: Graylog2/graylog2-server
protected void checkPermission(String permission, String instanceId) {
if (!isPermitted(permission, instanceId)) {
LOG.info("Not authorized to access resource id <{}>. User <{}> is missing permission <{}:{}>",
instanceId, getSubject().getPrincipal(), permission, instanceId);
throw new ForbiddenException("Not authorized to access resource id <" + instanceId + ">");
}
}
代码示例来源:origin: stylefeng/Guns
/**
* 认证通过或已记住的用户。与guset搭配使用。
*
* @return 用户:true,否则 false
*/
public boolean isUser() {
return getSubject() != null && getSubject().getPrincipal() != null;
}
代码示例来源:origin: stylefeng/Guns
/**
* 认证通过或已记住的用户。与guset搭配使用。
*
* @return 用户:true,否则 false
*/
public static boolean isUser() {
return getSubject() != null && getSubject().getPrincipal() != null;
}
代码示例来源:origin: apache/geode
@Override
public void logout() {
Subject currentUser = getSubject();
try {
logger.debug("Logging out " + currentUser.getPrincipal());
currentUser.logout();
} catch (ShiroException e) {
logger.info("error logging out: " + currentUser.getPrincipal());
throw new GemFireSecurityException(e.getMessage(), e);
}
// clean out Shiro's thread local content
ThreadContext.remove();
}
代码示例来源:origin: linlinjava/litemall
@RequiresAuthentication
@GetMapping("/info")
public Object info() {
Subject currentUser = SecurityUtils.getSubject();
LitemallAdmin admin = (LitemallAdmin) currentUser.getPrincipal();
Map<String, Object> data = new HashMap<>();
data.put("name", admin.getUsername());
data.put("avatar", admin.getAvatar());
Integer[] roleIds = admin.getRoleIds();
Set<String> roles = roleService.queryByIds(roleIds);
Set<String> permissions = permissionService.queryByRoleIds(roleIds);
data.put("roles", roles);
data.put("perms", permissions);
return ResponseUtil.ok(data);
}
代码示例来源:origin: wuyouzhuguli/FEBS-Shiro
@Override
@Transactional
public void updatePassword(String password) {
User user = (User) SecurityUtils.getSubject().getPrincipal();
Example example = new Example(User.class);
example.createCriteria().andCondition("username=", user.getUsername());
String newPassword = MD5Utils.encrypt(user.getUsername().toLowerCase(), password);
user.setPassword(newPassword);
this.userMapper.updateByExampleSelective(user, example);
}
代码示例来源:origin: apache/geode
@Before
public void before() throws Exception {
this.mockSecurityManager = mock(SecurityManager.class);
this.shiroManager = mock(org.apache.shiro.mgt.SecurityManager.class);
this.provider = mock(SecurityManagerProvider.class);
this.mockSubject = mock(Subject.class);
when(provider.getShiroSecurityManager()).thenReturn(shiroManager);
when(provider.getSecurityManager()).thenReturn(mockSecurityManager);
when(shiroManager.createSubject(any(SubjectContext.class))).thenReturn(mockSubject);
when(mockSubject.getPrincipal()).thenReturn("principal");
when(mockSubject.getSession()).thenReturn(mock(Session.class));
this.securityService = new IntegratedSecurityService(provider, null);
}
代码示例来源:origin: apache/shiro
public String call() throws Exception {
Subject callingSubject = SecurityUtils.getSubject();
assertNotNull(callingSubject);
assertNotNull(SecurityUtils.getSecurityManager());
assertEquals(callingSubject, sourceSubject);
return "Hello " + callingSubject.getPrincipal();
}
};
内容来源于网络,如有侵权,请联系作者删除!