use of org.apache.shiro.authc.UsernamePasswordToken in project production_ssm by megagao.
the class LoginController method ajaxLogin.
/**
* shiro ajax登录
*/
@RequestMapping(value = "/ajaxLogin")
@ResponseBody
public Map<String, Object> ajaxLogin(@RequestParam String username, @RequestParam String password, @RequestParam(required = false) String randomcode, HttpSession session) throws Exception {
Map<String, Object> map = CollectionsFactory.newHashMap();
if (randomcode != null && !randomcode.equals("")) {
// 取出session的验证码(正确的验证码)
String validateCode = (String) session.getAttribute(VALIDATE_CODE);
// 页面中输入的验证和session中的验证进行对比
if (validateCode != null && !randomcode.equals(validateCode)) {
// 如果校验失败,将验证码错误失败信息放入map中
map.put("msg", "randomcode_error");
// 直接返回,不再校验账号和密码
return map;
}
}
Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
currentUser.login(token);
} catch (UnknownAccountException ex) {
map.put("msg", "account_error");
} catch (IncorrectCredentialsException ex) {
map.put("msg", "password_error");
} catch (AuthenticationException ex) {
map.put("msg", "authentication_error");
}
}
// 返回json数据
return map;
}
use of org.apache.shiro.authc.UsernamePasswordToken in project ART-TIME by Artezio.
the class LoginBean method login.
public String login() {
if (loggedEmployee == null) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
SecurityUtils.getSubject().login(token);
}
return calculateOutcom();
}
use of org.apache.shiro.authc.UsernamePasswordToken in project ART-TIME by Artezio.
the class LoginBeanTest method testLogin.
@Test
public void testLogin() throws Exception {
Subject subject = createMock(Subject.class);
setField(loginBean, "username", "admin");
setField(loginBean, "password", "qwerty");
setField(loginBean, "rememberMe", true);
setField(loginBean, "loggedEmployee", null);
setField(loginBean, "externalContext", externalContext);
UsernamePasswordToken token = PowerMock.createMock(UsernamePasswordToken.class);
PowerMock.expectNew(UsernamePasswordToken.class, "admin", "qwerty", true).andReturn(token);
PowerMock.mockStatic(SecurityUtils.class);
expect(SecurityUtils.getSubject()).andReturn(subject);
expect(externalContext.isUserInRole(anyString())).andReturn(true).anyTimes();
subject.login(token);
PowerMock.replayAll(externalContext);
loginBean.login();
PowerMock.verifyAll();
}
use of org.apache.shiro.authc.UsernamePasswordToken in project ART-TIME by Artezio.
the class AdminRealmTest method testdoGetAuthenticationInfo.
@Test
public void testdoGetAuthenticationInfo() {
Settings settings = new Settings(new EnumMap<>(Setting.Name.class));
settings.setAdminUsername("admin");
AuthenticationToken token = new UsernamePasswordToken("admin", new char[] {});
PowerMock.mockStatic(CDIUtils.class);
expect(CDIUtils.getBean(SettingsService.class)).andReturn(settingsService);
expect(settingsService.getSettings()).andReturn(settings);
PowerMock.replayAll(CDIUtils.class, settingsService);
AuthenticationInfo actual = adminRealm.doGetAuthenticationInfo(token);
PowerMock.verifyAll();
assertNotNull(actual);
assertTrue(actual instanceof SimpleAccount);
}
use of org.apache.shiro.authc.UsernamePasswordToken in project tesla by linking12.
the class TeslaUserRealm method doGetAuthenticationInfo.
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
String username = upToken.getUsername();
if (username == null) {
throw new AccountException("Null usernames are not allowed by this realm.");
}
Users user = userDao.findByUserNamed(username);
Long userId = user.userId();
String password = user.password();
int status = user.status();
if (password == null) {
throw new UnknownAccountException("No account found for " + username);
}
if (!password.equals(new String((char[]) token.getCredentials()))) {
throw new IncorrectCredentialsException("Password is not right for " + username);
}
if (status == 0) {
throw new LockedAccountException("account is locked for user " + username);
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userId, password.toCharArray(), username);
info.setCredentialsSalt(ByteSource.Util.bytes(username));
return info;
}
Aggregations