Java Code Examples for org.apache.shiro.subject.PrincipalCollection#getPrimaryPrincipal()
The following examples show how to use
org.apache.shiro.subject.PrincipalCollection#getPrimaryPrincipal() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShiroRealm.java From blog-sample with Apache License 2.0 | 6 votes |
/** * 角色权限和对应权限添加 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { // 获取用户名 String name = (String) principalCollection.getPrimaryPrincipal(); // 获取用户对象 User user = userService.findByName(name); // 添加角色和权限 SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); List<Role> roles = getRoles(user.getId()); for (Role role : roles) { // 添加角色 simpleAuthorizationInfo.addRole(role.getName()); // 添加权限 List<Permission> permissions = getPermission(role.getId()); for (Permission permission : permissions) { simpleAuthorizationInfo.addStringPermission(permission.getName()); } } return simpleAuthorizationInfo; }
Example 2
Source File: MyShiroRealm.java From erp-framework with MIT License | 6 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { System.out.println("权限配置-->MyShiroRealm.doGetAuthorizationInfo()"); // 当前登录信息 ShiroUser shiroUser = (ShiroUser) principalCollection.getPrimaryPrincipal(); // 查询当前用户 ErpUser user = erpUserService.findUserByLoginName(shiroUser.getLoginName()); // 添加角色和权限 SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); List<ErpRole> roles = user.getRoles(); for(ErpRole role: roles) { if(StringUtils.isNotBlank(role.getName())) { simpleAuthorizationInfo.addRole(role.getName()); } } List<ErpMenu> menus = user.getMenus(); for(ErpMenu menu: menus) { if(StringUtils.isNotBlank(menu.getPermission())) { simpleAuthorizationInfo.addStringPermission(menu.getPermission()); } } return simpleAuthorizationInfo; }
Example 3
Source File: MovieRealm.java From Movie_Recommend with MIT License | 6 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { Admin admin = (Admin)principalCollection.getPrimaryPrincipal(); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); // 根据用户名查询用户拥有的角色 // AdminExample adminExample = new AdminExample(); // AdminExample.Criteria criteria = adminExample.createCriteria(); // criteria.andAdminnameEqualTo(adminname); // List<Admin> list = adminMapper.selectByExample(adminExample); Set<String> roleNames = new HashSet<String>(); if (0 == admin.getRole()) { roleNames.add("admin"); } else { roleNames.add("user"); } // 将角色名称提供给info authorizationInfo.setRoles(roleNames); return authorizationInfo; }
Example 4
Source File: JwtRealm.java From bootshiro with MIT License | 6 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { String payload = (String) principalCollection.getPrimaryPrincipal(); // likely to be json, parse it: if (payload.startsWith(JWT) && payload.charAt(NUM_4) == LEFT && payload.charAt(payload.length() - 1) == RIGHT) { Map<String, Object> payloadMap = JsonWebTokenUtil.readValue(payload.substring(4)); Set<String> roles = JsonWebTokenUtil.split((String)payloadMap.get("roles")); Set<String> permissions = JsonWebTokenUtil.split((String)payloadMap.get("perms")); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); if(null!=roles&&!roles.isEmpty()) { info.setRoles(roles); } if(null!=permissions&&!permissions.isEmpty()) { info.setStringPermissions(permissions); } return info; } return null; }
Example 5
Source File: AbstractAuthorizingRealm.java From onedev with MIT License | 6 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { Long userId = (Long) principals.getPrimaryPrincipal(); RequestCycle requestCycle = RequestCycle.get(); if (requestCycle != null) { Map<Long, AuthorizationInfo> authorizationInfos = requestCycle.getMetaData(AUTHORIZATION_INFOS); if (authorizationInfos == null) { authorizationInfos = new HashMap<>(); requestCycle.setMetaData(AUTHORIZATION_INFOS, authorizationInfos); } AuthorizationInfo authorizationInfo = authorizationInfos.get(userId); if (authorizationInfo == null) { authorizationInfo = newAuthorizationInfo(userId); authorizationInfos.put(userId, authorizationInfo); } return authorizationInfo; } else { return newAuthorizationInfo(userId); } }
Example 6
Source File: UserRealm.java From ssm with Apache License 2.0 | 6 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SysUser sysUser = (SysUser)principals.getPrimaryPrincipal(); List<SysPermission> sysPermissions = sysPermissionService.getPermissionsByUserAccount(sysUser.getAccount()); List<String> permissionValus = new ArrayList<String>(); if (sysPermissions != null) { // System.out.println(sysPermissions.size()); for (SysPermission sysPermission : sysPermissions) { permissionValus.add(sysPermission.getValue()); // System.out.println(sysPermission.toString()); } } SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); simpleAuthorizationInfo.addStringPermissions(permissionValus); return simpleAuthorizationInfo; }
Example 7
Source File: SimpleAuthorizingRealm.java From NutzSite with Apache License 2.0 | 6 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // null usernames are invalid if (principals == null) { throw new AuthorizationException("PrincipalCollection method argument cannot be null."); } User user = (User) principals.getPrimaryPrincipal(); if (user == null) { return null; } // 角色列表 Set<String> roles =userService.getRoleCodeList(user.getId()); // 功能列表 Set<String> menus = userService.getPermsByUserId(user.getId()); SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo(); auth.setRoles(roles); auth.setStringPermissions(menus); return auth; }
Example 8
Source File: ShiroRealm.java From taoshop with Apache License 2.0 | 5 votes |
/** * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用,负责在应用程序中决定用户的访问控制的方法(non-Javadoc) * @see AuthorizingRealm#doGetAuthorizationInfo(PrincipalCollection) */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) { String username = (String)pc.getPrimaryPrincipal(); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.setRoles(userService.getRoles(username)); authorizationInfo.setStringPermissions(userService.getPermissions(username)); return authorizationInfo; }
Example 9
Source File: MyShiroRealmTest.java From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String userName = (String) principals.getPrimaryPrincipal(); // 模拟从数据库中查询角色 Set<String> roles = getRolesByUserName(userName); // 模拟从数据库中查询权限 Set<String> permissions = getPermissionsByUserName(userName); SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); simpleAuthorizationInfo.setStringPermissions(permissions); simpleAuthorizationInfo.setRoles(roles); return simpleAuthorizationInfo; }
Example 10
Source File: MyRealm.java From songjhh_blog with Apache License 2.0 | 5 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String userName=(String)principals.getPrimaryPrincipal(); SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo(); authorizationInfo.setRoles(userService.getRolesByUserName(userName)); authorizationInfo.setStringPermissions(userService.getPermissionsByUserName(userName)); return authorizationInfo; }
Example 11
Source File: MyShiroRealm.java From chronus with Apache License 2.0 | 5 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { //获取登录用户名 String name = (String) principalCollection.getPrimaryPrincipal(); SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); if (Objects.equals("guest", name)) { simpleAuthorizationInfo.addRole("guest"); } else if (Objects.equals("admin", name)) { simpleAuthorizationInfo.addRole("admin"); } else { throw new UnknownAccountException("账户不存在!"); } return simpleAuthorizationInfo; }
Example 12
Source File: ShiroDbRealm.java From WebStack-Guns with MIT License | 5 votes |
/** * 权限认证 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { UserAuthService shiroFactory = UserAuthServiceServiceImpl.me(); ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal(); List<Integer> roleList = shiroUser.getRoleList(); Set<String> permissionSet = new HashSet<>(); Set<String> roleNameSet = new HashSet<>(); for (Integer roleId : roleList) { List<String> permissions = shiroFactory.findPermissionsByRoleId(roleId); if (permissions != null) { for (String permission : permissions) { if (ToolUtil.isNotEmpty(permission)) { permissionSet.add(permission); } } } String roleName = shiroFactory.findRoleNameByRoleId(roleId); roleNameSet.add(roleName); } SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.addStringPermissions(permissionSet); info.addRoles(roleNameSet); return info; }
Example 13
Source File: MyShiroRealm.java From EasyReport with Apache License 2.0 | 5 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(final PrincipalCollection principals) { final String account = (String)principals.getPrimaryPrincipal(); final User user = this.membershipFacade.getUser(account); final SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.setRoles(this.membershipFacade.getRoleSet(user.getRoles())); authorizationInfo.setStringPermissions(this.membershipFacade.getPermissionSet(user.getRoles())); return authorizationInfo; }
Example 14
Source File: MyRealm.java From SpringBoot-Base-System with GNU Lesser General Public License v3.0 | 5 votes |
/** * 根据用户名来添加相应的权限和角色 * * @time 2018年4月10日 下午5:07:03. * * @version V1.0 * @param principals * @return 授权用户信息 AuthorizationInfo */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { User user = (User) principals.getPrimaryPrincipal(); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); User dbUser = userService.findByUserName(user.getUserName()); Set<String> shiroPermissions = new HashSet<>();// 存放用户权限 Set<String> roleSet = new HashSet<String>();// 存放用户角色名 Set<Role> roles = dbUser.getRoles();// 从用户信息中获取用户拥有的角色 for (Role role : roles) { /** * 对于用户的每个角色,获取角色拥有的资源 */ Set<Resource> resources = role.getResources(); for (Resource resource : resources) { /** * 添加角色拥有的资源的全部id到集合中 */ shiroPermissions.add(resource.getSourceKey()); } /** * 保存用户的角色的id */ roleSet.add(role.getRoleKey()); } authorizationInfo.setRoles(roleSet); authorizationInfo.setStringPermissions(shiroPermissions); return authorizationInfo; }
Example 15
Source File: AuthorizationRealm.java From base-framework with Apache License 2.0 | 5 votes |
/** * * 当用户进行访问链接时的授权方法 * */ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); SessionVariable model = (SessionVariable) principals.getPrimaryPrincipal(); Assert.notNull(model, "找不到principals中的SessionVariable"); String id = model.getUser().getId(); //加载用户的组信息和资源信息 List<Resource> authorizationInfo = accountManager.getUserResources(id); List<Group> groupsList = accountManager.getUserGroups(id); List<Resource> resourcesList = accountManager.mergeResourcesToParent(authorizationInfo, ResourceType.Security); model.setAuthorizationInfo(authorizationInfo); model.setGroupsList(groupsList); model.setMenusList(resourcesList); //添加用户拥有的permission addPermissions(info,authorizationInfo); //添加用户拥有的role addRoles(info,groupsList); return info; }
Example 16
Source File: MyShiroRealm.java From JavaQuarkBBS with Apache License 2.0 | 5 votes |
/** * 授权 * @param principalCollection * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { Integer id = (Integer) principalCollection.getPrimaryPrincipal(); List<Permission> permissionList = permissionService.loadUserPermission(id); // 权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission) SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); permissionList.forEach(p->info.addStringPermission(p.getPerurl())); return info; }
Example 17
Source File: AuthRealm.java From spring-boot-demo with MIT License | 5 votes |
/** * 授权 * * @param principals * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { log.info("调用授权方法"); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); UserInfo userInfo = (UserInfo) principals.getPrimaryPrincipal(); for (SysRole role : userInfo.getRoleList()) { authorizationInfo.addRole(role.getRole()); for (SysPermission p : role.getPermissions()) { authorizationInfo.addStringPermission(p.getPermission()); } } return authorizationInfo; }
Example 18
Source File: ShiroRealmAdapter.java From seed with Mozilla Public License 2.0 | 5 votes |
@Override protected Object getAuthorizationCacheKey(PrincipalCollection principals) { Object primaryPrincipal = principals.getPrimaryPrincipal(); if (primaryPrincipal instanceof PrincipalProvider) { return ((PrincipalProvider<?>) primaryPrincipal).get(); } else { return primaryPrincipal; } }
Example 19
Source File: UserRealm.java From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = (String) principals.getPrimaryPrincipal(); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.setRoles(userService.findRoles(username)); authorizationInfo.setStringPermissions(userService.findPermissions(username)); return authorizationInfo; }
Example 20
Source File: UserRealm.java From es with Apache License 2.0 | 5 votes |
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = (String) principals.getPrimaryPrincipal(); User user = userService.findByUsername(username); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.setRoles(userAuthService.findStringRoles(user)); authorizationInfo.setStringPermissions(userAuthService.findStringPermissions(user)); return authorizationInfo; }