本文整理匯總了Java中org.apache.shiro.authc.UsernamePasswordToken.setRememberMe方法的典型用法代碼示例。如果您正苦於以下問題:Java UsernamePasswordToken.setRememberMe方法的具體用法?Java UsernamePasswordToken.setRememberMe怎麽用?Java UsernamePasswordToken.setRememberMe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.shiro.authc.UsernamePasswordToken
的用法示例。
在下文中一共展示了UsernamePasswordToken.setRememberMe方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: signin
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@RequestMapping(value = "/signin", method = {
RequestMethod.POST})
public String signin(ModelMap map, User user, HttpServletRequest request) {
String error;
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPasswd());
token.setRememberMe(null != request.getParameter("rememberme") ? true : false);
try {
Subject subject = SecurityUtils.getSubject();
subject.login(token);
subject.getSession().setAttribute("curUser", userService.findByUsername((String) subject.getPrincipal()));
return "redirect:/dashboard/console";
} catch (UnknownAccountException uae) {
error = "用戶名錯誤!";
} catch (IncorrectCredentialsException ice) {
error = "密碼錯誤!";
} catch (LockedAccountException lae) {
error = "用戶被鎖定!";
}
map.addAttribute("error", error);
return "signin";
}
示例2: tryLogin
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
public boolean tryLogin(String email, String password, Boolean rememberMe) {
org.apache.shiro.subject.Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(email, password);
token.setRememberMe(rememberMe);
try {
currentUser.login(token);
System.out.println("User [" + currentUser.getPrincipal().toString() + "] logged in successfully.");
// save username in the session
currentUser.getSession().setAttribute("username", email);
return true;
} catch (UnknownAccountException uae) {
System.out.println("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
System.out.println("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
System.out.println("The account for username " + token.getPrincipal() + " is locked. " + "Please contact your administrator to unlock it.");
}
return false;
}
示例3: dashboard
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@RequestMapping(value = "/login", method = {
RequestMethod.POST})
public String dashboard(ModelMap map, Admin admin) {
String error = null;
UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(), admin.getPassword());
token.setRememberMe(false);
try {
SecurityUtils.getSubject().login(token);
return "redirect:/video/all";
} catch (UnknownAccountException uae) {
error = "用戶名錯誤!";
} catch (IncorrectCredentialsException ice) {
error = "密碼錯誤!";
} catch (LockedAccountException lae) {
error = "用戶被鎖定!";
}
map.addAttribute("error", error);
return "login.ftl";
}
示例4: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@RequestMapping(value = "login", method = RequestMethod.POST)
public String login(@ModelAttribute("formDto") LoginDto formDto, BindingResult errors) {
UsernamePasswordToken token = formDto.token();
token.setRememberMe(false);
try {
SecurityUtils.getSubject().login(token);
} catch (Exception e) {
LOG.debug("Error authenticating.", e);
errors.rejectValue("username", null, "The username or password was not correct.");
return "login";
}
return "redirect:index";
}
示例5: signup
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@RequestMapping(value = "/signup", method = {
RequestMethod.POST})
public String signup(User user, ModelMap map) {
//檢查用戶名是否被注冊
User temp = userService.findByUsername(user.getUsername());
if (null == temp) {
//注冊
String passwdTemp = user.getPasswd();
passwordHelper.encryptPassword(user);
userService.insert(user);
//登錄到shiro中
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), passwdTemp);
token.setRememberMe(true);
Subject subject = SecurityUtils.getSubject();
subject.login(token);
subject.getSession().setAttribute("curUser", user);
return "redirect:/dashboard/console";
} else {
map.addAttribute("error", "用戶名已經被占用!");
return "signup";
}
}
示例6: changepwd
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@RequestMapping(value = "/changepwd", method = {
RequestMethod.POST})
public String changepwd(ModelMap map, User user, @RequestParam(value = "passwdnew", required = true) String passwdnew) {
//驗證當前賬號
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPasswd());
token.setRememberMe(false);
try {
SecurityUtils.getSubject().login(token);
//驗證通過更新用戶密碼
user.setId(getCurrentUser().getId());
user.setPasswd(passwdnew);
passwordHelper.encryptPassword(user);
userService.updateById(user);
return "redirect:/dashboard/console";
} catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException e) {
map.addAttribute("exception", e.getMessage());
return "common/error";
}
}
示例7: execute
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@Override
public String execute(Database database) {
Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
token.setRememberMe(true);
try {
currentUser.login(token);
return "user: "+username + " logged in successfully";
} catch (Exception e) {
throw new VectorizationException("Login failed");
}
}else{
currentUser.logout();
return execute(database);
}
}
示例8: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@Override
public boolean login(LoginVo loginVo) {
Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(
loginVo.getUsername(), SecurityUtil.encodeMd5(loginVo
.getPassword()));
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
return false;
} catch (IncorrectCredentialsException ice) {
return false;
} catch (LockedAccountException lae) {
return false;
} catch (AuthenticationException ae) {
return false;
}
return true;
}
示例9: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@Override
public Map<String, Object> login(String userName, String password) {
Map<String, Object> loginInfo = new HashMap<String, Object>();
Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(userName,
password);
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
return null;
} catch (IncorrectCredentialsException ice) {
return null;
} catch (LockedAccountException lae) {
return null;
} catch (AuthenticationException ae) {
return null;
}
loginInfo.put(WizardWebUtils.USER_NAME, userName);
loginInfo.put(WizardWebUtils.ROLE_NAME, getRole(currentUser));
return loginInfo;
}
示例10: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@Override
public Map<String, Object> login(String userName, String password) {
Map<String, Object> loginInfo = new HashMap<String, Object>();
Subject currentUser = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(userName,
password);
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
return null;
} catch (IncorrectCredentialsException ice) {
return null;
} catch (LockedAccountException lae) {
return null;
} catch (AuthenticationException ae) {
return null;
}
loginInfo.put(WizardWebUtils.USER_NAME, userName);
loginInfo.put(WizardWebUtils.ROLE_NAME, getRole(currentUser));
return loginInfo;
}
示例11: tryLogin
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
public boolean tryLogin(String username, String password, Boolean rememberMe) {
org.apache.shiro.subject.Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
token.setRememberMe(rememberMe);
try {
currentUser.login(token);
LOGGER.info("User [" + currentUser.getPrincipal().toString() + "] logged in successfully.");
// save username in the session
currentUser.getSession().setAttribute("username", username);
return true;
} catch (UnknownAccountException uae) {
LOGGER.info("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
LOGGER.info("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
LOGGER.info("The account for username " + token.getPrincipal() + " is locked. "
+ "Please contact your administrator to unlock it.");
}
} else {
return true;
}
return false;
}
示例12: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@Transactional
@Override
public User login(String username, String password) throws IncorrectCredentialsException{
UsernamePasswordToken token = new UsernamePasswordToken();
token.setUsername(username);
token.setPassword(password.toCharArray());
token.setRememberMe(true);
SecurityUtils.getSubject().login(token);
if (SecurityUtils.getSubject().isAuthenticated()) {
SecurityUtils.getSubject().getSession().setAttribute("username", username);
return userDao.get("from User u where u.username = ?",
new String[] { username });
}
return null;
}
示例13: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
/**
* 安全登陸方法
*
* @param loginName
* 用戶名稱
* @param loginPwd
* 用戶密碼
* @param isRemeberMe
* 是否記住我
* @return
*/
public static boolean login(String loginName, String loginPwd, boolean isRemeberMe) {
UsernamePasswordToken token = new UsernamePasswordToken(loginName, loginPwd);
// 記錄該令牌,如果不記錄則類似購物車功能不能使用。
token.setRememberMe(isRemeberMe);
// subject理解成權限對象。類似user
Subject subject = SecurityUtils.getSubject();
// 如果當前已登陸,先退出登陸
SecurityUser securityUser = getSecurityUser();
if (securityUser != null) {
logout();
}
try {
subject.login(token);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
}
// } catch (UnknownAccountException ex) {// 用戶名沒有找到
// logger.error(ex.getMessage(), ex);
// } catch (IncorrectCredentialsException ex) {// 用戶名密碼不匹配
// logger.error(ex.getMessage(), ex);
// } catch (AuthenticationException e) {// 其他的登錄錯誤
// logger.error(e.getMessage(), e);
// }
if (subject.isAuthenticated()) {
return true;
}
return false;
}
示例14: authenticateUser
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
private void authenticateUser(Subject currentUser, ShiroSecurityToken securityToken) {
boolean authenticated = currentUser.isAuthenticated();
boolean sameUser = securityToken.getUsername().equals(currentUser.getPrincipal());
LOG.trace("Authenticated: {}, same Username: {}", authenticated, sameUser);
if (!authenticated || !sameUser) {
UsernamePasswordToken token = new UsernamePasswordToken(securityToken.getUsername(), securityToken.getPassword());
if (policy.isAlwaysReauthenticate()) {
token.setRememberMe(false);
} else {
token.setRememberMe(true);
}
try {
currentUser.login(token);
LOG.debug("Current user {} successfully authenticated", currentUser.getPrincipal());
} catch (UnknownAccountException uae) {
throw new UnknownAccountException("Authentication Failed. There is no user with username of " + token.getPrincipal(), uae.getCause());
} catch (IncorrectCredentialsException ice) {
throw new IncorrectCredentialsException("Authentication Failed. Password for account " + token.getPrincipal() + " was incorrect!", ice.getCause());
} catch (LockedAccountException lae) {
throw new LockedAccountException("Authentication Failed. The account for username " + token.getPrincipal() + " is locked."
+ "Please contact your administrator to unlock it.", lae.getCause());
} catch (AuthenticationException ae) {
throw new AuthenticationException("Authentication Failed.", ae.getCause());
}
}
}
示例15: testUser1
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@Test
public void testUser1() throws Exception {
/*
* login the current subject
*/
Subject subject = SecurityUtils.getSubject();
// use a username/pass token
UsernamePasswordToken token = new UsernamePasswordToken("root", "123456");
token.setRememberMe(true);
subject.login(token);
log.info("User successfuly logged in");
Thread.sleep(2 * 1000);
log.info("hasRole:" + subject.hasRole("admin"));
Thread.sleep(2 * 1000);
log.info("isPermitted:" + subject.isPermitted("*"));
log.info("session:" + subject.getSession().getTimeout());
log.info("session:" + subject.getSession().getLastAccessTime());
DefaultSessionManager sessionManager = (DefaultSessionManager) ((DefaultSecurityManager) SecurityUtils.getSecurityManager()).getSessionManager();
sessionManager.getSessionDAO().getActiveSessions();
Thread.sleep(30 * 1000);
}