<>springboot Realize user login function

This is also true here SpringMVC, The principle is the same
one , Front page , use validate Made some judgments
<script type="text/javascript"> $(function(){ $("#loginForm").validate({ rules:
{ "username": { required: true }, "password":{ required:true } }, messages:{
"username":{ required:" The user name cannot be empty " }, "password":{ required:" Password cannot be empty " } },
submitHandler:function(form){ $(form).ajaxSubmit({ dataType:'json', success:
function(data){ if(data.success){ $.messager.confirm(" Prompt information "," Login successful ",function(){
window.location.href="/personal"; }) }else{ $.messager.popup(data.msg); } } });
}, // Custom error style errorClass:"text-danger", // Failed validation , Highlight or otherwise ; highlight:function(
input){ $(input).closest(".form-group").addClass("has-error"); },
// Pass the verification , Clear highlights or other treatments ; unhighlight:function(input){ $(input).closest(".form-group")
.removeClass("has-error"); } }); }); </script>
Second, the background implementation
1.controller
/** * Login control */ @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(" Wrong user name or password "); }else{ UserContext.putCurrebtUser(logininfo); }
return result; }
2.userContext Tools ( The core code is here )
/** * Save and get the tool class of the current user * Created by lenovo on 2020/1/16. */ public class
UserContext { private static final String CURRENT_USER_IN_SESSION = "logininfo";
/** * obtain session */ private static HttpSession getSession(){
//SpringMVC obtain session By way of RequestContextHolder return ((ServletRequestAttributes)
RequestContextHolder.currentRequestAttributes()).getRequest().getSession(); }
/** * Set current user to session in */ public static void putCurrebtUser(Logininfo currentUser
) { getSession().setAttribute(CURRENT_USER_IN_SESSION,currentUser); } /** *
Get current user */ public static Logininfo getCurreentUser() { return (Logininfo)
getSession().getAttribute(CURRENT_USER_IN_SESSION); } }
3.mapper
/** * User login * @param username user name * @param encode password * @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>
three :
What we do here is when the login is successful , Save the current user's information to session in , This is very important , Because do page Jump will be a lot of use to the current user's information
Using RequestContextHolder
RequestContextHolder seeing the name of a thing one thinks of its function , Contextual Request container .
The specific implementation is as follows :
// The two methods are not used in the JSF There is no difference between the two projects RequestAttributes requestAttributes =
RequestContextHolder.currentRequestAttributes();
//RequestContextHolder.getRequestAttributes(); // from session Get the corresponding value String str = (
String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);
HttpServletRequest request= ((ServletRequestAttributes)requestAttributes).
getRequest(); HttpServletResponse response = ((ServletRequestAttributes)
requestAttributes).getResponse();
2.RequestContextHolder This class , There are two in it ThreadLocal Save the current thread request
// Get what's stored in it request private static final ThreadLocal<RequestAttributes>
requestAttributesHolder= new NamedThreadLocal<RequestAttributes>("Request
attributes"); // Inheritable by child threads request private static final ThreadLocal<
RequestAttributes> inheritableRequestAttributesHolder = new
NamedInheritableThreadLocal<RequestAttributes>("Request context");

3.getRequestAttributes()` method , Equivalent to direct access ThreadLocal The value inside , In this way, we can ensure that every time we get the information Request That's the reason for the request request
public static RequestAttributes getRequestAttributes() { RequestAttributes
attributes= requestAttributesHolder.get(); if (attributes == null) { attributes
= inheritableRequestAttributesHolder.get(); } return attributes; }
I'm just looking at the source code , concrete request and response When was waiting set up? I haven't got a deep understanding .

Technology