本文整理汇总了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;
}