org.apache.shiro.subject.Subject.runAs()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(370)

本文整理了Java中org.apache.shiro.subject.Subject.runAs()方法的一些代码示例,展示了Subject.runAs()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Subject.runAs()方法的具体详情如下:
包路径:org.apache.shiro.subject.Subject
类名称:Subject
方法名:runAs

Subject.runAs介绍

[英]Allows this subject to 'run as' or 'assume' another identity indefinitely. This can only be called when the Subject instance already has an identity (i.e. they are remembered from a previous log-in or they have authenticated during their current session).

Some notes about runAs:

  • You can tell if a Subject is 'running as' another identity by calling the #isRunAs() method.
  • If running as another identity, you can determine what the previous 'pre run as' identity was by calling the #getPreviousPrincipals() method.
  • When you want a Subject to stop running as another identity, you can return to its previous 'pre run as' identity by calling the #releaseRunAs() method.
    [中]允许此主题无限期地“运行为”或“假定”另一身份。只有当主题实例已经具有标识时(即,他们从以前的登录中被记住,或者他们在当前会话中进行了身份验证),才能调用此函数。
    关于runAs的一些注意事项:
    *通过调用#isRunAs()方法,可以判断一个主题是否作为另一个身份“运行”。
    *如果以另一个身份运行,可以通过调用#GetPreviousPrinciples()方法来确定上一个“pre run as”身份是什么。
    *当您希望主题停止以另一身份运行时,可以通过调用#releaseRunAs()方法返回到其以前的“pre run as”身份。

代码示例

代码示例来源:origin: apache/shiro

subject.runAs(new SimplePrincipalCollection("user2", IniSecurityManagerFactory.INI_REALM_NAME));
assertTrue(subject.isRunAs());
assertEquals("user2", subject.getPrincipal());
subject.runAs(new SimplePrincipalCollection("user3", IniSecurityManagerFactory.INI_REALM_NAME));
assertTrue(subject.isRunAs());
assertEquals("user3", subject.getPrincipal());

代码示例来源:origin: theonedev/onedev

@Override
public void process(IFormSubmitter submittingComponent) {
  SecurityUtils.getSubject().runAs(getUser().getPrincipals());
  try {
    super.process(submittingComponent);
  } finally {
    SecurityUtils.getSubject().releaseRunAs();
  }
}

代码示例来源:origin: org.fcrepo/fcrepo-auth-common

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
    throws IOException, ServletException {
  final HttpServletRequest hsRequest = (HttpServletRequest) request;
  final Subject currentUser = SecurityUtils.getSubject();
  PrincipalCollection principals = currentUser.getPrincipals();
  final Set<Principal> newPrincipals = getPrincipals(hsRequest);
  if (newPrincipals.size() > 0) {
    final Set<Principal> currentPrincipals;
    if (principals == null || principals.asList().isEmpty()) {
      log.debug("Shiro Principal object is not found!");
      currentPrincipals = newPrincipals;
     } else {
      currentPrincipals = new HashSet<>((Set<Principal>) principals.asSet());
      log.debug("Number of Principals already in session object: {}", currentPrincipals.size());
      currentPrincipals.addAll(newPrincipals);
    }
    log.debug("Number of Principals after processing the current request: {}", currentPrincipals.size());
    principals = new SimplePrincipalCollection(currentPrincipals, REALM_NAME);
    currentUser.runAs(principals);
  } else {
    log.debug("New Principals not found in the request!");
  }
  chain.doFilter(request, response);
}

代码示例来源:origin: lerry903/RuoYi

public static void setSysUser(SysUser user) {
  Subject subject = getSubject();
  PrincipalCollection principalCollection = subject.getPrincipals();
  String realmName = principalCollection.getRealmNames().iterator().next();
  PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
  // 重新加载Principal
  subject.runAs(newPrincipalCollection);
}

代码示例来源:origin: java-aodeng/hope-plus

/***
 * 重新加载用户权限
 * @param user
 */
@Override
public void reloadAuthorizingByUserId(SysUser user) {
  RealmSecurityManager realmSecurityManager=(RealmSecurityManager) SecurityUtils.getSecurityManager();
  HopeShiroRealm hopeShiroReam=(HopeShiroRealm)realmSecurityManager.getRealms().iterator().next();
  Subject subject=SecurityUtils.getSubject();
  String realmName=subject.getPrincipals().getRealmNames().iterator().next();
  SimplePrincipalCollection simplePrincipalCollection=new SimplePrincipalCollection(user,realmName);
  subject.runAs(simplePrincipalCollection);
  hopeShiroReam.getAuthorizationCache().remove(subject.getPrincipals());
  subject.releaseRunAs();
  log.info("[以下用户权限更新成功!]-[{}]",user.getUsername());
}

代码示例来源:origin: zhangyd-c/OneBlog

/**
 * 重新加载用户权限
 *
 * @param user
 */
public void reloadAuthorizingByUserId(User user) {
  RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
  ShiroRealm shiroRealm = (ShiroRealm) rsm.getRealms().iterator().next();
  Subject subject = SecurityUtils.getSubject();
  String realmName = subject.getPrincipals().getRealmNames().iterator().next();
  SimplePrincipalCollection principals = new SimplePrincipalCollection(user.getId(), realmName);
  subject.runAs(principals);
  shiroRealm.getAuthorizationCache().remove(subject.getPrincipals());
  subject.releaseRunAs();
  log.info("用户[{}]的权限更新成功!!", user.getUsername());
}

代码示例来源:origin: zhangyd-c/springboot-shiro

/**
 * 重新加载用户权限
 *
 * @param user
 */
@Override
public void reloadAuthorizingByUserId(User user) {
  RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
  ShiroRealm shiroRealm = (ShiroRealm) rsm.getRealms().iterator().next();
  Subject subject = SecurityUtils.getSubject();
  String realmName = subject.getPrincipals().getRealmNames().iterator().next();
  SimplePrincipalCollection principals = new SimplePrincipalCollection(user.getId(), realmName);
  subject.runAs(principals);
  shiroRealm.getAuthorizationCache().remove(subject.getPrincipals());
  subject.releaseRunAs();
  LOG.info("用户[{}]的权限更新成功!!", user.getUsername());
}

代码示例来源:origin: theonedev/onedev

@Override
protected void finished() {
  WebSession.get().logout();
  User root = OneDev.getInstance(UserManager.class).getRoot();
  SecurityUtils.getSubject().runAs(root.getPrincipals());
  throw new RestartResponseException(ProjectListPage.class);
}

代码示例来源:origin: theonedev/onedev

Preconditions.checkState(fields.size() == 2);
SecurityUtils.getSubject().runAs(User.asPrincipal(Long.valueOf(fields.get(1))));
try {
  Project project = projectManager.load(Long.valueOf(fields.get(0)));

代码示例来源:origin: theonedev/onedev

@Override
protected void onSubmit() {
  super.onSubmit();
  
  UserManager userManager = OneDev.getInstance(UserManager.class);
  User userWithSameName = userManager.findByName(user.getName());
  if (userWithSameName != null) {
    editor.getErrorContext(new PathSegment.Property("name"))
        .addError("This name has already been used by another user.");
  } 
  User userWithSameEmail = userManager.findByEmail(user.getEmail());
  if (userWithSameEmail != null) {
    editor.getErrorContext(new PathSegment.Property("email"))
        .addError("This email has already been used by another user.");
  } 
  if (!editor.hasErrors(true)) {
    user.setPassword(AppLoader.getInstance(PasswordService.class).encryptPassword(user.getPassword()));
    userManager.save(user, null);
    Session.get().success("New user registered");
    SecurityUtils.getSubject().runAs(user.getPrincipals());
    setResponsePage(MyAvatarPage.class);
  }
}

相关文章