本文整理匯總了Java中org.apache.shiro.authz.SimpleAuthorizationInfo類的典型用法代碼示例。如果您正苦於以下問題:Java SimpleAuthorizationInfo類的具體用法?Java SimpleAuthorizationInfo怎麽用?Java SimpleAuthorizationInfo使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SimpleAuthorizationInfo類屬於org.apache.shiro.authz包,在下文中一共展示了SimpleAuthorizationInfo類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
/**
* 完成授權
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
//得到認證成功之後憑證的身份信息
String username=((User) principals.getPrimaryPrincipal()).getUsername();
//查詢數據庫得到所有的權限列表
List<String> permissionList=new ArrayList<String>();
UserCustom userCustom = customMapper.findUserCustomByUsername(username);
Set<RoleCustom> roles=userCustom.getRoleSet();
for(RoleCustom role:roles){
Set<Permission> permissionSet = role.getPermissionSet();
for (Permission permission:permissionSet) {
permissionList.add(permission.getPname());
}
}
SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
authorizationInfo.addStringPermissions(permissionList);
return authorizationInfo;
}
示例2: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
/**
* 完成授權
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
//得到認證成功之後憑證的身份信息
String username=(String) principals.getPrimaryPrincipal();
//查詢數據庫得到所有的權限列表
List<String> permissionList=new ArrayList<String>();
UserCustomMapper mapper=getUserCustomMapper();
UserCustom userCustom = mapper.findUserCustomByUsername(username);
Set<RoleCustom> roles=userCustom.getRoleSet();
for(RoleCustom role:roles){
Set<Permission> permissionSet = role.getPermissionSet();
for (Permission permission:permissionSet) {
permissionList.add(permission.getPname());
}
}
SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
authorizationInfo.addStringPermissions(permissionList);
return authorizationInfo;
}
示例3: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
/**
* 此方法調用 hasRole,hasPermission的時候才會進行回調.
*
* 權限信息.(授權):
* 1、如果用戶正常退出,緩存自動清空;
* 2、如果用戶非正常退出,緩存自動清空;
* 3、如果我們修改了用戶的權限,而用戶不退出係統,修改的權限無法立即生效。
* (需要手動編程進行實現;放在service進行調用)
* 在權限修改後調用realm中的方法,realm已經由spring管理,所以從spring中獲取realm實例,
* 調用clearCached方法;
* :Authorization 是授權訪問控製,用於對用戶進行的操作授權,證明該用戶是否允許進行當前操作,如訪問某個鏈接,某個資源文件等。
* @param principals
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) throws AuthenticationException{
/*
* 當沒有使用緩存的時候,不斷刷新頁麵的話,這個代碼會不斷執行,
* 當其實沒有必要每次都重新設置權限信息,所以我們需要放到緩存中進行管理;
* 當放到緩存中時,這樣的話,doGetAuthorizationInfo就隻會執行一次了,
* 緩存過期之後會再次執行。
*/
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
AdminDO userInfo = (AdminDO)principals.getPrimaryPrincipal();
Set<String> set = new HashSet<String>();
set.add(userInfo.getRole().getRoleId());
authorizationInfo.setRoles(set);
Set<String> menus = roleMenuService.getMenuCodesByRoleId(userInfo.getRole().getRoleId());
authorizationInfo.addStringPermissions(menus);
return authorizationInfo;
}
示例4: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
Long userId = (Long) WebUtil.getCurrentUser();
Parameter parameter = new Parameter("sysAuthorizeService", "queryPermissionByUserId").setId(userId);
logger.info("{} execute queryPermissionByUserId start...", parameter.getNo());
List<?> list = provider.execute(parameter).getList();
logger.info("{} execute queryPermissionByUserId end.", parameter.getNo());
for (Object permission : list) {
if (StringUtils.isNotBlank((String) permission)) {
// 添加基於Permission的權限信息
info.addStringPermission((String) permission);
}
}
// 添加用戶權限
info.addStringPermission("user");
return info;
}
示例5: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
@SuppressWarnings("rawtypes")
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = (String) principals.getPrimaryPrincipal();
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
// 根據用戶名查詢當前用戶擁有的角色
List<Map> roles = userService.findRolesByUserName(username);
Set<String> roleNames = new HashSet<String>();
for (Map role : roles) {
roleNames.add(role.get("name").toString());
}
// 將角色名稱提供給info
authorizationInfo.setRoles(roleNames);
// 根據用戶名查詢當前用戶權限
List<Map> permissions = userService.findPermissionsByUserName(username);
Set<String> permissionNames = new HashSet<String>();
for (Map permission : permissions) {
permissionNames.add(permission.get("name").toString());
}
// 將權限名稱提供給info
authorizationInfo.setStringPermissions(permissionNames);
return authorizationInfo;
}
示例6: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
String userName = (String) principals.fromRealm(getName()).iterator()
.next();
List<WizardRoleInfo> roleInfos = securityService.getRoleInfo(userName);
List<WizardOrgInfo> permissionInfos = null;
if (null == roleInfos || 0 == roleInfos.size()) {
return null;
} else {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for (WizardRoleInfo roleInfo : roleInfos) {
info.addRole(roleInfo.getRoleName());
permissionInfos = securityService.getPermissionInfo(roleInfo
.getRoleName());
for (WizardOrgInfo permissionInfo : permissionInfos) {
info.addStringPermission(permissionInfo.getOrgName());
}
}
return info;
}
}
示例7: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
/**
* 授權查詢回調函數, 進行鑒權但緩存中無用戶的授權信息時調用.
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principalCollection) {
if (principalCollection == null) {
throw new AuthorizationException("Principal is not null!");
}
Shiro shiro = (Shiro) principalCollection.getPrimaryPrincipal();
User entity = new User();
entity.setId(shiro.getId());
entity = (User) service.iUserService.select(entity);
if (null == entity) {
throw new UnknownAccountException("No account found for user ["
+ shiro.getId() + "]");
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
return info;
}
示例8: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// 配置用戶權限
Principal principal = (Principal) principals.fromRealm(getName()).iterator().next();
if (principal != null) {
// 推薦將權限標識加入緩存,然後從緩存獲取權限標識集合,並添加給SimpleAuthorizationInfo,避免頻繁訪問數據庫獲取資源
// 如:List<String> permissions = redisService.findUserPermissions(userId);
// 權限標識與applicationContext-shiro.xml配置的filterChainDefinitionsperms中類似["admin:userModule"]
// 以下示例
List<String> permissions = new ArrayList<String>();
permissions.add("admin:userModule");// 賦予這個標識才有權限訪問/user/findPage.html
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
authorizationInfo.addStringPermissions(permissions);
return authorizationInfo;
}
return null;
}
示例9: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
/**
* 獲取當前用戶的角色集合,權限集合
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//獲取保存在session中的用戶信息
SysUser user = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysUser.SYS_USER);
if (user == null) {
throw new IllegalArgumentException();
}
//獲取當前用戶擁有的所有角色
List<SysRole> roles = roleService.getSysRoleByUserId(user.getUserId().toString(), PublicEnum.NORMAL.value());
for (SysRole sysRole : roles) {
authorizationInfo.addRole(sysRole.getRoleCode());
}
//獲取當前用戶擁有的所有權限
List<SysPermission> permissions = permissionService.getSysPermissionByUserId(user.getUserId(), PublicEnum.NORMAL.value());
for (SysPermission sysPermission : permissions) {
authorizationInfo.addStringPermission(sysPermission.getPermission());
}
System.out.println("用戶權限:"+ JSON.toJSONString(authorizationInfo));
return authorizationInfo;
}
示例10: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
final String username = getUsername(principals);
List<Roles> roles = usersRepository.findRolesByUsername(username);
LOG.debug("Load Roles[{}] by username: {}", roles, username);
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for (Roles role : roles) {
info.addRole(role.roleName());
for (RolesPermissions permissions : role.permissions()) {
info.addStringPermission(permissions.permission());
}
}
return info;
}
示例11: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
/**
* 完成授權
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) {
//得到認證成功之後憑證的身份信息
String username=((User) principals.getPrimaryPrincipal()).getUsername();
//查詢數據庫得到所有的權限列表
List<String> permissionList=new ArrayList<String>();
List<String> roleList=new ArrayList<>();
UserCustom userCustom = customMapper.findUserCustomByUsername(username);
Set<RoleCustom> roles=userCustom.getRoleSet();
for(RoleCustom role:roles){
roleList.add(role.getRname());
Set<Permission> permissionSet = role.getPermissionSet();
for (Permission permission:permissionSet) {
permissionList.add(permission.getPname());
}
}
SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
authorizationInfo.addStringPermissions(permissionList);
authorizationInfo.addRoles(roleList);
return authorizationInfo;
}
示例12: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
@Override
//@org.springframework.transaction.annotation.Transactional
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//獲取當前用戶
//UserDto user = convertToDto(userDao.findUserByUsername((String)principalCollection.getPrimaryPrincipal()));
//User currentUser = userDao.findUserByUsername((String)principalCollection.getPrimaryPrincipal());
UserDto user = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("user");
//把principals放session中,key=userId value=principals
SecurityUtils.getSubject().getSession().setAttribute(String.valueOf(user.getId()),SecurityUtils.getSubject().getPrincipals());
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//賦予角色
for(RoleDto role:user.getRoles()){
info.addRole(role.getName());
}
//賦予權限
for(PermissionDto permission:user.getPermissions()){
//System.out.println(permission.getName());
info.addStringPermission(permission.getName());
}
return info;
}
示例13: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
/**
* 授權
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//Long userId = TokenManager.getUserId();
User token = (User)SecurityUtils.getSubject().getPrincipal();
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//根據用戶ID查詢角色(role),放入到Authorization裏。
Set<String> roleName = roleService.findRoleNameByUserId(String.valueOf(token.getUuid()));
info.setRoles(roleName);
//根據用戶ID查詢權限(permission),放入到Authorization裏。
Set<String> permissions = permissionService.findPermissionByUserId(String.valueOf(token.getUuid()));
info.setStringPermissions(permissions);
return info;
}
示例14: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
Preconditions.checkNotNull(principalCollection, "You can't have a null collection of principals. No really, how did you do that");
String userEmail = (String) getAvailablePrincipal(principalCollection);
if (userEmail == null) {
throw new NullPointerException("Can't find a principal in the collection");
}
LOGGER.debug("Finding authorization info for " + userEmail + " in DB");
final User user = userRepository.findByEmailAndActive(userEmail, true);
LOGGER.debug("Found " + userEmail + " in DB");
final int totalRoles = user.getRoles().size();
final Set<String> roleNames = new LinkedHashSet<>(totalRoles);
final Set<String> permissionNames = new LinkedHashSet<>();
if (totalRoles > 0) {
for (Role role : user.getRoles()) {
roleNames.add(role.getName());
for (Permission permission : role.getPermissions()) {
permissionNames.add(permission.getName());
}
}
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addRoles(roleNames);
info.addStringPermissions(permissionNames);
return info;
}
示例15: doGetAuthorizationInfo
import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0)
{
ShiroToken token = (ShiroToken) SecurityUtils.getSubject().getPrincipal();
String username = token.getUsername();
logger.info(username + "授權...");
// 從數據庫中查找該用戶的角色和權限
SimpleAuthorizationInfo sainfo = new SimpleAuthorizationInfo();
Set<String> roles = new HashSet<String>();
roles.add("admin");
//roles.add("role1");
Set<String> permissions = new HashSet<String>();
permissions.add("add");
permissions.add("delete");
sainfo.setRoles(roles);
sainfo.setStringPermissions(permissions);
return sainfo;
}