Example usage for org.apache.shiro.authz SimpleAuthorizationInfo setStringPermissions

List of usage examples for org.apache.shiro.authz SimpleAuthorizationInfo setStringPermissions

Introduction

In this page you can find the example usage for org.apache.shiro.authz SimpleAuthorizationInfo setStringPermissions.

Prototype

public void setStringPermissions(Set<String> stringPermissions) 

Source Link

Document

Sets the string-based permissions assigned directly to the account.

Usage

From source file:aaa.realms.MySQLRealm.java

License:Apache License

/**
 * This implementation of the interface expects the principals collection to return a String username keyed off of
 * this realm's {@link #getName() name}//from  w w  w  .  j av  a 2  s  .  c om
 *
 * @see #getAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    //null usernames are invalid
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }

    String username = (String) getAvailablePrincipal(principals);

    Connection conn = null;
    Set<String> roleNames = null;
    Set<String> permissions = null;
    try {
        conn = dataSource.getConnection();

        // Retrieve roles and permissions from database
        roleNames = getRoleNamesForUser(conn, username);
        if (permissionsLookupEnabled) {
            permissions = getPermissions(conn, username);
        }

    } catch (SQLException e) {
        final String message = "There was a SQL error while authorizing user [" + username + "]";
        if (log.isErrorEnabled()) {
            log.error(message, e);
        }

        // Rethrow any SQL errors as an authorization exception
        throw new AuthorizationException(message, e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
    info.setStringPermissions(permissions);
    return info;

}

From source file:action.ShiroDbRealm.java

License:Apache License

/**
 * ?, ???./*from   w w w  . j  a  v  a2 s  .  c  om*/
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    // roles
    List<Role> roles = roleService.getByUserId(shiroUser.id);
    List<String> stringRoles = new ArrayList<String>(roles.size());
    for (Role role : roles) {
        stringRoles.add(role.getName());
    }
    info.addRoles(stringRoles);

    // permissions
    List<Permission> permissions = permissionService.getByUserId(shiroUser.id);
    Set<String> stringPermissions = new HashSet<String>(permissions.size());
    for (Permission permission : permissions) {
        stringPermissions.add(permission.getValue());
    }
    info.setStringPermissions(stringPermissions);

    return info;
}

From source file:br.com.betsportclub.controller.security.SecurityRealm.java

License:Apache License

/**
 * This implementation of the interface expects the principals collection to return a String username keyed off of
 * this realm's {@link #getName() name}//from w  ww  .  ja  v a2 s .com
 *
 * @see #getAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    //null usernames are invalid
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }

    String username = (String) getAvailablePrincipal(principals);

    Connection conn = null;
    Set<String> roleNames = null;
    Set<String> permissions = null;
    try {
        conn = dataSource.getConnection();

        // Retrieve roles and permissions from database
        roleNames = getRoleNamesForUser(conn, username);
        if (permissionsLookupEnabled) {
            permissions = getPermissions(conn, username, roleNames);
        }

    } catch (SQLException e) {
        final String message = "There was a SQL error while authorizing user [" + username + "]";
        if (log.isErrorEnabled()) {
            log.error(message, e);
        }

        // Rethrow any SQL errors as an authorization exception
        throw new AuthorizationException(message, e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
    info.setStringPermissions(permissions);
    return info;

}

From source file:cn.itganhuo.app.web.shiro.ShiroDbRealm.java

License:Apache License

/**
 * ???????// w  ww . jav a 2 s. com
 *
 * @version 0.0.1-SNAPSHOT
 * @author -?
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    log.debug("Start reading user permissions.");

    String account = (String) getAvailablePrincipal(principals);
    User user = userService.loadByAccount(account);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Set<String> role = new HashSet<String>();
    Set<String> stringPermissions = new HashSet<String>();
    // TODO ?????
    if (1 == user.getType()) {
        role.add("user");
        stringPermissions.add("user:*");
    } else if (999 == user.getType()) {
        role.add("admin");
        stringPermissions.add("admin:*");
    }
    info.setRoles(role);
    info.setStringPermissions(stringPermissions);
    return info;
}

From source file:cn.mypandora.shiro.realm.UserRealm.java

License:Apache License

/**
 * ?Subject??//w  ww  .  ja  v a 2  s  . c  om
 *
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    //????,(String)principals.fromRealm(this.getName()).iterator().next()
    String username = (String) principals.getPrimaryPrincipal();
    //???
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.setRoles(baseUserService.findRole(username));
    authorizationInfo.setStringPermissions(baseUserService.findPermission(username));

    return authorizationInfo;
}

From source file:com.appleframework.pay.permission.shiro.realm.OperatorRealm.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from   www . j  a  v  a 2 s. c o m
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String loginName = (String) principals.getPrimaryPrincipal();

    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

    Subject subject = SecurityUtils.getSubject();
    Session session = subject.getSession();
    PmsOperator operator = (PmsOperator) session.getAttribute("PmsOperator");
    if (operator == null) {
        operator = pmsOperatorService.findOperatorByLoginName(loginName);
        session.setAttribute("PmsOperator", operator);
    }
    // ????
    Long operatorId = operator.getId();

    Set<String> roles = (Set<String>) session.getAttribute("ROLES");
    if (roles == null || roles.isEmpty()) {
        roles = pmsOperatorRoleService.getRoleCodeByOperatorId(operatorId);
        session.setAttribute("ROLES", roles);
    }
    // ?
    authorizationInfo.setRoles(roles);

    Set<String> permisstions = (Set<String>) session.getAttribute("PERMISSIONS");
    if (permisstions == null || permisstions.isEmpty()) {
        permisstions = pmsRolePermissionService.getPermissionsByOperatorId(operatorId);
        session.setAttribute("PERMISSIONS", permisstions);
    }
    // ?????
    authorizationInfo.setStringPermissions(permisstions);
    return authorizationInfo;
}

From source file:com.charmyin.shiro.realm.jdbc.CustomJdbcRealm.java

License:Apache License

/**
 * This implementation of the interface expects the principals collection to return a String username keyed off of
 * this realm's {@link #getName() name}/*from w  w w  .  j  a  v  a  2 s .  c o m*/
 *
 * @see #getAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    //null usernames are invalid
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }

    String username = (String) getAvailablePrincipal(principals);

    Connection conn = null;
    Set<String> roleNames = null;
    Set<String> permissions = null;
    try {
        conn = dataSource.getConnection();

        // Retrieve roles and permissions from database
        roleNames = getRoleNamesForUser(conn, username);
        if (permissionsLookupEnabled) {
            permissions = getPermissions(conn, username, roleNames);
        }

    } catch (SQLException e) {
        final String message = "There was a SQL error while authorizing user [" + username + "]";
        if (log.isErrorEnabled()) {
            log.error(message, e);
        }

        // Rethrow any SQL errors as an authorization exception
        throw new AuthorizationException(message, e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
    info.setStringPermissions(permissions);
    return info;
}

From source file:com.charmyin.shiro.realm.jdbc.JMongodbRealm.java

License:Apache License

/**
 * This implementation of the interface expects the principals collection to return a String username keyed off of
 * this realm's {@link #getName() name}//from   w  w  w . j  a va  2s  .co  m
 *
 * @see #getAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    //null usernames are invalid
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }

    String username = (String) getAvailablePrincipal(principals);

    Connection conn = null;
    Set<String> roleNames = null;
    Set<String> permissions = null;
    /* try {
    conn = dataSource.getConnection();
            
    // Retrieve roles and permissions from database
    roleNames = getRoleNamesForUser(conn, username);
    if (permissionsLookupEnabled) {
        permissions = getPermissions(conn, username, roleNames);
    }
            
     } catch (SQLException e) {
    final String message = "There was a SQL error while authorizing user [" + username + "]";
    if (log.isErrorEnabled()) {
        log.error(message, e);
    }
            
    // Rethrow any SQL errors as an authorization exception
    throw new AuthorizationException(message, e);
     } finally {
    JdbcUtils.closeConnection(conn);
     }*/

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
    info.setStringPermissions(permissions);
    return info;
}

From source file:com.cssnb.commons.shiro.MyJdbcRealm.java

License:Apache License

/**
 * This implementation of the interface expects the principals collection to return a String username keyed off of
 * this realm's {@link #getName() name}//from www  .jav  a2 s  .  co  m
 *
 * @see #getAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    //null usernames are invalid
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }

    //String username = (String) getAvailablePrincipal(principals);
    ShiroUser shiroUser = (ShiroUser) getAvailablePrincipal(principals);
    String username = shiroUser.getLoginName();
    Connection conn = null;
    Set<String> roleNames = null;
    Set<String> permissions = null;
    try {
        conn = dataSource.getConnection();

        // Retrieve roles and permissions from database
        roleNames = getRoleNamesForUser(conn, username);
        if (permissionsLookupEnabled) {
            permissions = getPermissions(conn, username, roleNames);
        }

    } catch (SQLException e) {
        final String message = "There was a SQL error while authorizing user [" + username + "]";
        if (log.isErrorEnabled()) {
            log.error(message, e);
        }

        // Rethrow any SQL errors as an authorization exception
        throw new AuthorizationException(message, e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
    info.setStringPermissions(permissions);
    return info;

}

From source file:com.cuisongliu.springboot.shiro.support.realm.ShiroAbstractRealm.java

License:Open Source License

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

    String username = (String) principalCollection.getPrimaryPrincipal();
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    String appKey = "";
    if (springShiroProperties.getEnableServer()) {
        appKey = springShiroProperties.getAppSuperKey();
    } else {/*  w  w  w . j  a  v a 2s  .com*/
        appKey = springShiroProperties.getAppKey();
    }
    UserInfo userInfo = userCache.selectUserInfoByUsername(appKey, username);
    authorizationInfo.setRoles(userInfo.getRoles());
    authorizationInfo.setStringPermissions(userInfo.getPermissions());
    return authorizationInfo;
}