Example usage for org.apache.shiro.authc UsernamePasswordToken getUsername

List of usage examples for org.apache.shiro.authc UsernamePasswordToken getUsername

Introduction

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

Prototype

public String getUsername() 

Source Link

Document

Returns the username submitted during an authentication attempt.

Usage

From source file:$.ShiroDbRealm.java

License:Apache License

/**
     * ?,./*from  w w  w .  jav  a  2s . c om*/
     */
    @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:action.ShiroDbRealm.java

License:Apache License

/**
 * ?,.//  w w w  .  ja  v  a2  s .c o m
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    User user = userService.getByUserName(token.getUsername());
    if (user != null) {
        return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getUsername(), user.getName()),
                user.getPassword(), getName());
    } else {
        return null;
    }
}

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

License:Open Source License

/**
 * ?, //w  w  w . jav  a 2s  . 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:au.org.theark.core.security.AAFRealm.java

License:Open Source License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    SimpleAuthenticationInfo sai = null;
    ArkUserVO userVO = null;//from   ww  w  .  ja v a  2s  .  co  m
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    //log.info("IN AAFRealm.doGetAuthenticationInfo");
    //log.info("authToken: " + authcToken.getPrincipal().toString());
    log.info("AAF token username: " + token.getUsername());

    try {
        //log.info("checking user");
        userVO = iArkCommonService.getUser(token.getUsername().trim());
        if (userVO != null) {
            // Check if the user is in the Ark Database
            ArkUser arkUser = iArkCommonService.getArkUser(token.getUsername().trim());
            // Also check if the Ark User is linked with any study and has roles.
            // If no roles found, stop the user from logging in until an administrator has set it up
            if (!iArkCommonService.isArkUserLinkedToStudies(arkUser)) {
                throw new UnknownAccountException(UNKNOWN_ACCOUNT);
            }

            final WebRequest webRequest = (WebRequest) RequestCycle.get().getRequest();
            final HttpServletRequest httpReq = (HttpServletRequest) webRequest.getContainerRequest();

            //log.info("checking shib headers");
            String userName = httpReq.getHeader("AJP_mail");
            String password = httpReq.getHeader("AJP_Shib-Session-ID");

            if (userName != null && password != null) {
                //log.info("creating SimpleAuthenticationInfo");
                sai = new SimpleAuthenticationInfo(token.getPrincipal(), token.getCredentials(), getName());
            }
        }
    } catch (ArkSystemException e) {
        log.error(e.getMessage());
    } catch (EntityNotFoundException e) {
        throw new UnknownAccountException(UNKNOWN_ACCOUNT);
    }
    return sai;
}

From source file:au.org.theark.core.security.ArkLdapRealm.java

License:Open Source License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    SimpleAuthenticationInfo sai = null;
    ArkUserVO userVO = null;//from   w ww . java  2 s.  c om
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

    try {
        userVO = iArkCommonService.getUser(token.getUsername().trim());// Example to use core services to get user
        if (userVO != null) {
            // Check if the user is in the Ark Database
            ArkUser arkUser = iArkCommonService.getArkUser(token.getUsername().trim());
            // Also check if the Ark User is linked with any study and has roles.
            // If no roles found, stop the user from logging in until an administrator has set it up
            if (!iArkCommonService.isArkUserLinkedToStudies(arkUser)) {
                throw new UnknownAccountException(UNKNOWN_ACCOUNT);
            }

            sai = new SimpleAuthenticationInfo(userVO.getUserName(), userVO.getPassword(), getName());
        }
    } catch (ArkSystemException e) {
        log.error(e.getMessage());
    } catch (EntityNotFoundException e) {
        throw new UnknownAccountException(UNKNOWN_ACCOUNT);
    }
    return sai;
}

From source file:b4f.seguridad.ShiroAuthorizingRealm.java

@Override
public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authToken)
        throws AuthenticationException {

    System.out.println("ShiroAuthorizingRealm.doGetAuthenticationInfo()");

    //SE ACCEDI CON UN JWT TOKEN
    if (authToken instanceof JwtToken) {
        JwtToken jwt = (JwtToken) authToken;
        if (jwt.getToken() != null && !jwt.getToken().equals("")) {

            if (!jwt.validar()) {
                throw new AuthenticationException("Token invalido.");
            }/*from   w  ww.j av a 2 s .  c  o m*/

            try {
                Usuario user = UsersManager.getUser(jwt.getUser());
                AuthenticationInfo rta = new SimpleAuthenticationInfo(user.getUsuario(), user.getPassword(),
                        getName());
                return rta;
            } catch (Exception ex) {
                Logger.getLogger(ShiroAuthorizingRealm.class.getName()).log(Level.SEVERE, null, ex);
                throw new AuthenticationException(ex.getMessage());
            }

        } else {
            throw new AuthenticationException("Token invalido.");
        }
    }

    UsernamePasswordToken token = (UsernamePasswordToken) authToken;

    Usuario user;
    try {
        user = UsersManager.getUser(token.getUsername());

    } catch (Exception ex) {
        System.err.println("Error looking up user: " + ex.getMessage());
        throw new AuthenticationException("Usuario '" + token.getUsername() + "' no encontrado", ex);
    }

    if (user != null) {
        System.out.println("Returning user " + user.getUsuario() + " password " + user.getPassword());
        return new SimpleAuthenticationInfo(user.getUsuario(), user.getPassword(), getName());

    } else {
        System.err.println("Usuarioname not found: " + token.getUsername());
        throw new AuthenticationException("User not found: " + token.getUsername());
    }
}

From source file:base.web.ShiroDbRealm.java

License:Apache License

/**
 * ?,.//from   w  w  w  . j a  va  2  s.  c o m
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    User user = userService.getByUserName(token.getUsername());
    if (user != null) {
        return new SimpleAuthenticationInfo(
                new ShiroUser(user.getId(), user.getUsername(), user.getName(), user.getSource()),
                user.getPassword(), getName());
    } else {
        return null;
    }
}

From source file:be.atbash.ee.security.octopus.book.ex1.ApplicationSecurityData.java

License:Apache License

@Override
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) {
    if (authenticationToken instanceof UsernamePasswordToken) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;

        AuthenticationInfoBuilder authenticationInfoBuilder = new AuthenticationInfoBuilder();
        authenticationInfoBuilder.principalId(principalId++)
                .name(authenticationToken.getPrincipal().toString());
        // TODO: Change for production. Here we use username as password
        authenticationInfoBuilder.password(usernamePasswordToken.getUsername());

        return authenticationInfoBuilder.build();
    }/*w ww  . j a  v a 2  s .co m*/

    return null;
}

From source file:be.atbash.ee.security.octopus.book.ex2.ApplicationSecurityData.java

License:Apache License

@Override
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) {
    if (authenticationToken instanceof UsernamePasswordToken) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;

        AuthenticationInfoBuilder authenticationInfoBuilder = new AuthenticationInfoBuilder();
        authenticationInfoBuilder.principalId(principalId++)
                .name(authenticationToken.getPrincipal().toString());
        authenticationInfoBuilder.userName(authenticationToken.getPrincipal().toString());
        // TODO: Change for production. Here we use username as password
        authenticationInfoBuilder.password(usernamePasswordToken.getUsername());

        return authenticationInfoBuilder.build();
    }//from   w w w. j  a  v  a2s.  c o  m

    return null;
}

From source file:be.atbash.ee.security.octopus.book.ex3.ApplicationSecurityData.java

License:Apache License

@Override
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) {
    if (authenticationToken instanceof UsernamePasswordToken) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;

        if (authenticationToken.getPrincipal().toString().length() > 2) {
            AuthenticationInfoBuilder authenticationInfoBuilder = new AuthenticationInfoBuilder();
            authenticationInfoBuilder.principalId(principalId++)
                    .name(authenticationToken.getPrincipal().toString());
            authenticationInfoBuilder.userName(authenticationToken.getPrincipal().toString());
            authenticationInfoBuilder.name(authenticationToken.getPrincipal().toString());
            // TODO: Change for production. Here we use username as password
            authenticationInfoBuilder.password(usernamePasswordToken.getUsername());

            return authenticationInfoBuilder.build();
        }//from   www .java 2 s . c  o m
    }

    return null;
}