當前位置: 首頁>>代碼示例>>Java>>正文


Java AuthenticationToken.getPrincipal方法代碼示例

本文整理匯總了Java中org.apache.shiro.authc.AuthenticationToken.getPrincipal方法的典型用法代碼示例。如果您正苦於以下問題:Java AuthenticationToken.getPrincipal方法的具體用法?Java AuthenticationToken.getPrincipal怎麽用?Java AuthenticationToken.getPrincipal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.shiro.authc.AuthenticationToken的用法示例。


在下文中一共展示了AuthenticationToken.getPrincipal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: queryForAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
/**
 * This implementation opens an LDAP connection using the token's
 * {@link #getLdapPrincipal(org.apache.shiro.authc.AuthenticationToken) discovered principal} and provided
 * {@link AuthenticationToken#getCredentials() credentials}.  If the connection opens successfully, the
 * authentication attempt is immediately considered successful and a new
 * {@link AuthenticationInfo} instance is
 * {@link #createAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken, Object, Object, javax.naming.ldap.LdapContext) created}
 * and returned.  If the connection cannot be opened, either because LDAP authentication failed or some other
 * JNDI problem, an {@link NamingException} will be thrown.
 *
 * @param token              the submitted authentication token that triggered the authentication attempt.
 * @param ldapContextFactory factory used to retrieve LDAP connections.
 * @return an {@link AuthenticationInfo} instance representing the authenticated user's information.
 * @throws NamingException if any LDAP errors occur.
 */
protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token,
                                                        LdapContextFactory ldapContextFactory)
        throws NamingException {

    Object principal = token.getPrincipal();
    Object credentials = token.getCredentials();

    log.debug("Authenticating user '{}' through LDAP", principal);

    principal = getLdapPrincipal(token);

    LdapContext ctx = null;
    try {
        ctx = ldapContextFactory.getLdapContext(principal, credentials);
        //context was opened successfully, which means their credentials were valid.  Return the AuthenticationInfo:
        return createAuthenticationInfo(token, principal, credentials, ctx);
    } finally {
        LdapUtils.closeContext(ctx);
    }
}
 
開發者ID:xuegongzi,項目名稱:rabbitframework,代碼行數:36,代碼來源:DefaultLdapRealm.java

示例2: doCredentialsMatch

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws ExcessiveAttemptsException {
    String username = (String)token.getPrincipal();
    AtomicInteger retryCount = passwordRetryCache.get(username);

    if(retryCount == null) {
        retryCount = new AtomicInteger(0);
        passwordRetryCache.put(username, retryCount);
    }
    if(retryCount.incrementAndGet() > retryMax) {
        throw new ExcessiveAttemptsException("您已連續錯誤達" + retryMax + "次!請10分鍾後再試");
    }

    boolean matches = super.doCredentialsMatch(token, info);
    if(matches) {
        passwordRetryCache.remove(username);
    }else {
        throw new IncorrectCredentialsException("密碼錯誤,已錯誤" + retryCount.get() + "次,最多錯誤" + retryMax + "次");
    }
    return true;
}
 
開發者ID:johntostring,項目名稱:spring-boot-shiro,代碼行數:22,代碼來源:RetryLimitHashedCredentialsMatcher.java

示例3: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
/**
 * 用戶認證-驗證用戶是否登錄、用戶名密碼是否匹配
 */
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	logger.info(">>> 【用戶認證】token = {}", token);
	String userName = (String)token.getPrincipal();
	AdminUser user = getPrincipalService().getPrincipalObject(userName);
       if(user == null) {
           throw new UnknownAccountException("Unknown account: " + userName);//沒找到帳號
       }
       if(AdminUserStatusEnum.ADMIN_USER_STATUS_DISABLED.getStatusCode().equals(user.getStatus())) {
           throw new LockedAccountException("Account[" + userName + "] has been locked!"); //帳號鎖定
       }
       //交給AuthenticatingRealm使用CredentialsMatcher進行密碼匹配
       SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
               user.getUserName(), //用戶名
               user.getPassword(), //密碼
               ByteSource.Util.bytes(user.getPasswordSalt()),//salt
               getName()  //realm name
       );
       return authenticationInfo;
}
 
開發者ID:penggle,項目名稱:xproject,代碼行數:23,代碼來源:AdminUserRealm.java

示例4: doCredentialsMatch

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
	String userName = (String)token.getPrincipal();
	final String key = REDIS_KEY_PREFIX + userName;
	long maxRetry = redisTemplate.opsForValue().increment(key, 1);
	if(maxRetry == 1){ //首次輸入密碼
		redisTemplate.expire(key, passwordRetryWaitMinutes, TimeUnit.MINUTES);
	}
	if(maxRetry >= passwordRetryLimit){
		throw new ExcessiveAttemptsException(passwordRetryLimit + "");
	}
	boolean matches = super.doCredentialsMatch(token, info);
       if(matches) {
       	redisTemplate.delete(key);
       }
       return matches;
}
 
開發者ID:penggle,項目名稱:xproject,代碼行數:17,代碼來源:RetryLimitHashedCredentialsMatcher.java

示例5: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // token是用戶輸入的用戶名和密碼
        // 第一步從token中取出用戶名
        String userCode = (String) token.getPrincipal();

        // 如果查詢不到返回null
        //數據庫中用戶賬號是zhangsansan
//        if(!userCode.equals("zhangsansan")){//
//            return null;
//        }

        // 模擬從數據庫查詢到密碼
        String password = "111111";

        //將activeUser設置simpleAuthenticationInfo
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                userCode, password, this.getName());

        return simpleAuthenticationInfo;
    }
 
開發者ID:lgpzjp,項目名稱:rure,代碼行數:22,代碼來源:CustomRealm.java

示例6: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	String username = (String) token.getPrincipal();// 根據剛剛傳過來的token獲取用戶名
	Blogger blogger = bloggerService.findByUsername(username);// 隻是根據用戶名查詢出,不涉及密碼
	if (blogger != null) {
		System.out.println("驗證信息:" + blogger);
		// 把獲取到的用戶存到session中
		SecurityUtils.getSubject().getSession().setAttribute("blogger", blogger);
		// 把從數據庫中查詢出來的博主信息放到AuthenticationInfo中,即把正確的用戶名,密碼,交給shiro,再和前台輸入的校驗。
		AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(blogger.getUsername(),
				blogger.getPassword(), "MyRealm");
		return authenticationInfo;
	} else {
		return null;
	}

}
 
開發者ID:shinyjunjun,項目名稱:myblog,代碼行數:18,代碼來源:MyRealm.java

示例7: doCredentialsMatch

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info){
    String username = (String) token.getPrincipal();

    Element element = passwordRetryCache.get(username);
    if(element == null){
        element = new Element(username, new AtomicInteger(0));
        passwordRetryCache.put(element);
    }

    AtomicInteger retryCount = (AtomicInteger) element.getObjectValue();

    if(retryCount.incrementAndGet() > 5){
        throw new ExcessiveAttemptsException();
    }

    boolean matches = super.doCredentialsMatch(token, info);
    if(matches){
        passwordRetryCache.remove(username);
    }
    return matches;
}
 
開發者ID:l81893521,項目名稱:shiro-demo,代碼行數:22,代碼來源:RetryLimitHashedCredentialsMatcher.java

示例8: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	String phoneNumber = (String)token.getPrincipal();
       if(StringUtils.trimToNull(phoneNumber) == null){
           throw new IncorrectCredentialsException();//賬號或密碼錯誤
       }
	CdMember query = new CdMember();
	query.setPhoneNumber(phoneNumber);
       CdMember member = memberService.findMember(query);
       if(member == null) {
           throw new UnknownAccountException();//沒找到帳號
       }
       SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
               phoneNumber, //用戶名
               member.getPassword(), //密碼
               ByteSource.Util.bytes(AppConstants.PC_PASSWORD_SALT),//salt=phoneNumber
               getName()  //realm name
       );
       return authenticationInfo;
}
 
開發者ID:xmomen,項目名稱:dms-webapp,代碼行數:21,代碼來源:MemberRealm.java

示例9: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

    String username = (String)token.getPrincipal();

    SysUsers user = userService.findByUsername(username);

    if(user == null) {
        throw new UnknownAccountException();//沒找到帳號
    }

    if(Boolean.TRUE.equals(user.getLocked())) {
        throw new LockedAccountException(); //帳號鎖定
    }

    //交給AuthenticatingRealm使用CredentialsMatcher進行密碼匹配,如果覺得人家的不好可以自定義實現
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
            username, //用戶名
            user.getPassword(), //密碼
            ByteSource.Util.bytes(user.getSalt()),//salt=salt
            getName()  //realm name
    );
    return authenticationInfo;
}
 
開發者ID:xmomen,項目名稱:dms-webapp,代碼行數:25,代碼來源:UserRealm.java

示例10: doCredentialsMatch

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
public boolean doCredentialsMatch(AuthenticationToken token,
		AuthenticationInfo info) {
	String username = (String) token.getPrincipal();
	// retry count + 1
	if (passwordRetryCache != null) {
		AtomicInteger retryCount = passwordRetryCache.get(username);
		if (retryCount == null) {
			retryCount = new AtomicInteger(0);
			passwordRetryCache.put(username, retryCount);
		}
		if (retryCount.incrementAndGet() > 5) {
			// if retry count > 5 throw
			throw new ExcessiveAttemptsException();
		}
	}
	boolean matches = super.doCredentialsMatch(token, info);
	if (matches && passwordRetryCache != null) {
		// clear retry count
		passwordRetryCache.remove(username);
	}
	return matches;
}
 
開發者ID:inexistence,項目名稱:VideoMeeting,代碼行數:24,代碼來源:RetryLimitHashedCredentialsMatcher.java

示例11: doCredentialsMatch

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
    String username = (String) token.getPrincipal();
    //retry count + 1
    AtomicInteger retryCount = (AtomicInteger) SilentGo.me().getConfig().getCacheManager().get("passwordRetryCache", username);
    if (retryCount == null) {
        retryCount = new AtomicInteger(0);
        SilentGo.me().getConfig().getCacheManager().set("passwordRetryCache", username, retryCount);
    }
    if (retryCount.incrementAndGet() > 5) {
        //if retry count > 5 throw
        throw new ExcessiveAttemptsException();
    }

    boolean matches = super.doCredentialsMatch(token, info);
    if (matches) {
        //clear retry count
        SilentGo.me().getConfig().getCacheManager().evict("passwordRetryCache", username);
    }
    return matches;
}
 
開發者ID:Teddy-Zhu,項目名稱:SilentGo,代碼行數:22,代碼來源:RetryLimitHashedCredentialsMatcher.java

示例12: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	log.debug("username[{}]doGetAuthenticationInfo", token.getPrincipal());
    String username = (String)token.getPrincipal();
    ShiroUser user = memberService.findUserModelByAccNo(username);
    if(user == null) {
        throw new UnknownAccountException();
    }
    //交給AuthenticatingRealm使用CredentialsMatcher進行密碼匹配
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
            user.getAccNo(),
            user.getPassword(),
            ByteSource.Util.bytes(""),//加鹽
            getName()  //realm name
    );
    return authenticationInfo;
}
 
開發者ID:ls960972314,項目名稱:report,代碼行數:18,代碼來源:UserRealm.java

示例13: assertCredentialsMatch

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
protected void assertCredentialsMatch(AuthenticationToken token,
		AuthenticationInfo info) throws AuthenticationException {
	// 如果驗證出錯,super會拋出異常
	super.assertCredentialsMatch(token, info);
	// 驗證通過,走下麵,刪除舊的subject,不刪好像也沒事
	// 刪除其他設備上的這個用戶的session
	// 人多了效率有點危險
	String username = (String) token.getPrincipal();
	if (token == null || username == null)
		return;
	if (SecurityUtils.getSubject() != null) {
		SecurityUtils.getSubject().logout();
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		for (Session session : sessions) {
			if (username.equals(session.getAttribute("username"))) {
				session.stop();
			}
		}
	}
}
 
開發者ID:inexistence,項目名稱:VideoMeeting,代碼行數:22,代碼來源:ShiroService.java

示例14: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.err.println("MyShiroRealm.doGetAuthenticationInfo()");
       String username = (String) token.getPrincipal();
        System.err.println(username);

        User user = userService.findUserByUsername(username);

        if (user==null){
            return null;
        }

        System.err.println(salt);

        //1:
//        SimpleAuthenticationInfo authenticationInfo =
//                new SimpleAuthenticationInfo(
//                        user, //用戶對象
//                        user.getPassword(), //密碼
//                        ByteSource.Util.bytes(username+salt),//salt=username+salt
//                        getName()  //realm name
//                );

        //2:或:
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                username, //用戶名
                user.getPassword(), //密碼""
                getName()  //realm name
        );

        return authenticationInfo;
    }
 
開發者ID:mmdsyl,項目名稱:BLOG-Microservice,代碼行數:33,代碼來源:MyShiroRealm.java

示例15: doGetAuthenticationInfo

import org.apache.shiro.authc.AuthenticationToken; //導入方法依賴的package包/類
/**
 * 登錄驗證
 * 
 * @param token
 *            用戶登錄時的賬號密碼組成的token
 * @return
 * @throws AuthenticationException
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
	Object principal = token.getPrincipal();
	String accountNumber = token.getPrincipal().toString();
	String credentials = userService.getUserByAccountNumber(accountNumber).getPassword();// 根據登錄accountNumber去數據庫中查找密碼
	String realmName = getName();
	String source = SysConst.SALTSOURCE;
	ByteSource credentialsSalt = new Md5Hash(source);
	// 密碼比對過程由shiro自己完成
	SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt,
			realmName);
	return info;
}
 
開發者ID:MarchMachao,項目名稱:ZHFS-WEB,代碼行數:22,代碼來源:MyRealm.java


注:本文中的org.apache.shiro.authc.AuthenticationToken.getPrincipal方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。