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

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

Introduction

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

Prototype

public void setUsername(String username) 

Source Link

Document

Sets the username for submission during an authentication attempt.

Usage

From source file:ch.bastiangardel.easypay.dto.CredentialDTO.java

License:Open Source License

public UsernamePasswordToken daoToModel(String host) {
    UsernamePasswordToken tmp = new UsernamePasswordToken();
    tmp.setHost(host);/*from   w w w.ja va 2 s .  c o m*/
    tmp.setRememberMe(false);

    if (password != null)
        tmp.setPassword(password.toCharArray());
    else
        tmp.setPassword(null);

    tmp.setUsername(username);

    return tmp;
}

From source file:com.glaf.shiro.ShiroSecurity.java

License:Apache License

public static void login(String actorId, String password) {
    logger.info("login user:" + actorId);
    Subject currentUser = SecurityUtils.getSubject();
    try {/*from   w  w w  . j  a  va  2 s  .com*/
        UsernamePasswordToken token = new UsernamePasswordToken();
        token.setUsername(actorId);
        token.setPassword(actorId.toCharArray());
        token.setRememberMe(false);
        Session session = currentUser.getSession();
        session.setAttribute(Constants.LOGIN_ACTORID, actorId);
        currentUser.login(token);
        logger.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
    } catch (Exception ex) {
        ex.printStackTrace();
        logger.error(ex);
    }
}

From source file:com.plateform.admin.security.ShiroDbRealm.java

License:Apache License

/**
 * ?,./*from www.java2  s  .  c  om*/
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
    SecurityUser<Long> user = userHessianService.findUserByAccount(token.getUsername(), UserStaEnum.ENABLE);
    if (user != null) {
        token.setUsername(user.getLoginName());
        return new SimpleAuthenticationInfo(user, user.getPassWord(), getName());
    } else {
        return null;
    }
}

From source file:com.sonicle.webtop.core.app.shiro.WTRealm.java

License:Open Source License

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    if (token instanceof UsernamePasswordToken) {
        UsernamePasswordToken upt = (UsernamePasswordToken) token;

        String domainId = null;//from w w  w .  j a va2s.c o  m
        if (token instanceof UsernamePasswordDomainToken) {
            domainId = ((UsernamePasswordDomainToken) token).getDomain();
        }
        //logger.debug("isRememberMe={}",upt.isRememberMe());

        String sprincipal = (String) upt.getPrincipal();
        String internetDomain = StringUtils.lowerCase(StringUtils.substringAfterLast(sprincipal, "@"));
        String username = StringUtils.substringBeforeLast(sprincipal, "@");
        logger.trace("doGetAuthenticationInfo [{}, {}, {}]", domainId, internetDomain, username);

        Principal principal = authenticateUser(domainId, internetDomain, username, upt.getPassword());

        // Update token with new values resulting from authentication
        if (token instanceof UsernamePasswordDomainToken) {
            ((UsernamePasswordDomainToken) token).setDomain(principal.getDomainId());
        }
        upt.setUsername(principal.getUserId());

        return new WTAuthenticationInfo(principal, upt.getPassword(), this.getName());

    } else {
        return null;
    }
}

From source file:org.apache.metron.dataservices.auth.CustomDomainADRealm.java

License:Apache License

@Override
protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token,
        LdapContextFactory ldapContextFactory) throws NamingException {

    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String userName = upToken.getUsername();
    upToken.setUsername(userName + "@" + customDomain);

    return super.queryForAuthenticationInfo(token, ldapContextFactory);
}

From source file:org.opendaylight.aaa.shiro.filters.AuthenticationListenerTest.java

License:Open Source License

@Test
public void testOnSuccess() throws Exception {
    // sets up a successful authentication attempt
    final AuthenticationListener authenticationListener = new AuthenticationListener();
    final UsernamePasswordToken authenticationToken = new UsernamePasswordToken();
    authenticationToken.setUsername("successfulUser1");
    authenticationToken.setHost("successfulHost1");
    final SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
    // the following call produces accounting output
    authenticationListener.onSuccess(authenticationToken, simpleAuthenticationInfo);

    // grab the latest log output and make sure it is in line with what is expected
    final List<LoggingEvent> loggingEvents = TestAppender.getCurrentInstance().getEvents();
    // the latest logging event is the one we need to inspect
    final int whichLoggingEvent = loggingEvents.size() - 1;
    final LoggingEvent latestLoggingEvent = loggingEvents.get(whichLoggingEvent);
    final String latestLogMessage = latestLoggingEvent.getMessage();
    assertEquals("Successful authentication attempt by successfulUser1 from successfulHost1", latestLogMessage);
}

From source file:org.opendaylight.aaa.shiro.filters.AuthenticationListenerTest.java

License:Open Source License

@Test
public void testOnFailure() throws Exception {
    // variables for an unsucessful authentication attempt
    final AuthenticationListener authenticationListener = new AuthenticationListener();
    final UsernamePasswordToken authenticationToken = new UsernamePasswordToken();
    authenticationToken.setUsername("unsuccessfulUser1");
    authenticationToken.setHost("unsuccessfulHost1");
    final AuthenticationException authenticationException = new AuthenticationException("test auth exception");
    // produces unsuccessful authentication attempt output
    authenticationListener.onFailure(authenticationToken, authenticationException);

    // grab the latest log output and ensure it is in line with what is expected
    final List<LoggingEvent> loggingEvents = TestAppender.getCurrentInstance().getEvents();
    final int whichLoggingEvent = loggingEvents.size() - 1;
    final LoggingEvent latestLoggingEvent = loggingEvents.get(whichLoggingEvent);
    final String latestLogMessage = latestLoggingEvent.getMessage();
    assertEquals("Unsuccessful authentication attempt by unsuccessfulUser1 from unsuccessfulHost1",
            latestLogMessage);//  w ww  .j a  v  a 2 s .c  o m
}

From source file:org.opendaylight.aaa.shiro.filters.AuthenticationTokenUtilsTest.java

License:Open Source License

@Test
public void testExtractUsername() throws Exception {
    // null test//from  ww w  .  j  a v  a 2  s .  c  o  m
    final AuthenticationToken nullAuthenticationToken = null;
    assertEquals(AuthenticationTokenUtils.DEFAULT_TOKEN,
            AuthenticationTokenUtils.extractUsername(nullAuthenticationToken));

    // non-UsernamePasswordToken test
    final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken();
    assertEquals(AuthenticationTokenUtils.DEFAULT_TOKEN,
            AuthenticationTokenUtils.extractUsername(notUsernamePasswordToken));

    // null username test
    final UsernamePasswordToken nullUsername = new UsernamePasswordToken();
    nullUsername.setUsername(null);
    assertEquals(AuthenticationTokenUtils.DEFAULT_USERNAME,
            AuthenticationTokenUtils.extractUsername(nullUsername));

    // positive test
    final UsernamePasswordToken positiveUsernamePasswordToken = new UsernamePasswordToken();
    final String testUsername = "testUser1";
    positiveUsernamePasswordToken.setUsername(testUsername);
    assertEquals(testUsername, AuthenticationTokenUtils.extractUsername(positiveUsernamePasswordToken));
}

From source file:org.opendaylight.aaa.shiro.filters.AuthenticationTokenUtilsTest.java

License:Open Source License

@Test
public void testGenerateUnsuccessfulAuthenticationMessage() throws Exception {
    final UsernamePasswordToken unsuccessfulToken = new UsernamePasswordToken();
    unsuccessfulToken.setUsername("unsuccessfulUser1");
    unsuccessfulToken.setHost("unsuccessfulHost1");
    assertEquals("Unsuccessful authentication attempt by unsuccessfulUser1 from unsuccessfulHost1",
            AuthenticationTokenUtils.generateUnsuccessfulAuthenticationMessage(unsuccessfulToken));
}

From source file:org.opendaylight.aaa.shiro.filters.AuthenticationTokenUtilsTest.java

License:Open Source License

@Test
public void testGenerateSuccessfulAuthenticationMessage() throws Exception {
    final UsernamePasswordToken successfulToken = new UsernamePasswordToken();
    successfulToken.setUsername("successfulUser1");
    successfulToken.setHost("successfulHost1");
    assertEquals("Successful authentication attempt by successfulUser1 from successfulHost1",
            AuthenticationTokenUtils.generateSuccessfulAuthenticationMessage(successfulToken));
}