The previous article mainly introduces the SSM integration and testing.
This article mainly introduces the login module, the login module used to filter, the configuration of the filter needs to be configured inside the Web.xml, the relevant configuration has been web.xml in the second part of the note.
The classes covered in this article are: Control layer Logincontroller, filter checkloginfilter and database access.
Controller layer: If the login is successful, set the user to the session, and then combine the front-end JS to determine whether the user is empty to show the mask layer or not, and set session expiration time. MD5 encryption is recommended for user passwords when they are stored in the database.
Package Com.tdrip.controller;
Import javax.servlet.http.HttpSession;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RestController;
Import Com.tdrip.model.util.ServiceResult;
Import Com.tdrip.service.OperatorService;
@RestController public class Logincontroller {@Autowired private operatorservice operatorservice;
@Autowired private HttpSession session; @RequestMapping (value = "/login/login", method=requestmethod.post) public serviceresult login (String password) {Servic
Eresult serviceresult = Operatorservice.findbyid (password);
if (null!= serviceresult.getdata ()) {Session.setattribute ("admin", Serviceresult.getdata ());
No activity 10 minutes after session failure Session.setmaxinactiveinterval (10*60);
return serviceresult; }
}
Operatorlservice: operator service, SERIVCE layer for login verification
Package Com.tdrip.service.impl;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import Com.tdrip.mapper.OperatorMapper;
Import Com.tdrip.model.db.OperatorModel;
Import Com.tdrip.model.util.ServiceResult;
Import Com.tdrip.service.OperatorService;
Import Com.tdrip.util.ToolUtil;
@Service public
class Operatorserviceimpl implements Operatorservice {
@Autowired
private Operatormapper Operatormapper;
@Override public
serviceresult FindByID (string password) {
string md5 = TOOLUTIL.GETMD5 (password);
Operatormodel model = Operatormapper.selectbyid (MD5);
if (model!= null) {return
Serviceresult.return (model);
}
Return Serviceresult.build (-1, "Password error.") ");
}
}
Operatormapper Interface:
Package com.tdrip.mapper;
Import org.springframework.stereotype.Repository;
Import Com.tdrip.model.db.OperatorModel;
@Repository Public
interface Operatormapper {public
Operatormodel Selectbyid (String ID);
Public Operatormodel Selectlikeid (String ID);
public int Insert (Operatormodel model);
}
Operatormpper.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "Com.tdrip.mapper.OperatorMapper" >
<resultmap type= " Com.tdrip.model.db.OperatorModel "id=" Operatormodelresult ">
<id property=" id "column=" id "/>
< Result property= "CUTC" column= "Cutc"/> <result property= "
permission" column= "permission"/>
</ resultmap>
<select id= "Selectbyid" resultmap= "Operatormodelresult" >
select ID, CUTC, permission From
operator
WHERE id = #{id}
</select>
<select id= "Selectlikeid" resultmap= "Operatormodelresult" >
SELECT IDs, permission from
operator
WHERE ids like CONCAT ('% ', #{id}, '% ')
</select>
< Insert id= Insert > INSERT INTO
operator (ID, CUTC) VALUES (#{id}, #{cutc})
</insert>
</ Mapper>
Checkloginfilter: This class needs to implement the filter interface, the filtering implementation method is mainly dofilter.
Package com.tdrip.filter;
Import java.io.IOException;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession; public class Checkloginfilter implements Filter {@Override public void init (Filterconfig filterconfig) throws Servlete xception {//TODO auto-generated Method stub}/** * Filtering process: * 1, if the access is the Home page index or static file (CSS,JS) and so on direct access through * 2, if
is a login request or query all content requests through * 3, does not meet the above two points will require the user to login. * * @Override public void Dofilter (ServletRequest req, servletresponse Res, Filterchain chain) throws IOException, Ser
vletexception {httpservletrequest request = (httpservletrequest) req;
HttpServletResponse response = (httpservletresponse) res;
HttpSession session = Request.getsession (); String Requesturl = Request.getrequesturi (); Boolean conditionpass = Requesturl.contains ("/index") | | Requesturl.endswith (". html") | |
Requesturl.contains ("/resource") | | Requesturl.endswith (". js") | | Requesturl.endswith (". css") | |
Requesturl.endswith (". ico");
if (conditionpass) {chain.dofilter (request, response); else {if (Requesturl.contains ("/login/login") | | | requesturl.endswith ("/content/findall")) {Chain.dofilter (reque
St, response);
else {Boolean loggedIn = Session!= null && session.getattribute ("admin")!= null;
if (loggedIn) {chain.dofilter (request, response);
else {request.getrequestdispatcher ("index"). Forward (request, response);
@Override public void Destroy () {//TODO auto-generated Method stub}}}
PS: This article describes the login module and filter. Because the landlord has not touched the Shiro frame before, so use the basic filter, interested in the small partner can go to see the Shiro framework, about the authorization to sign in. Landlord recently in the integration of their own so far learned things to a project, which also used the Shiro, and then launched.
The next article starts with the introduction of Redis and integrates into spring.