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

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

Introduction

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

Prototype

public SimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSalt,
        String realmName) 

Source Link

Document

Constructor that takes in a single 'primary' principal of the account, its corresponding hashed credentials, the salt used to hash the credentials, and the name of the realm to associate with the principals.

Usage

From source file:$.ShiroDbRealm.java

License:Apache License

/**
     * ?,./* w ww .  j  ava2 s. co  m*/
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        User user = accountService.findUserByLoginName(token.getUsername());
        if (user != null) {
            byte[] salt = Encodes.decodeHex(user.getSalt());
            return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getLoginName(), user.getName()),
                    user.getPassword(), ByteSource.Util.bytes(salt), getName());
        } else {
            return null;
        }
    }

From source file:apm.modules.sys.security.SystemAuthorizingRealm.java

License:Open Source License

/**
 * ?, /*from  w w w  . j ava2  s .c  om*/
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

    if (LoginController.isValidateCodeLogin(token.getUsername(), false, false)) {
        // ??
        Session session = SecurityUtils.getSubject().getSession();
        String code = (String) session.getAttribute(ValidateCodeServlet.VALIDATE_CODE);
        if (token.getCaptcha() == null || !token.getCaptcha().toUpperCase().equals(code)) {
            throw new CaptchaException("??.");
        }
    }

    User user = getUserService().findByLoginName(token.getUsername());
    if (user != null) {
        byte[] salt = Encodes.decodeHex(user.getPassword().substring(0, 16));
        return new SimpleAuthenticationInfo(new Principal(user), user.getPassword().substring(16),
                ByteSource.Util.bytes(salt), getName());
    } else {
        return null;
    }
}

From source file:cc.rainier.fss.service.account.ShiroDbRealm.java

License:Apache License

/**
 * ?,./*from   w w  w .  ja va 2s .com*/
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    User user = accountService.findUserByLoginName(token.getUsername());
    if (user != null) {
        byte[] salt = Encodes.decodeHex(user.getSalt());
        return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getLoginName(), user.getName()),
                user.getPassword(), ByteSource.Util.bytes(salt), getName());
    } else {
        return null;
    }
}

From source file:cc.sion.base.auth.shiro.ShiroDbRealm.java

License:Apache License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    SysUser user = accountService.findUserByLoginName(token.getUsername());
    if (user != null) {
        System.out.println("---user name:" + user.getName());
        return new SimpleAuthenticationInfo(token.getUsername(), //??
                user.getPassword(), //?
                ByteSource.Util.bytes(user.getCredentialSalt()), //salt=username+salt
                getName());//realm name
    } else {// w ww . ja v  a  2  s . c  o  m
        return null;
    }
}

From source file:cn.cdwx.jpa.service.account.ShiroDbRealm.java

License:Apache License

/**
 * ?,./*from   ww  w. j a  v a  2  s  . co  m*/
 */

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    User user = accountService.findUserByLoginName(token.getUsername());
    if (user != null) {
        byte[] salt = Encodes.decodeHex(user.getSalt());
        return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getLoginName(), user.getName()),
                user.getPassword(), ByteSource.Util.bytes(salt), getName());
    } else {
        return null;
    }
}

From source file:cn.com.axiom.system.security.ShiroDbRealm.java

License:Apache License

/**
 * ?,./*  w ww.  j a v  a2 s.  c  om*/
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    User user = userService.findUserByUserName(token.getUsername());
    if (user != null) {
        if (user.getStatus() != 1) {
            throw new DisabledAccountException();
        }
        // sysLogService.log("","",user.getUserName(), SysLog.INFO,
        // token.getHost(),SysLog.USER);
        SimpleByteSource salt = (SimpleByteSource) ByteSource.Util.bytes(user.getUserName());
        String passwordMd5 = new Md5Hash(user.getPassword().getBytes(), salt).toString();
        return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getUserName(), user.getRealName()),
                passwordMd5, ByteSource.Util.bytes(user.getUserName()), getName());
    } else {
        return null;
    }

}

From source file:cn.com.infcn.ade.system.service.UserRealm.java

/**
 * ?,./*ww  w  . ja  va  2 s.c o  m*/
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    try {
        checkHandler.checkExpireDate();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        throw new LicenseException(e.getMessage(), e);
    }
    UsernamePasswordCaptchaToken token = (UsernamePasswordCaptchaToken) authcToken;
    User user = userService.getUser(token.getUsername());

    if (user != null && doCaptchaValidate(token)) {
        byte[] salt = Encodes.decodeHex(user.getSalt());
        ShiroUser shiroUser = new ShiroUser(user.getId(), user.getLoginName(), user.getName());
        //session
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute("user", user);
        return new SimpleAuthenticationInfo(shiroUser, user.getPassword(), ByteSource.Util.bytes(salt),
                getName());
    } else {
        return null;
    }
}

From source file:cn.com.qiqi.order.web.system.security.ShiroDbRealm.java

License:Apache License

/**
 * ?,.//www.  j  av a 2 s . c  o  m
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    User user = userService.findUserByUserName(token.getUsername());
    if (user != null) {
        if (user.getStatus() != 1) {
            Subject subject = SecurityUtils.getSubject();
            subject.getSession().setAttribute(Constants.CURRENT_USER_NAME, user.getUserName());
            throw new DisabledAccountException();
        }
        String md5 = Encodes.encodeHex(
                Digests.md5(String.valueOf(token.getPassword()).getBytes(), user.getUserName().getBytes(), 1));
        if (!user.getPassword().equals(md5)) {
            throw new IncorrectCredentialsException();
        }
        sysLogService.log("", "", user.getUserName(), SysLog.INFO,
                token.getHost(), SysLog.USER);

        Subject subject = SecurityUtils.getSubject();
        subject.getSession().setAttribute(Constants.CURRENT_USER_NAME, user.getUserName());

        return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getUserName(), user.getRealName()),
                user.getPassword(), ByteSource.Util.bytes(user.getUserName()), getName());
    } else {
        throw new UnknownAccountException();
    }

}

From source file:cn.com.xl.core.shiro.DefaultShiroFactory.java

License:Apache License

public SimpleAuthenticationInfo info(ShiroUser shiroUser, User user, String realmName) {
    String credentials = user.getPassword();
    // ??//  ww w .  jav  a2s.  co m
    String source = user.getSalt();
    ByteSource credentialsSalt = new Md5Hash(source);
    return new SimpleAuthenticationInfo(shiroUser, credentials, credentialsSalt, realmName);
}

From source file:cn.ilongfei.shiro.util.ShiroRealmImpl.java

License:Apache License

/**
 * AuthenticationInfo represents a Subject's (aka user's) stored account
 * information relevant to the authentication/log-in process only.
 *//*from   w  ww.  j  a  va  2  s . co m*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    /**
     * Constructor that takes in a single 'primary' principal of the
     * account, its corresponding hashed credentials, the salt used to hash
     * the credentials, and the name of the realm to associate with the
     * principals. This is a convenience constructor and will construct a
     * PrincipalCollection based on the principal and realmName argument.
     * 
     * 
     * Parameters:
     * 
     * principal - the 'primary' principal associated with the specified
     * realm. hashedCredentials - the hashed credentials that verify the
     * given principal. credentialsSalt - the salt used when hashing the
     * given hashedCredentials realmName - the realm from where the
     * principal and credentials were acquired.
     */
    UsernamePasswordToken usernamePasswordToke = (UsernamePasswordToken) token;

    String username = usernamePasswordToke.getUsername();

    String encodedPassword = new Sha256Hash(usernamePasswordToke.getPassword()).toBase64();

    System.out.println("====================?  begin ==========================");
    System.out.println("username: " + username);
    System.out.println("password: " + encodedPassword);
    System.out.println("principal: " + usernamePasswordToke.getPrincipal());
    System.out.println("======================?  end ========================");

    return new SimpleAuthenticationInfo(new ShiroUser("admin", "00011", ""),
            String.valueOf(usernamePasswordToke.getPassword()), ByteSource.Util.bytes("admin"), getName());
}