简单介绍:需求上有个小功能,用户登录后点击用户名,可以点击锁屏,锁屏解除,需要输入正确的密码才能进入管理后台页面enheng(*/ω\*)(*/ω\*)(*/ω\*)
思路简介:其实刚看到要做锁屏,第一反应就是没做过啊,懵逼,后来中午和同事去吃饭的时候,我啪的一下,就把笔记本合上了,loading·············,吃完饭,回来,打开本子,输入完密码,就准备去点击登录那个箭头,突然反应过来,卧槽,这不就是锁屏么,给他个锁屏界面不就行了么,不过还要有几个表单,能够实现登录,不就相当于解锁了么!!!haヾ(◍°∇°◍)ノ゙༺༺超༒神༻༻
//html代码 点击锁屏进入controller的lock方法 <li>
<a href="lock" >
<i class="icon-lock"></i> 锁屏 </a>
</li>
//controller代码 /**
* 跳转锁屏页面
*/
@RequestMapping(value = "/main/lock")
public String lock(Model model) throws Exception {
Session session = Jurisdiction.getSession();
OhSysPriUser user = (OhSysPriUser)session.getAttribute(SessionConst.SESSION_USER);
String name = user.getLoginName();
model.addAttribute("username", name);
logger.info("进入锁屏");
return "/lock";//进入锁屏页面
}
//html锁屏页面登录 <form action="locklogin.shtml" method="post" name="lockloginForm" id="lockloginForm">
<div class="control-group normal_text"><h3>锁屏登录</h3></div>
<div class="control-group">
<input id="inputbtn" class="form-contro" type="text" autocomplete="off"
placeholder="Username" name="username" th:value="${username}" readonly="readonly"/>
</div>
<div class="control-group">
<input type="password" name="password" id="password" placeholder="请输入密码"
class="keypad" keypadMode="full" allowKeyboard="true" value=""/>
</div>
<div class="form-actions">
<div style="86%;padding-left:8%;">
<span class="pull-right">
<a onclick="locklogin();" class="flip-link btn btn-info" id="to-recover">登录</a>
</span>
</div>
<div style="86%;padding-left:8%;">
<span class="pull-left">
<a href="login" class="flip-link btn btn-info" id="to-login">前往登录页面</a>
</span>
</div>
</div>
</form>
/**
* js代码 锁屏登录
*/
function locklogin() {
if ($("#password").val() == "") {
$("#password").tips({
side: 2,
msg: '密码不得为空',
bg: '#AE81FF',
time: 3
});
$("#password").focus;
}else{
$.ajax({
type: "POST",
url: 'locklogin',
data: {
username : $("#inputbtn").val(),
password : $("#password").val()
},
dataType: 'json',
cache: false,
success : function (data) {
if(data=="success"){
window.location.href="index";//成功回到主页
}else{
$("#password").tips({
side: 2,
msg: data,
bg: '#ff4a54',
time: 3
});
$("#password").val("");
$("#password").focus;
}
}
});
}
}
/**
* Controller代码 锁屏登陆
*
* @param username
* @param password
* @param request
* @param model
* @return
* */
@ResponseBody
@RequestMapping(value = "/main/locklogin", method = RequestMethod.POST, produces = "text/html; charset=utf-8")
public String locklogin(String username, String password, HttpServletRequest request, Model model) {
String error = "";
try {
model.addAttribute("username", username);
if (!request.getMethod().equals("POST")) {
model.addAttribute("errors","支持POST方法提交!");
}
if (Tools.isEmpty(username) || Tools.isEmpty(password)) {
error = "密码不能为空!";
}
// 想要得到 SecurityUtils.getSubject() 的对象..访问地址必须跟 shiro 的拦截地址内.不然后会报空指针
Subject user = SecurityUtils.getSubject();
// 用户输入的账号和密码,,存到UsernamePasswordToken对象中..然后由shiro内部认证对比,
// 认证执行者交由ShiroDbRealm中doGetAuthenticationInfo处理
// 当以上认证成功后会向下执行,认证失败会抛出异常
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
user.login(token);
error = "success";
logger.info("解锁成功");
}catch (AuthenticationException e) {
token.clear();
request.setAttribute("error", "密码不正确!");
error = "密码不正确!";
}
} catch (Exception e) {
e.printStackTrace();
error = "登录异常,请联系管理员!";
logger.info("登录异常,请联系管理员!");
// 注销登录
logger.info(">>>>>>>>>> Logout");
SecurityUtils.getSubject().logout();
}
return error;
}
总结:刚拿到锁屏,瞬间懵逼,不过总得去做不是么,ヾ(◍°∇°◍)ノ゙