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

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

Introduction

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

Prototype

public UnknownAccountException() 

Source Link

Document

Creates a new UnknownAccountException.

Usage

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

License:Apache License

/**
 * ?,./* w  w  w.  ja va 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 User user(String account) {
    User user = Blade.create(User.class).findFirstBy("account = #{account}",
            CMap.init().set("account", account));
    // ??//from w  ww  . j ava 2  s. co  m
    if (null == user) {
        throw new UnknownAccountException();
    }
    // ?
    if (user.getStatus() == 3 || user.getStatus() == 4) {
        throw new DisabledAccountException();
    }
    // ?
    if (user.getStatus() == 2 || user.getStatus() == 5) {
        throw new DisabledAccountException();
    }
    return user;
}

From source file:cn.itganhuo.app.service.impl.UserServiceImpl.java

License:Apache License

public User loadByAccount(String account) {
    User d_user = null;/*from  w ww .  j av  a  2s  . c  o m*/
    if (StringUtil.hasText(account)) {
        // 1??????
        d_user = userDao.loadByAccount(account);
        if (d_user == null) {
            throw new UnknownAccountException();
        }
        if (0 != d_user.getIsLock()) {
            throw new LockedAccountException();
        }
    }
    return d_user;
}

From source file:cn.ligoo.part.service.shiro.CustomAuthorizingRealm.java

License:Apache License

/**
 * ?,.//w  w w . j  av a 2s  . c  om
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
        throws AuthenticationException {
    logger.debug("...CustomAuthorizingRealm.doGetAuthenticationInfo()");
    CustomToken token = (CustomToken) authcToken;

    String username = token.getUsername();
    if (username == null) {
        throw new AccountException();
    }

    UserInfo user = userInfoService.findByEmail(username);
    if (user == null) {
        throw new UnknownAccountException();
    }

    if (user.getIs_del() == Constants.BYTE_1) {
        throw new DisabledAccountException();
    }
    return new SimpleAuthenticationInfo(new ShiroUser(user.getId(), user.getEmail()), user.getPassword(),
            getName());

}

From source file:cn.mario256.blog.AuthenticationRealm.java

License:Open Source License

/**
 * ???//  w w w .j a v  a2  s  .co  m
 * 
 * @param token
 *            
 * @return ??
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken token) {
    AuthenticationToken authenticationToken = (AuthenticationToken) token;
    String username = authenticationToken.getUsername();
    String password = new String(authenticationToken.getPassword());
    String captchaId = authenticationToken.getCaptchaId();
    String captcha = authenticationToken.getCaptcha();
    String ip = authenticationToken.getHost();
    if (!captchaService.isValid(Setting.CaptchaType.adminLogin, captchaId, captcha)) {
        throw new IncorrectCaptchaException();
    }
    if (username != null && password != null) {
        Admin admin = adminService.findByUsername(username);
        if (admin == null) {
            throw new UnknownAccountException();
        }
        if (!admin.getIsEnabled()) {
            throw new DisabledAccountException();
        }
        Setting setting = SystemUtils.getSetting();
        if (admin.getIsLocked()) {
            if (ArrayUtils.contains(setting.getAccountLockTypes(), Setting.AccountLockType.admin)) {
                int loginFailureLockTime = setting.getAccountLockTime();
                if (loginFailureLockTime == 0) {
                    throw new LockedAccountException();
                }
                Date lockedDate = admin.getLockedDate();
                Date unlockDate = DateUtils.addMinutes(lockedDate, loginFailureLockTime);
                if (new Date().after(unlockDate)) {
                    admin.setLoginFailureCount(0);
                    admin.setIsLocked(false);
                    admin.setLockedDate(null);
                    adminService.update(admin);
                } else {
                    throw new LockedAccountException();
                }
            } else {
                admin.setLoginFailureCount(0);
                admin.setIsLocked(false);
                admin.setLockedDate(null);
                adminService.update(admin);
            }
        }
        if (!DigestUtils.md5Hex(password).equals(admin.getPassword())) {
            int loginFailureCount = admin.getLoginFailureCount() + 1;
            if (loginFailureCount >= setting.getAccountLockCount()) {
                admin.setIsLocked(true);
                admin.setLockedDate(new Date());
            }
            admin.setLoginFailureCount(loginFailureCount);
            adminService.update(admin);
            throw new IncorrectCredentialsException();
        }
        admin.setLoginIp(ip);
        admin.setLoginDate(new Date());
        admin.setLoginFailureCount(0);
        adminService.update(admin);
        return new SimpleAuthenticationInfo(new Principal(admin.getId(), username), password, getName());
    }
    throw new UnknownAccountException();
}

From source file:cn.mypandora.shiro.realm.UserRealm.java

License:Apache License

/**
 * ??subject//  w ww. ja  va  2  s . co  m
 *
 * @param token
 * @return
 * @throws AuthenticationException
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    String username = upToken.getUsername().trim();
    BaseUser user = baseUserService.findUserByUsername(username);
    if (user == null) {
        throw new UnknownAccountException();//??
    }
    if (Boolean.TRUE.equals(user.getLocked())) {
        throw new LockedAccountException(); //???
    }

    //AuthenticatingRealmCredentialsMatcher????
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getUsername(), //??
            user.getPassword(), //?
            ByteSource.Util.bytes(user.getCredentialsSalt()), //salt=username+salt
            getName() //realm name
    );
    return authenticationInfo;
}

From source file:com.app.AuthenticationRealm.java

License:Open Source License

/**
 * ???//from w  ww .ja  v a2s .c om
 * 
 * @param token
 *            
 * @return ??
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken token) {
    AuthenticationToken authenticationToken = (AuthenticationToken) token;
    String username = authenticationToken.getUsername();
    String password = new String(authenticationToken.getPassword());
    String captchaId = authenticationToken.getCaptchaId();
    String captcha = authenticationToken.getCaptcha();
    String ip = authenticationToken.getHost();
    if (!captchaService.isValid(CaptchaType.adminLogin, captchaId, captcha)) {
        throw new UnsupportedTokenException();
    }
    if (username != null && password != null) {
        Admin admin = adminService.findByUsername(username);
        if (admin == null) {
            throw new UnknownAccountException();
        }
        if (!admin.getIsEnabled()) {
            throw new DisabledAccountException();
        }
        Setting setting = SettingUtils.get();
        if (admin.getIsLocked()) {
            if (ArrayUtils.contains(setting.getAccountLockTypes(), AccountLockType.admin)) {
                int loginFailureLockTime = setting.getAccountLockTime();
                if (loginFailureLockTime == 0) {
                    throw new LockedAccountException();
                }
                Date lockedDate = admin.getLockedDate();
                Date unlockDate = DateUtils.addMinutes(lockedDate, loginFailureLockTime);
                if (new Date().after(unlockDate)) {
                    admin.setLoginFailureCount(0);
                    admin.setIsLocked(false);
                    admin.setLockedDate(null);
                    adminService.update(admin);
                } else {
                    throw new LockedAccountException();
                }
            } else {
                admin.setLoginFailureCount(0);
                admin.setIsLocked(false);
                admin.setLockedDate(null);
                adminService.update(admin);
            }
        }
        if (!DigestUtils.md5Hex(password).equals(admin.getPassword())) {
            int loginFailureCount = admin.getLoginFailureCount() + 1;
            if (loginFailureCount >= setting.getAccountLockCount()) {
                admin.setIsLocked(true);
                admin.setLockedDate(new Date());
            }
            admin.setLoginFailureCount(loginFailureCount);
            adminService.update(admin);
            throw new IncorrectCredentialsException();
        }
        admin.setLoginIp(ip);
        admin.setLoginDate(new Date());
        admin.setLoginFailureCount(0);
        adminService.update(admin);
        return new SimpleAuthenticationInfo(new Principal(admin.getId(), username), password, getName());
    }
    throw new UnknownAccountException();
}

From source file:com.app.test.controller.UserControllerTest.java

License:Open Source License

@Test
public void testPostLogInWithUnknownAccountException() throws Exception {
    PowerMockito.spy(SecurityUtils.class);

    Session session = new SimpleSession();

    Subject mockSubject = Mockito.mock(Subject.class);

    PowerMockito.doReturn(mockSubject).when(SecurityUtils.class, "getSubject");

    PowerMockito.doReturn(session).when(mockSubject).getSession();

    Mockito.doThrow(new UnknownAccountException()).when(mockSubject)
            .login(Mockito.any(AuthenticationToken.class));

    MockHttpServletRequestBuilder request = post("/log_in");

    request.param("emailAddress", "test@test.com");
    request.param("password", "password");

    this.mockMvc.perform(request).andExpect(status().is3xxRedirection())
            .andExpect(view().name("redirect:log_in")).andExpect(redirectedUrl("log_in"))
            .andExpect(flash().attributeExists("error"))
            .andExpect(flash().attribute("error", LanguageUtil.getMessage("log-in-failure")));
}

From source file:com.appleframework.pay.permission.shiro.realm.OperatorRealm.java

License:Apache License

@Override
// ?/*from www.  j a  va2 s . c o m*/
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    String loginName = (String) token.getPrincipal();
    if (StringUtils.isEmpty(loginName.trim())) {
        throw new UnknownAccountException();// ??
    }

    // ????
    PmsOperator operator = pmsOperatorService.findOperatorByLoginName(loginName);

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

    if (PublicStatusEnum.UNACTIVE.equals(operator.getStatus())) {
        throw new LockedAccountException(); // ???
    }

    // AuthenticatingRealmCredentialsMatcher????
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(operator.getLoginName(), // ??
            operator.getLoginPwd(), // ?
            ByteSource.Util.bytes(operator.getCredentialsSalt()), // salt=username+salt
            getName() // realm name
    );

    return authenticationInfo;
}

From source file:com.attendance.manage.security.AuthenticationRealm.java

License:Open Source License

/**
 * ???/*w  w  w .  jav  a  2 s .c o m*/
 * 
 * @param token
 *            
 * @return ??
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken token) {
    System.out.println("in aut");
    UsernamePasswordToken authenticationToken = (UsernamePasswordToken) token;
    String username = authenticationToken.getUsername();
    String password = new String(authenticationToken.getPassword());
    if (username != null && password != null) {
        Stuff admin = stuffServiceImpl.findByUsername(username);
        if (admin == null) {
            throw new UnknownAccountException();
        }

        if (!password.equals(admin.getPassword())) {
            int loginFailureCount = admin.getLoginFailureCount() + 1;
            admin.setLoginFailureCount(loginFailureCount);
            stuffServiceImpl.updateByPrimaryKey(admin);
            throw new IncorrectCredentialsException();
        }
        // md5
        // if (!DigestUtils.md5Hex(password).equals(admin.getPassword())) {
        // int loginFailureCount = admin.getLoginFailureCount() + 1;
        // admin.setLoginFailureCount(loginFailureCount);
        // adminService.updateByPrimaryKey(admin);
        // throw new IncorrectCredentialsException();
        // }
        admin.setLoginDate(new Date());
        admin.setLoginFailureCount(0);
        stuffServiceImpl.updateByPrimaryKey(admin);
        return new SimpleAuthenticationInfo(username, password, getName());

    }
    throw new UnknownAccountException();
}