Search in sources :

Example 71 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project moon by gentoo111.

the class EhCacheTokenManager method getToken.

@Override
public UsernamePasswordToken getToken(String key) {
    Cache cache = cacheManager.getCacheManager().getCache("login_user_tokens");
    Element element = cache.get(key);
    if (element != null) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) element.getValue();
        return usernamePasswordToken;
    }
    return null;
}
Also used : Element(net.sf.ehcache.Element) Cache(net.sf.ehcache.Cache) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Example 72 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project Ganster-CMS by Gangster-trio.

the class LoginController method login.

@RequestMapping("/login")
public com.ganster.cms.auth.dto.Message login(Model model, HttpServletRequest request) {
    com.ganster.cms.auth.dto.Message message = new com.ganster.cms.auth.dto.Message();
    Subject subject = SecurityUtils.getSubject();
    logger.info(subject.getSession().toString());
    String username = request.getParameter("userName");
    String password = request.getParameter("password");
    logger.info("用户" + username + "进行登录");
    UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    // token.setRememberMe(true);
    try {
        subject.login(token);
        subject.getSession().setAttribute("status", true);
        message.setCode(100);
        message.setMsg("ok");
    } catch (Exception e) {
        message.setCode(120);
        message.setMsg("抱歉,信息错误");
        return message;
    }
    return message;
}
Also used : Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 73 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project fruit-manage by liuzhaozhao.

the class LoginController method auth.

/**
 * 登录操作
 */
public void auth() {
    Object uid = getSessionAttr(Constant.SESSION_UID);
    if (uid != null) {
        renderJson(new DataResult<>(DataResult.CODE_SUCCESS, "登录成功"));
    }
    String userName = getPara("username");
    String password = StringUtils.isNotBlank(getPara("password")) ? HashKit.md5(getPara("password")) : getPara("password");
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
    try {
        subject.login(token);
        Session session = subject.getSession();
        session.setAttribute(Constant.SESSION_UID, User.dao.getUser(userName).getId());
        renderNull();
    } catch (Exception e) {
        if (StringUtils.isAllBlank(userName, password)) {
            renderLogin("身份认证失败");
        } else {
            renderErrorText("用户名或密码错误");
        }
    }
}
Also used : Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Session(org.apache.shiro.session.Session)

Example 74 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project cas by apereo.

the class ShiroAuthenticationHandler method authenticateUsernamePasswordInternal.

@Override
protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential transformedCredential, final String originalPassword) throws GeneralSecurityException {
    try {
        val token = new UsernamePasswordToken(transformedCredential.getUsername(), transformedCredential.getPassword());
        if (transformedCredential instanceof RememberMeUsernamePasswordCredential) {
            token.setRememberMe(RememberMeUsernamePasswordCredential.class.cast(transformedCredential).isRememberMe());
        }
        val currentUser = getCurrentExecutingSubject();
        currentUser.login(token);
        checkSubjectRolesAndPermissions(currentUser);
        val strategy = getPasswordPolicyHandlingStrategy();
        val messageList = new ArrayList<MessageDescriptor>();
        if (strategy != null) {
            LOGGER.debug("Attempting to examine and handle password policy via [{}]", strategy.getClass().getSimpleName());
            val principal = this.principalFactory.createPrincipal(token.getUsername());
            messageList.addAll(strategy.handle(principal, getPasswordPolicyConfiguration()));
        }
        return createAuthenticatedSubjectResult(transformedCredential, currentUser, messageList);
    } catch (final UnknownAccountException uae) {
        throw new AccountNotFoundException(uae.getMessage());
    } catch (final LockedAccountException | ExcessiveAttemptsException lae) {
        throw new AccountLockedException(lae.getMessage());
    } catch (final ExpiredCredentialsException eae) {
        throw new CredentialExpiredException(eae.getMessage());
    } catch (final DisabledAccountException eae) {
        throw new AccountDisabledException(eae.getMessage());
    } catch (final AuthenticationException ice) {
        throw new FailedLoginException(ice.getMessage());
    }
}
Also used : lombok.val(lombok.val) DisabledAccountException(org.apache.shiro.authc.DisabledAccountException) AccountLockedException(javax.security.auth.login.AccountLockedException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) ArrayList(java.util.ArrayList) ExcessiveAttemptsException(org.apache.shiro.authc.ExcessiveAttemptsException) ExpiredCredentialsException(org.apache.shiro.authc.ExpiredCredentialsException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) FailedLoginException(javax.security.auth.login.FailedLoginException) AccountNotFoundException(javax.security.auth.login.AccountNotFoundException) CredentialExpiredException(javax.security.auth.login.CredentialExpiredException) RememberMeUsernamePasswordCredential(org.apereo.cas.authentication.credential.RememberMeUsernamePasswordCredential) LockedAccountException(org.apache.shiro.authc.LockedAccountException) AccountDisabledException(org.apereo.cas.authentication.exceptions.AccountDisabledException)

Example 75 with UsernamePasswordToken

use of org.apache.shiro.authc.UsernamePasswordToken in project spring-boot-starter-samples by vindell.

the class AuthzPrincipalRepositoryImpl method getAuthenticationInfo.

@Override
public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
    if (!StringUtils.hasText(upToken.getUsername()) || upToken.getPassword() == null) {
        throw new UnknownAccountException("Username or password is required.");
    }
    // 密码加密
    // Base64.encodeBase64String(new String(upToken.getPassword()).getBytes());
    String pwd = new String(upToken.getPassword());
    // 账号状态
    Map<String, String> statusMap = getAuthzLoginDao().getAccountStatus(upToken.getUsername(), pwd);
    // 账号不存在 或 用户名或密码不正确
    if ("0".equals(statusMap.get("num_1")) || "0".equals(statusMap.get("num_2"))) {
        throw new InvalidAccountException("Username or password is incorrect, please re-enter.");
    } else // 账号被禁用
    if ("0".equals(statusMap.get("num_4"))) {
        throw new DisabledAccountException("Account is disabled.");
    } else // 用户无所属角色
    if ("0".equals(statusMap.get("num_3"))) {
        throw new NoneRoleException();
    }
    // 用户主体对象
    AuthzLoginModel model = getAuthzLoginDao().getAccount(upToken.getUsername(), pwd);
    // 用户角色ID集合
    List<String> roles = getAuthzUserDao().getRoles(model.getUserid());
    model.setRoles(Sets.newHashSet(roles.iterator()));
    model.setRoleid(roles.get(0));
    // 用户权限标记集合
    Set<String> perms = Sets.newHashSet();
    for (String roleid : model.getRoles()) {
        perms.addAll(getAuthzRolePermsDao().getPermissions(roleid));
    }
    model.setPerms(perms);
    // 认证信息
    return new SimpleAuthenticationInfo(model, upToken.getPassword(), "login");
}
Also used : DisabledAccountException(org.apache.shiro.authc.DisabledAccountException) AuthzLoginModel(net.jeebiz.boot.demo.dao.entities.AuthzLoginModel) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) InvalidAccountException(org.apache.shiro.biz.authc.exception.InvalidAccountException) NoneRoleException(org.apache.shiro.biz.authc.exception.NoneRoleException)

Aggregations

UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)118 Subject (org.apache.shiro.subject.Subject)52 Test (org.junit.Test)30 AuthenticationException (org.apache.shiro.authc.AuthenticationException)28 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)28 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)19 AuthenticationInfo (org.apache.shiro.authc.AuthenticationInfo)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Test (org.testng.annotations.Test)11 LockedAccountException (org.apache.shiro.authc.LockedAccountException)10 IncorrectCredentialsException (org.apache.shiro.authc.IncorrectCredentialsException)9 UnknownAccountException (org.apache.shiro.authc.UnknownAccountException)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 DelegatingSubject (org.apache.shiro.subject.support.DelegatingSubject)7 Session (org.apache.shiro.session.Session)6 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)6 DisabledAccountException (org.apache.shiro.authc.DisabledAccountException)4 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)4