本文整理了Java中org.apache.shiro.subject.Subject.runAs()
方法的一些代码示例,展示了Subject.runAs()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Subject.runAs()
方法的具体详情如下:
包路径:org.apache.shiro.subject.Subject
类名称: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:
代码示例来源: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);
}
}
内容来源于网络,如有侵权,请联系作者删除!