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

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

Introduction

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

Prototype

public IncorrectCredentialsException(Throwable cause) 

Source Link

Document

Constructs a new IncorrectCredentialsException.

Usage

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

License:Apache License

/**
 * ??//w w  w  . java  2 s .  c om
 */
@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.enioka.jqm.webui.shiro.JpaRealm.java

License:Open Source License

@Override
protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info)
        throws AuthenticationException {
    if (token instanceof CertificateToken) {
        if (!((CertificateToken) token).getUserName().equals(info.getPrincipals().getPrimaryPrincipal())) {
            throw new IncorrectCredentialsException("certificate presented did not match the awaited username");
        }//from   w ww .  j ava 2s  .c  om
        return;
    }
    super.assertCredentialsMatch(token, info);
}

From source file:com.migo.shiro.UserRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
        throws AuthenticationException {
    String username = (String) authenticationToken.getPrincipal();
    String password = new String((char[]) authenticationToken.getCredentials());

    //?/*ww w.  ja v  a 2  s .co  m*/
    SysUserEntity user = sysUserService.queryByUserName(username);

    //??
    if (user == null) {
        throw new UnknownAccountException("???");
    }

    //?
    if (!password.equals(user.getPassword())) {
        throw new IncorrectCredentialsException("???");
    }

    //??
    if (user.getStatus() == 0) {
        throw new LockedAccountException("??,??");
    }

    return new SimpleAuthenticationInfo(user, password, getName());
}

From source file:com.whale.eos.service.org.ShiroDbRealm.java

License:Apache License

public void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info)
        throws AuthenticationException {
    if (PropertyUtil.getBoolean("encrypt")) {
        super.assertCredentialsMatch(token, info);
    } else {//from  ww w  . j a  v  a 2s  .  co m
        if (token != null && info != null) {
            CaptchaUsernamePasswordToken tk = (CaptchaUsernamePasswordToken) token;
            if (!(String.valueOf(tk.getPassword())).equals((String) info.getCredentials())) {
                // not successful - throw an exception to indicate this:
                String msg = "Submitted credentials for token [" + tk
                        + "] did not match the expected credentials.";
                throw new IncorrectCredentialsException(msg);
            }
        } else {
            throw new AuthenticationException("A CredentialsMatcher must be configured in order to verify "
                    + "credentials during authentication.  If you do not wish for credentials to be examined, you "
                    + "can configure an " + AllowAllCredentialsMatcher.class.getName() + " instance.");
        }
    }
}

From source file:com.yiguang.payment.rbac.controller.ShiroDbRealm.java

License:Apache License

/**
 * ??//from w w  w  . j a va  2 s.  com
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    String userName = token.getUsername();
    String pwd = null;
    if (!StringUtil.isNullOrEmpty(userName)) {
        User user = userService.queryUserByName(userName);
        if (user != null) {
            if (CommonConstant.CommonStatus.CLOSE == user.getStatus()) {
                throw new LockedAccountException("?????");
            }

            String loginPwd = user.getPassword();

            pwd = String.valueOf(token.getPassword());
            String md5Password = securityKeystoreService.getEncryptKeyByJSRSAKey(pwd, user.getId());
            if (!md5Password.equals(loginPwd)) {
                throw new IncorrectCredentialsException("????");
            }
            token.setPassword(md5Password.toCharArray());
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user, loginPwd,
                    getName());
            return simpleAuthenticationInfo;
        } else {
            throw new UnknownAccountException("???!");
        }
    } else {
        throw new AuthenticationException("????");
    }
}

From source file:eu.eubrazilcc.lvl.storage.security.shiro.LinkedInRealm.java

License:EUPL

@Override
protected AuthenticationInfo doGetAuthenticationInfo(final AuthenticationToken token)
        throws AuthenticationException {
    // validate token
    if (token == null) {
        throw new CredentialsException("Uninitialized token");
    }/*from  w ww. j a v a  2 s.  com*/
    if (!(token instanceof AccessTokenToken)) {
        throw new UnsupportedTokenException("Unsuported token type: " + token.getClass().getCanonicalName());
    }
    // get access token
    final AccessTokenToken accessToken = (AccessTokenToken) token;
    final String secret = trimToNull(accessToken.getToken());
    if (isEmpty(secret)) {
        throw new AccountException("Empty tokens are not allowed in this realm");
    }
    // find token in the LVL OAuth2 database
    String ownerid = null;
    final AtomicReference<String> ownerIdRef = new AtomicReference<String>();
    if (TOKEN_DAO.isValid(secret, ownerIdRef)) {
        ownerid = ownerIdRef.get();
    }
    if (isEmpty(ownerid)) {
        throw new IncorrectCredentialsException("Incorrect credentials found");
    }
    // find resource owner in the LVL IdP database      
    final ResourceOwner owner = RESOURCE_OWNER_DAO.useGravatar(false).find(ownerid);
    if (owner == null || owner.getUser() == null) {
        throw new UnknownAccountException("No account found for user [" + ownerid + "]");
    }
    return new SimpleAuthenticationInfo(ownerid, secret, getName());
}

From source file:io.cassandrareaper.resources.auth.LoginResource.java

License:Apache License

@Path("/login")
@POST//from   w ww .j  a  v a  2  s. co  m
public void login(@FormParam("username") String username, @FormParam("password") String password,
        @Auth Subject subject) throws IOException {
    ensurePresent(username, "Invalid credentials: missing username.");
    ensurePresent(password, "Invalid credentials: missing password.");

    try {
        subject.login(new UsernamePasswordToken(username, password));
    } catch (AuthenticationException e) {
        throw new IncorrectCredentialsException("Invalid credentials combination for user: " + username);
    }
}

From source file:io.cassandrareaper.resources.auth.LoginResource.java

License:Apache License

private void ensurePresent(String value, String message) {
    if (StringUtils.isBlank(value)) {
        throw new IncorrectCredentialsException(message);
    }/*from w  w  w.  j  ava 2  s.  c o  m*/
}

From source file:io.github.howiefh.jeews.modules.sys.controller.LoginCotroller.java

License:Apache License

@RequestMapping(value = "", method = RequestMethod.POST)
public Map<String, Object> login(@RequestBody User u) {
    String username = u.getUsername();
    String password = u.getPassword();
    if (username == null) {
        throw new NullPointerException("????");
    }//from   w w  w . j  av  a 2  s  . c  o  m
    User user = userService.findByName(username);

    if (user == null) {
        throw new UnknownAccountException("??");// ??
    }

    if (Boolean.TRUE.equals(user.getLocked())) {
        throw new LockedAccountException("???"); // ???
    }

    if (!userService.passwordsMatch(user, password)) {
        throw new IncorrectCredentialsException("????");
    }

    JWTSigner signer = new JWTSigner(secret);
    Options options = new Options();
    // 7 * 24 * 60 * 60 = 604800
    options.setExpirySeconds(604800);
    Map<String, Object> claims = new HashMap<String, Object>();
    RolePermission rolePermission = user.new RolePermission();
    claims.put("perms", rolePermission.getPermissionSet());
    claims.put("iss", user.getUsername());
    String token = signer.sign(claims, options);

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("access_token", token);
    Map<String, Object> userMap = new HashMap<String, Object>();
    userMap.put("id", user.getId());
    userMap.put("username", user.getUsername());
    userMap.put("perms", rolePermission.getPermissionSet());
    userMap.put("roles", rolePermission.getRoleSet());
    map.put("user", userMap);
    return map;
}

From source file:no.priv.bang.ukelonn.web.security.dbrealm.UkelonnRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
    if (!(token instanceof UsernamePasswordToken)) {
        throw new AuthenticationException("UkelonnRealm shiro realm only accepts UsernamePasswordToken");
    }/* w  ww.  j av  a 2  s. co m*/

    UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
    Object principal = usernamePasswordToken.getPrincipal();
    String username = usernamePasswordToken.getUsername();
    try {
        try (PreparedStatement statement = database.prepareStatement("select * from users where username=?")) {
            statement.setString(1, username);
            ResultSet passwordResultSet = database.query(statement);
            if (passwordResultSet == null) {
                throw new AuthenticationException(
                        "UkelonnRealm shiro realm failed to get passwords from the database");
            }

            if (passwordResultSet.next()) {
                String password = passwordResultSet.getString("password");
                String salt = passwordResultSet.getString("salt");
                ByteSource decodedSalt = Util.bytes(Base64.getDecoder().decode(salt));
                return new SimpleAuthenticationInfo(principal, password, decodedSalt, getName());
            } else {
                throw new IncorrectCredentialsException("Username \"" + username + "\" not found");
            }
        }
    } catch (SQLException e) {
        throw new AuthenticationException(
                "UkelonnRealm shiro realm got SQL error exploring the password results", e);
    }
}