Example usage for org.apache.shiro.authc AuthenticationException AuthenticationException

List of usage examples for org.apache.shiro.authc AuthenticationException AuthenticationException

Introduction

In this page you can find the example usage for org.apache.shiro.authc AuthenticationException AuthenticationException.

Prototype

public AuthenticationException(String message, Throwable cause) 

Source Link

Document

Constructs a new AuthenticationException.

Usage

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

License:Apache License

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername();

    // Null username is invalid
    if (username == null) {
        throw new AccountException("Null usernames are not allowed by this realm.");
    }/*from  www. j  a va 2 s  . com*/

    Connection conn = null;
    SimpleAuthenticationInfo info = null;
    try {
        conn = dataSource.getConnection();

        String password = null;
        String salt = null;
        switch (saltStyle) {
        case NO_SALT:
            password = getPasswordForUser(conn, username)[0];
            break;
        case CRYPT:
            // TODO: separate password and hash from getPasswordForUser[0]
            throw new ConfigurationException("Not implemented yet");
            //break;
        case COLUMN:
            String[] queryResults = getPasswordForUser(conn, username);
            password = queryResults[0];
            salt = queryResults[1];
            break;
        case EXTERNAL:
            password = getPasswordForUser(conn, username)[0];
            salt = getSaltForUser(username);
        }

        if (password == null) {
            throw new UnknownAccountException("No account found for user [" + username + "]");
        }

        info = new SimpleAuthenticationInfo(username, password.toCharArray(), getName());

        if (salt != null) {
            info.setCredentialsSalt(ByteSource.Util.bytes(salt));
        }

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

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

    return info;
}

From source file:cn.guoyukun.spring.shiro.realm.AbstractUserPasswordRealm.java

License:Apache License

/**
 * ??//  w  w  w . j  a va  2  s  .c o m
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upt = (UsernamePasswordToken) token;
    // 
    String identify = upt.getUsername().trim();
    try {
        // ?
        SystemAccount account = getAccountByLoginIdentify(identify);
        // ?
        if (account == null) {
            throw new UnknownAccountException("[" + identify + "]??");
        }
        if (account.isLocked()) {
            throw new LockedAccountException("[" + identify + "]????");
        }
        //         LOG.debug("[{}]???:[{}]",identify,account.getCredentials());
        SimpleAuthenticationInfo sai = new SimpleAuthenticationInfo(account.getIdentify(),
                account.getCredentials(), this.getName());
        if (!getCredentialsMatcher().doCredentialsMatch(token, sai)) {
            throw new IncorrectCredentialsException("?");
        }
        LOG.debug("[{}]?", identify);
        return sai;
    } catch (AuthenticationException ae) {
        throw ae;
    } catch (Exception e) {
        throw new AuthenticationException("?[" + identify + "]?", e);
    }
}

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

License:Apache License

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername();

    // Null username is invalid
    if (username == null) {
        throw new AccountException("Null usernames are not allowed by this realm.");
    }//from  w  w w .  j a va2  s. c om

    SimpleAuthenticationInfo info = null;
    try {
        //conn = dataSource.getConnection();

        String password = null;
        String salt = null;
        switch (saltStyle) {
        case NO_SALT:
            password = getPasswordForUser(username)[0];
            break;
        case CRYPT:
            // TODO: separate password and hash from getPasswordForUser[0]
            throw new ConfigurationException("Not implemented yet");
            //break;
        case COLUMN:
            String[] queryResults = getPasswordForUser(username);
            password = queryResults[0];
            salt = queryResults[1];
            break;
        case EXTERNAL:
            password = getPasswordForUser(username)[0];
            salt = getSaltForUser(username);
        }

        if (password == null) {
            throw new UnknownAccountException("No account found for user [" + username + "]");
        }

        info = new SimpleAuthenticationInfo(username, password.toCharArray(), getName());

        if (salt != null) {
            info.setCredentialsSalt(ByteSource.Util.bytes(salt));
        }

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

        // Rethrow any SQL errors as an authentication exception
        throw new AuthenticationException(message, e);
    }

    return info;
}

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

License:Apache License

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    //UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    CaptchaUsernamePasswordToken upToken = (CaptchaUsernamePasswordToken) token;

    //?? ?/*from  ww  w . j a  va 2  s  . c o  m*/
    String captcha = null;
    Object obj_captcha = SecurityUtils.getSubject().getSession().getAttribute(Constants.CAPTCHA_KEY);
    //Object obj_count = SecurityUtils.getSubject().getSession().getAttribute( "login_fail_count" );
    //int failed_count = (obj_count ==null || !(obj_count instanceof Integer))?0:(Integer)obj_count;
    if (obj_captcha instanceof String)
        captcha = (String) obj_captcha;
    log.debug("you input:{},img:{}", upToken.getCaptcha(), captcha);
    if (captcha != null
            //&& failed_count >0
            && !captcha.equalsIgnoreCase(upToken.getCaptcha())) {
        throw new IncorrectCaptchaException("???");
    }

    String username = upToken.getUsername();

    // Null username is invalid
    if (username == null) {
        throw new AccountException("Null usernames are not allowed by this realm.");
    }

    Connection conn = null;
    SimpleAuthenticationInfo info = null;
    try {
        conn = dataSource.getConnection();

        String password = null;
        String salt = null;
        switch (saltStyle) {
        case NO_SALT:
            password = getPasswordForUser(conn, username)[0];
            break;
        case CRYPT:
            // TODO: separate password and hash from getPasswordForUser[0]
            throw new ConfigurationException("Not implemented yet");
            //break;
        case COLUMN:
            String[] queryResults = getPasswordForUser(conn, username);
            password = queryResults[0];
            salt = queryResults[1];
            break;
        case EXTERNAL:
            password = getPasswordForUser(conn, username)[0];
            salt = getSaltForUser(username);
        }

        if (password == null) {
            throw new UnknownAccountException("No account found for user [" + username + "]");
        }

        info = new SimpleAuthenticationInfo(new ShiroUser(username, username), password.toCharArray(),
                getName());

        if (salt != null) {
            info.setCredentialsSalt(ByteSource.Util.bytes(salt));
        }

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

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

    return info;
}

From source file:com.devcru.shirosandbox.realm.SaltAwareJdbcRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername();

    // Null username is invalid
    if (username == null) {
        throw new AccountException("Null usernames are not allowed by this realm.");
    }//from   w  w w .  j  ava2 s.com

    Connection conn = null;
    AuthenticationInfo info = null;
    try {
        conn = dataSource.getConnection();

        String password = getPasswordForUser(conn, username);

        if (password == null) {
            throw new UnknownAccountException("No account found for user [" + username + "]");
        }

        SimpleAuthenticationInfo saInfo = new SimpleAuthenticationInfo(username, password, getName());
        /**
         * This (very bad) example uses the username as the salt in this sample app.  DON'T DO THIS IN A REAL APP!
         *
         * Salts should not be based on anything that a user could enter (attackers can exploit this).  Instead
         * they should ideally be cryptographically-strong randomly generated numbers.
         */
        saInfo.setCredentialsSalt(ByteSource.Util.bytes(username));

        info = saInfo;

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

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

    return info;
}

From source file:com.digitalplay.network.ireader.shiro.ShiroDbRealm.java

License:Apache License

/**
 * ?,.//  w  ww. j a v  a2 s  .co  m
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) authcToken;
    String username = upToken.getUsername().trim();
    String password = "";
    if (upToken.getPassword() != null) {
        password = new String(upToken.getPassword());
    }

    User user = null;
    try {
        user = userService.login(username, password);
    } catch (UserNotExistsException e) {
        throw new UnknownAccountException(e.getMessage(), e);
    } catch (UserPasswordNotMatchException e) {
        throw new AuthenticationException(e.getMessage(), e);
    } catch (UserPasswordRetryLimitExceedException e) {
        throw new ExcessiveAttemptsException(e.getMessage(), e);
    } catch (UserBlockedException e) {
        throw new LockedAccountException(e.getMessage(), e);
    } catch (Exception e) {
        throw new AuthenticationException(new UserException("user.unknown.error", null));
    }

    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getUsername(), password.toCharArray(),
            getName());
    return info;
}

From source file:com.github.pires.example.shiro.SMRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    final String email = upToken.getUsername();

    // null email is invalid
    if (email == null) {
        throw new AccountException("Null email is not allowed by this realm.");
    }//ww w. ja v a2 s. c  om

    Connection conn = null;
    SimpleAuthenticationInfo info = null;
    try {
        conn = dataSource.getConnection();
        String password = null;
        String salt = null;
        switch (saltStyle) {
        case NO_SALT:
            password = getPasswordForUser(conn, email)[0];
            break;
        case CRYPT:
            // TODO: separate password and hash from getPasswordForUser[0]
            throw new ConfigurationException("Not implemented yet");
            // break;
        case COLUMN:
            String[] queryResults = getPasswordForUser(conn, email);
            password = queryResults[0];
            salt = queryResults[1];
            break;
        case EXTERNAL:
            password = getPasswordForUser(conn, email)[0];
            salt = getSaltForUser(email);
        }

        if (password == null) {
            throw new UnknownAccountException("No account found for user identified by [" + email + "]");
        }
        info = new SimpleAuthenticationInfo(email, password.toCharArray(), getName());
        if (salt != null) {
            info.setCredentialsSalt(ByteSource.Util.bytes(salt));
        }
    } catch (SQLException e) {
        final String message = "There was a SQL error while authenticating user identified by [" + email + "]";
        logger.error(message, e);
        // rethrow any SQL errors as an authentication exception
        throw new AuthenticationException(message, e);
    } finally {
        JdbcUtils.closeConnection(conn);
    }

    return info;
}

From source file:com.greenline.hrs.admin.auth.realm.WebRealm.java

License:Open Source License

/**
 * Retrieves authentication data from an implementation-specific datasource (RDBMS, LDAP, etc) for the given
 * authentication token.//  w ww  .  j  a  va  2s  .c o  m
 * <p/>
 * For most datasources, this means just 'pulling' authentication data for an associated subject/user and nothing
 * more and letting Shiro do the rest.  But in some systems, this method could actually perform EIS specific
 * log-in logic in addition to just retrieving data - it is up to the Realm implementation.
 * <p/>
 * A {@code null} return value means that no account could be associated with the specified token.
 *
 * @param token the authentication token containing the user's principal and credentials.
 * @return an {@link org.apache.shiro.authc.AuthenticationInfo} object containing account data resulting from the
 * authentication ONLY if the lookup is successful (i.e. account exists and is valid, etc.)
 * @throws org.apache.shiro.authc.AuthenticationException if there is an error acquiring data or performing
 *                                                        realm-specific authentication logic for the specified <tt>token</tt>
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername();
    String encryptPasswd = new String(upToken.getPassword());
    SimpleAuthenticationInfo authInfo = null;
    UserPassport userPassport = null;
    try {
        userPassport = manUserService.getUserPassportFromEncryptedPwd(Long.valueOf(username), encryptPasswd);
    } catch (Exception e) {
        LOG.error(AuthMessageConstants.AUTHORICATION_EXCEPTION, e);
        throw new AuthenticationException(AuthMessageConstants.AUTHORICATION_EXCEPTION, e);
    }
    if (userPassport == null) {
        throw new AuthenticationException(AuthMessageConstants.USER_PWD_ILLEGAL);
    }
    authInfo = new SimpleAuthenticationInfo(username, userPassport.getPassword(), getName());
    authInfo.setCredentialsSalt(ByteSource.Util.bytes(userPassport.getSalt()));
    return authInfo;
}

From source file:com.huntering.security.UserRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername().trim();
    String password = "";
    if (upToken.getPassword() != null) {
        password = new String(upToken.getPassword());
    }/* w  ww  .j a  v a2 s  .  c  om*/

    Account account = null;
    try {
        account = accountService.login(username, password);
    } catch (UserNotExistsException e) {
        throw new UnknownAccountException(e.getMessage(), e);
    } catch (UserPasswordNotMatchException e) {
        throw new AuthenticationException(e.getMessage(), e);
    } catch (UserPasswordRetryLimitExceedException e) {
        throw new ExcessiveAttemptsException(e.getMessage(), e);
    } catch (UserBlockedException e) {
        throw new LockedAccountException(e.getMessage(), e);
    } catch (Exception e) {
        log.error("login error", e);
        throw new AuthenticationException(new UserException("user.unknown.error", null));
    }

    String name = username;
    for (Email email : account.getEmails()) {
        if (Boolean.TRUE.equals(email.getMain())) {
            name = email.getEmail();
            break;
        }
    }
    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(name, password.toCharArray(), getName());
    return info;
}

From source file:com.ikanow.aleph2.security.service.CoreRealm.java

License:Apache License

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String principalName = upToken.getUsername();

    // Null username is invalid
    if (principalName == null) {
        throw new AccountException("Null usernames are not allowed by this realm.");
    }//from  w ww .  j a va2 s .c om

    AuthenticationInfo info = null;
    try {

        AuthorizationBean b = authProvider.getAuthBean(principalName);
        info = new CoreAuthenticationInfo(b, getName());

    } catch (Exception e) {
        final String message = "There was an error while authenticating user [" + principalName + "]";
        logger.error(message, e);

        // Rethrow any errors as an authentication exception
        throw new AuthenticationException(message, e);
    } finally {
        // TODO close connection?
    }

    return info;
}