本文整理匯總了Java中org.apache.shiro.authc.UsernamePasswordToken.clear方法的典型用法代碼示例。如果您正苦於以下問題:Java UsernamePasswordToken.clear方法的具體用法?Java UsernamePasswordToken.clear怎麽用?Java UsernamePasswordToken.clear使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.shiro.authc.UsernamePasswordToken
的用法示例。
在下文中一共展示了UsernamePasswordToken.clear方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
/**
* 用戶登錄
* @param request
* @param user
* @param model
* @return
*/
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String login(HttpServletRequest request, AdminUser user, Model model) {
if (StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){
request.setAttribute("msg","用戶名或者密碼不能為空!");
return "login";
}
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken(user.getUsername(),user.getPassword());
try {
subject.login(token);
return "redirect:/initPage";
}catch (LockedAccountException lae) {
token.clear();
request.setAttribute("msg", "用戶已經被鎖定不能登錄,請與管理員聯係!");
return "login";
} catch (AuthenticationException e) {
token.clear();
request.setAttribute("msg", "用戶或密碼不正確!");
return "login";
}
}
示例2: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@PostMapping("/login")
public String login(HttpServletRequest request, User user, Model model){
if (StringUtils.isEmpty(user.getLoginId()) || StringUtils.isEmpty(user.getPassword())) {
request.setAttribute("msg", "用戶名或密碼不能為空!");
return "login";
}
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken(user.getLoginId(),user.getPassword());
try {
subject.login(token);
return "manage";
}catch (LockedAccountException lae) {
token.clear();
request.setAttribute("msg", "用戶已經被鎖定不能登錄,請與管理員聯係!");
return "login";
} catch (AuthenticationException e) {
token.clear();
request.setAttribute("msg", "用戶或密碼不正確!");
return "login";
}
}
示例3: validation
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@Log
@OnEvent(value = EventConstants.VALIDATE, component = "loginForm")
public void validation()
{
if (!loginForm.isValid()) {
return;
}
//LOG.debug("onValidateLoginForm [" + username + "] ");
try {
Subject subject = SecurityUtils.getSubject();
if (!subject.isAuthenticated()) {
// http://shiro.apache.org/static/1.2.2/apidocs/org/apache/shiro/authc/UsernamePasswordToken.html
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//token.setRememberMe(rememberMe);
subject.login(token);
token.clear();
LOG.debug("User [" + subject.getPrincipal() + "] logged in successfully.");
} else {
LOG.debug("User [" + subject.getPrincipal() + "] failed to log.");
}
} catch (Exception e) {
loginForm.recordError("Error " + e.getMessage());
}
}
示例4: postLogin
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@POST
@Path("/authentication")
@Consumes("application/x-www-form-urlencoded")
@PermitAll
public Response postLogin(@FormParam("j_username") String username,@FormParam("j_password") String password) {
Response.ResponseBuilder rb;
Subject subject;
try {
subject = securityService.getSubject();
if (!subject.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(token);
token.clear();
String userPassword= username + ":" + password;
String basicAuth = new String(Base64.encodeBytes(userPassword.getBytes()));
Token cltToken = new Token();
cltToken.setAccess_token("Basic "+basicAuth);
cltToken.setExpires_in(1799);
cltToken.setToken_type("bearer");
cltToken.setScope("read write");
rb = Response.ok(cltToken);
return rb.build();
} else {
LOG.debug("User [" + subject.getPrincipal() + "] already authenticated.");
if(subject.getPrincipal().toString().equals(username))
{
rb = Response.ok();
return rb.build();
}
}
} catch (Exception e) {
LOG.debug("User failed to log.");
}
JSONObject JSONEntity = new JSONObject();
JSONEntity.put("message","invalid user or password");
return Response.status(Response.Status.BAD_REQUEST).entity(JSONEntity.toString()).build();
}
示例5: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
private Response login(
HttpServletRequest request,
ServletContext servletContext,
UriInfo uriInfo,
String username,
String password,
String ftlFile) {
username = StringUtils.trimToNull(username);
password = StringUtils.trimToNull(password);
if (username == null) {
return displayError(servletContext, uriInfo, username, ftlFile, "UserNameMissing");
}
if (password == null) {
return displayError(servletContext, uriInfo, username, ftlFile, "PasswordMissing");
}
String ipAddress = findIpAddress(request);
UsernamePasswordToken token = new UsernamePasswordToken(username, password, false, ipAddress);
try {
Subject subject = AuthUtils.getSubject();
subject.login(token);
token.clear();
URI successUrl = null;
SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(request);
if (savedRequest != null && savedRequest.getMethod().equalsIgnoreCase(AccessControlFilter.GET_METHOD)) {
successUrl = uriInfo.getBaseUri().resolve(savedRequest.getRequestUrl());
} else {
if (ftlFile.equals(LOGIN_GREEN)) {
successUrl = WebHomeResource.uri(uriInfo);
} else {
successUrl = uriInfo.getBaseUri().resolve("/");
}
}
return Response.seeOther(successUrl).build();
} catch (AuthenticationException ex) {
String errorCode = StringUtils.substringBeforeLast(ex.getClass().getSimpleName(), "Exception");
return displayError(servletContext, uriInfo, username, ftlFile, errorCode);
}
}
示例6: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@RequestMapping(value = "/login" ,method = RequestMethod.POST)
public String login(User user, Model model, HttpServletRequest request) {
log.info("Login user=====" + user);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
String remember = WebUtils.getCleanParam(request, "remember");
log.info("remember=" + remember);
try {
if(remember != null && remember.equalsIgnoreCase("on")) {
token.setRememberMe(true);
}
subject.login(token);
return "redirect:/index";
} catch(UnknownAccountException ue) {
token.clear();
model.addAttribute("error", "登錄失敗,您輸入的賬號不存在");
return "security/login";
} catch(IncorrectCredentialsException ie) {
token.clear();
model.addAttribute("username", user.getUsername());
model.addAttribute("error", "登錄失敗,密碼不匹配");
return "security/login";
} catch(RuntimeException re) {
token.clear();
model.addAttribute("username", user.getUsername());
model.addAttribute("error", "登錄失敗");
return "security/login";
}
}
示例7: restlogin
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
/**
* 前後端分離的情況之下rest風格登錄獲取TGT ST
* @Description:TODO
* @author:hsj qq:2356899074
* @time:2017年12月1日 下午2:49:40
* @param req
* @param respon
* @return
* @throws Exception
*/
@RequestMapping("/restlogin")
public String restlogin(HttpServletRequest req,HttpServletResponse respon)throws Exception{
// return RestFulLogin.validateFromCAS("hsjhsj", "hsjhsj");
//校驗有沒有username和password
String username = "hsjhsj";
UsernamePasswordToken token = new UsernamePasswordToken("hsjhsj","hsjhsj");
//獲取當前的Subject
Subject currentUser = SecurityUtils.getSubject();
try {
//在調用了login方法後,SecurityManager會收到AuthenticationToken,並將其發送給已配置的Realm執行必須的認證檢查
//每個Realm都能在必要時對提交的AuthenticationTokens作出反應
//所以這一步在調用login(token)方法時,它會走到MyRealm.doGetAuthenticationInfo()方法中,具體驗證方式詳見此方法
logger.info("對用戶[" + username + "]進行登錄驗證..驗證開始");
currentUser.login(token);
logger.info("對用戶[" + username + "]進行登錄驗證..驗證通過");
}catch(UnknownAccountException uae){
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,未知賬戶");
return "403";
}catch(IncorrectCredentialsException ice){
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,錯誤的憑證");
return "403";
}catch(LockedAccountException lae){
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,賬戶已鎖定");
return "403";
}catch(ExcessiveAttemptsException eae){
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,錯誤次數過多");
return "403";
}catch(AuthenticationException ae){
//通過處理Shiro的運行時AuthenticationException就可以控製用戶登錄失敗或密碼錯誤時的情景
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,堆棧軌跡如下");
ae.printStackTrace();
return "403";
}
//驗證是否登錄成功
if(currentUser.isAuthenticated()){
logger.info("用戶[" + username + "]登錄認證通過(這裏可以進行一些認證通過後的一些係統參數初始化操作)");
return "inde";
}else{
token.clear();
return "403";
}
}
示例8: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@RequestMapping(value="/login", method=RequestMethod.POST)
public String login(String username, String password, HttpServletRequest request){
System.out.println("-------------------------------------------------------");
String rand = (String)request.getSession().getAttribute("rand");
String captcha = WebUtils.getCleanParam(request, "captcha");
System.out.println("用戶["+username+"]登錄時輸入的驗證碼為["+captcha+"],HttpSession中的驗證碼為["+rand+"]");
if(!StringUtils.equals(rand, captcha)){
request.setAttribute("message_login", "驗證碼不正確");
return InternalResourceViewResolver.FORWARD_URL_PREFIX + "/";
}
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
token.setRememberMe(true);
System.out.print("為驗證登錄用戶而封裝的Token:");
System.out.println(ReflectionToStringBuilder.toString(token, ToStringStyle.MULTI_LINE_STYLE));
//獲取當前的Subject
Subject currentUser = SecurityUtils.getSubject();
try {
//在調用了login方法後,SecurityManager會收到AuthenticationToken,並將其發送給已配置的Realm執行必須的認證檢查
//每個Realm都能在必要時對提交的AuthenticationTokens作出反應
//所以這一步在調用login(token)方法時,它會走到MyRealm.doGetAuthenticationInfo()方法中,具體驗證方式詳見此方法
System.out.println("對用戶[" + username + "]進行登錄驗證...驗證開始");
currentUser.login(token);
System.out.println("對用戶[" + username + "]進行登錄驗證...驗證通過");
}catch(UnknownAccountException uae){
System.out.println("對用戶[" + username + "]進行登錄驗證...驗證未通過,未知賬戶");
request.setAttribute("message_login", "未知賬戶");
}catch(IncorrectCredentialsException ice){
System.out.println("對用戶[" + username + "]進行登錄驗證...驗證未通過,錯誤的憑證");
request.setAttribute("message_login", "密碼不正確");
}catch(LockedAccountException lae){
System.out.println("對用戶[" + username + "]進行登錄驗證...驗證未通過,賬戶已鎖定");
request.setAttribute("message_login", "賬戶已鎖定");
}catch(ExcessiveAttemptsException eae){
System.out.println("對用戶[" + username + "]進行登錄驗證...驗證未通過,錯誤次數過多");
request.setAttribute("message_login", "用戶名或密碼錯誤次數過多");
}catch(AuthenticationException ae){
//通過處理Shiro的運行時AuthenticationException就可以控製用戶登錄失敗或密碼錯誤時的情景
System.out.println("對用戶[" + username + "]進行登錄驗證...驗證未通過,堆棧軌跡如下");
ae.printStackTrace();
request.setAttribute("message_login", "用戶名或密碼不正確");
}
//驗證是否登錄成功
if(currentUser.isAuthenticated()){
System.out.println("用戶[" + username + "]登錄認證通過(這裏可進行一些認證通過後的係統參數初始化操作)");
return "main";
}else{
token.clear();
return InternalResourceViewResolver.FORWARD_URL_PREFIX + "/";
}
}
示例9: login
import org.apache.shiro.authc.UsernamePasswordToken; //導入方法依賴的package包/類
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(@Valid User user,BindingResult bindingResult,RedirectAttributes redirectAttributes){
if(bindingResult.hasErrors()){
return "login";
}
String username = user.getUsername();
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
//獲取當前的Subject
Subject currentUser = SecurityUtils.getSubject();
try {
//在調用了login方法後,SecurityManager會收到AuthenticationToken,並將其發送給已配置的Realm執行必須的認證檢查
//每個Realm都能在必要時對提交的AuthenticationTokens作出反應
//所以這一步在調用login(token)方法時,它會走到MyRealm.doGetAuthenticationInfo()方法中,具體驗證方式詳見此方法
logger.info("對用戶[" + username + "]進行登錄驗證..驗證開始");
currentUser.login(token);
logger.info("對用戶[" + username + "]進行登錄驗證..驗證通過");
}catch(UnknownAccountException uae){
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,未知賬戶");
redirectAttributes.addFlashAttribute("message", "未知賬戶");
}catch(IncorrectCredentialsException ice){
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,錯誤的憑證");
redirectAttributes.addFlashAttribute("message", "密碼不正確");
}catch(LockedAccountException lae){
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,賬戶已鎖定");
redirectAttributes.addFlashAttribute("message", "賬戶已鎖定");
}catch(ExcessiveAttemptsException eae){
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,錯誤次數過多");
redirectAttributes.addFlashAttribute("message", "用戶名或密碼錯誤次數過多");
}catch(AuthenticationException ae){
//通過處理Shiro的運行時AuthenticationException就可以控製用戶登錄失敗或密碼錯誤時的情景
logger.info("對用戶[" + username + "]進行登錄驗證..驗證未通過,堆棧軌跡如下");
ae.printStackTrace();
redirectAttributes.addFlashAttribute("message", "用戶名或密碼不正確");
}
//驗證是否登錄成功
if(currentUser.isAuthenticated()){
logger.info("用戶[" + username + "]登錄認證通過(這裏可以進行一些認證通過後的一些係統參數初始化操作)");
return "redirect:/user";
}else{
token.clear();
return "redirect:/login";
}
}