springboot实现用户登录功能
这里也适用于SpringMVC,使用原理是一样的
一,前台页面,使用validate做了一些判断
<script type="text/javascript">
$(function(){
$("#loginForm").validate({
rules: {
"username": {
required: true
},
"password":{
required:true
}
},
messages:{
"username":{
required:"用户名不能为空"
},
"password":{
required:"密码不能为空"
}
},
submitHandler:function(form){
$(form).ajaxSubmit({
dataType:'json',
success:function(data){
if(data.success){
$.messager.confirm("提示信息","登录成功",function(){
window.location.href="/personal";
})
}else{
$.messager.popup(data.msg);
} }
});
},
//自定义错误样式
errorClass:"text-danger",
//未通过验证,进行高亮处理或其他处理;
highlight:function(input){
$(input).closest(".form-group").addClass("has-error");
},
//通过验证,清除高亮效果或其他处理;
unhighlight:function(input){
$(input).closest(".form-group").removeClass("has-error");
}
});
});
</script>
二后台实现
1.controller
/**
* 登录控制
*/
@RequestMapping("login")
@ResponseBody
public AjaxResult login(String username,String password){
AjaxResult result = new AjaxResult();
Logininfo logininfo = service.login(username, password);
if(logininfo==null){
result.setMsg("用户名或密码错误");
}else{
UserContext.putCurrebtUser(logininfo);
}
return result;
}
2.userContext工具类(核心代码在这里)
/**
* 保存和获取当前用户的工具类
* Created by lenovo on 2020/1/16.
*/
public class UserContext {
private static final String CURRENT_USER_IN_SESSION = "logininfo";
/**
* 得到session
*/
private static HttpSession getSession(){
//SpringMVC获取session的方式通过RequestContextHolder
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
}
/**
* 设置当前用户到session中
*/
public static void putCurrebtUser(Logininfo currentUser) {
getSession().setAttribute(CURRENT_USER_IN_SESSION,currentUser);
}
/**
* 获取当前用户
*/
public static Logininfo getCurreentUser() {
return (Logininfo) getSession().getAttribute(CURRENT_USER_IN_SESSION);
}
}
3.mapper
/**
* 用户登录
* @param username 用户名
* @param encode 密码
* @return
*/
Logininfo login(@Param("username") String username, @Param("password") String encode);
4.mapper.xml
<select id="login" resultMap="BaseResultMap">
SELECT <include refid="base_column"/>
FROM logininfo
WHERE username=#{username} and password=#{password}
</select>
三:
这里做的就是当登录成功后,把当前用户的信息保存到session中,这个是很重要的,因为做页面跳转是会大量使用到当前用户的信息
使用的是RequestContextHolder
RequestContextHolder顾名思义,持有上下文的Request容器.
具体实现如下:
//两个方法在没有使用JSF的项目中是没有区别的
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
//RequestContextHolder.getRequestAttributes();
//从session里面获取对应的值
String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
2.RequestContextHolder这个类,里面有两个ThreadLocal保存当前线程下的request
//得到存储进去的request
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes");
//可被子线程继承的request
private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
new NamedInheritableThreadLocal<RequestAttributes>("Request context");
3.getRequestAttributes()`方法,相当于直接获取ThreadLocal里面的值,这样就保证了每一次获取到的Request是该请求的request
public static RequestAttributes getRequestAttributes() {
RequestAttributes attributes = requestAttributesHolder.get();
if (attributes == null) {
attributes = inheritableRequestAttributesHolder.get();
}
return attributes;
}
我这里只是简单的看了一下源码,具体的request和response等是什么时候设置进去的等还没有深入了解.