本文整理匯總了Java中org.apache.shiro.util.ByteSource類的典型用法代碼示例。如果您正苦於以下問題:Java ByteSource類的具體用法?Java ByteSource怎麽用?Java ByteSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ByteSource類屬於org.apache.shiro.util包,在下文中一共展示了ByteSource類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
/**
* 認證回調函數,登錄時調用.
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException {
UsernamePassword2Token token = (UsernamePassword2Token) authcToken;
String username = token.getUsername();
if (username == null || null == username) {
throw new AccountException(
"Null usernames are not allowed by this realm.");
}
User entity = new User();
entity.setEmail(username);
entity.setStatus(Constant.STATUS_ENABLED);
entity = (User) service.iUserService.select(entity);
if (null == entity) {
throw new UnknownAccountException("No account found for user ["
+ username + "]");
}
byte[] key = Encode.decodeHex(entity.getRandom());
return new SimpleAuthenticationInfo(new Shiro(entity.getId(),
entity.getEmail(), entity.getName()), entity.getPassword(),
ByteSource.Util.bytes(key), getName());
}
示例2: genPassword
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
private String genPassword(final String psw, final String salt, final int iter) {
try {
final DefaultHashService hash = new DefaultHashService();
hash.setPrivateSalt(ByteSource.Util.bytes(STATIC_SALT));
hash.setHashIterations(iter);
hash.setGeneratePublicSalt(false);
hash.setHashAlgorithmName(ALG_NAME);
final String pswEnc = hash.computeHash(new HashRequest.Builder()
.setSource(psw).setSalt(salt).setIterations(iter).build()).toHex();
return pswEnc;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:19,代碼來源:QueryAndEncodeDatabaseAuthenticationHandlerTests.java
示例3: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
/**
* 認證
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//獲取用戶的輸入的賬號.
String username = (String)token.getPrincipal();
AdminUser user = adminUserService.findByUserName(username);
if(user==null) throw new UnknownAccountException();
if (0==user.getEnable()) {
throw new LockedAccountException(); // 帳號鎖定
}
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
user.getId(), //用戶
user.getPassword(), //密碼
ByteSource.Util.bytes(username),
getName() //realm name
);
// 把用戶信息放在session裏
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("AdminSession", user);
session.setAttribute("AdminSessionId", user.getId());
return authenticationInfo;
}
示例4: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
/**
* 對用戶基本信息進行判定
*
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
Admin admin = adminService.findByUsername(username);
if (admin == null) {
throw new UnknownAccountException();//沒找到帳號
}
//交給AuthenticatingRealm使用CredentialsMatcher進行密碼匹配
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
admin.getUsername(), //用戶名
admin.getPassword(),
ByteSource.Util.bytes(admin.getCredentialsSalt()),
getName() //realm name
);
return authenticationInfo;
}
示例5: passwordsMatch
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
public boolean passwordsMatch(Object submittedPlaintext, String encrypted) {
ByteSource plaintextBytes = ByteSource.Util.bytes(submittedPlaintext);
if (encrypted == null || encrypted.length() == 0) {
return plaintextBytes == null || plaintextBytes.isEmpty();
} else {
if (plaintextBytes == null || plaintextBytes.isEmpty()) {
return false;
}
}
String plaintext = new String(plaintextBytes.getBytes());
String[] tokens = encrypted.split("\\$");
int iterations = Integer.parseInt(tokens[1]);
byte[] salt = tokens[2].getBytes();
String hash = tokens[3];
KeySpec spec = new PBEKeySpec(plaintext.toCharArray(), salt, iterations, 256);
try {
String algorithmName = getAlgorithmFullName(tokens[0]);
SecretKeyFactory f = SecretKeyFactory.getInstance(algorithmName);
return hash.equals(Base64.encodeToString(f.generateSecret(spec).getEncoded()));
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
}
示例6: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的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;
}
示例7: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String)token.getPrincipal();
User user = userService.findUserByLoginName(username);
if(user == null) {
throw new UnknownAccountException();//沒找到帳號
}
UsernamePasswordToken hostToken = (UsernamePasswordToken)token;
//交給AuthenticatingRealm使用CredentialsMatcher進行密碼匹配,如果覺得不好可以自定義實現
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
new ShiroUser(user.getId(),user.getLoginName(),user.getNickname()), //用戶名
user.getPassword(), //密碼
ByteSource.Util.bytes(user.getCredentialsSalt()),//salt=username+salt
getName() //realm name
);
return authenticationInfo;
}
示例8: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//獲取用戶的輸入的賬號.
String username = (String)token.getPrincipal();
User user = userService.findByUserId(username);
if(user==null) throw new UnknownAccountException();
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
user, //用戶
user.getPassword(), //密碼
ByteSource.Util.bytes(username),
getName() //realm name
);
// 當驗證都通過後,把用戶信息放在session裏
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("userSession", user);
session.setAttribute("userSessionId", user.getLoginId());
return authenticationInfo;
}
示例9: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
/**
* 對用戶基本信息進行判定
*
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
User user = userService.findByUsername(username);
if (user == null) {
throw new UnknownAccountException();//沒找到帳號
}
//交給AuthenticatingRealm使用CredentialsMatcher進行密碼匹配
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
user.getUsername(), //用戶名
user.getPasswd(),
ByteSource.Util.bytes(user.getCredentialsSalt()),
getName() //realm name
);
return authenticationInfo;
}
示例10: testHashService
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
@Test
public void testHashService(){
//默認也是好似用sha512
DefaultHashService hashService = new DefaultHashService();
hashService.setHashAlgorithmName("SHA-512");
//私鹽
hashService.setPrivateSalt(new SimpleByteSource("123"));
//是否生成公鹽
hashService.setGeneratePublicSalt(true);
//用於生成公鹽,默認就這個
hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());
//生成hash值的迭代次數
hashService.setHashIterations(1);
HashRequest request = new HashRequest.Builder().setAlgorithmName("MD5")
.setSource(ByteSource.Util.bytes("hello"))
.setSalt(ByteSource.Util.bytes("123"))
.setIterations(2)
.build();
String hex = hashService.computeHash(request).toHex();
System.out.println(hex);
}
示例11: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = "liu";
//隨機數
String salt2 = "0072273a5d87322163795118fdd7c45e";
//加密後的密碼
String password = "be320beca57748ab9632c4121ccac0db";
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName());
authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(username + salt2));
return authenticationInfo;
}
示例12: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
/**
* 主要用於驗證
* @param token
* @return
* @throws AuthenticationException
*/
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String)token.getPrincipal();
User 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(
user.getUsername(), //用戶名
user.getPassword(), //密碼
ByteSource.Util.bytes(user.getCredentialsSalt()),//salt=username+salt
getName() //realm name
);
return authenticationInfo;
}
示例13: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的package包/類
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
/* if (Strings.isBlank(upToken.getCaptcha()))
throw new AuthenticationException("驗證碼不能為空");
String _captcha = Strings.sBlank(SecurityUtils.getSubject().getSession(true).getAttribute(Toolkit.captcha_attr));
if (!upToken.getCaptcha().equalsIgnoreCase(_captcha))
throw new AuthenticationException("驗證碼錯誤");*/
User user = dao().fetch(User.class, Cnd.where("name", "=", upToken.getUsername()));
if (user == null)
return null;
if (user.isLocked())
throw new LockedAccountException("Account [" + upToken.getUsername() + "] is locked.");
ByteSource salt = ByteSource.Util.bytes(user.getSalt());
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
info.setCredentialsSalt(salt);
return info;
}
示例14: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的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;
}
示例15: doGetAuthenticationInfo
import org.apache.shiro.util.ByteSource; //導入依賴的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;
}