Log 5.3 staff management systems and filters

A. Log in functions to achieve

1. Step:

  1. Modify the login form action and method using post submission, input required to submit to add the label name attribute, and add error display

  2. Add Controller

  3. Custom view edit control

2. Modify the login form action and method using post submission, input required to submit to add the label name attribute, and add error display

(1) Overall Code

 1 <form class="form-signin" th:action="@{/user/login}" method="post">
 2     <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
 3     <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
 4 
 5     <!--判断是否显示,使用if,$ {} Class tools can be used, you can see thymeleaf Chinese documents <6->
     p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
 7 
 8     <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
 9     <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
10     <div class="checkbox mb-3">
11         <label>
12   <input type="checkbox" value="remember-me" > [[#{login.remember}]]
13 </label>
14     </div>
15     <button class="btn btn-lg btn-primary btn-block" type="submit" >[[#{login.btn}]]</button>
16     <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
17     <a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
18     <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
19 </form>
View Code

(2) adding an action attribute method and methods

<form class="form-signin" th:action="@{/user/login}" method="post">

(3) input labeling required to submit to add the name attribute

<input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">

(4) add error display

<! - determining whether to use if, $ {} class tools can be used, you can see thymeleaf Chinese documents -> 
< P style = "Color: Red" TH: text = "{$ MSG}" TH: IF = "$ {# Not strings.isEmpty (MSG)}" > </ P >

3. Add the controller LoginController.java

  • If the username is not empty and when the password is 123456, the redirect to main.html

 1 import org.springframework.stereotype.Controller;
 2 import org.springframework.ui.Model;
 3 import org.springframework.util.StringUtils;
 4 import org.springframework.web.bind.annotation.PostMapping;
 5 import org.springframework.web.bind.annotation.RequestParam;
 6 
 7 @Controller
 8 public class LoginController {
 9 
10     //@RequestMapping(value = "/user/login",method = RequestMethod.POST)
11     @PostMapping("/user/login")
12     publicLogin String (@RequestParam ( "username" ) String username,
 13 is                          @RequestParam ( "password" ) String password,
 14                          the Model Model) {
 15  
16          IF (! StringUtils.isEmpty (username) && "123456" .equals (password)) {
 17              // successful login! Prevent duplicate submission form, we redirect 
18              return "redirect: /main.html" ;
 19          } the else {
 20              // Login failed! Error message store 
21              model.addAttribute ( "msg", "username password error" );
 22              return "index" ;
         }
24     }
25 }

4. custom view edit control

  • In addViewControllers method of MyMvcConfig.java

registry.addViewController("/main.html").setViewName("dashboard");

II. The filter function implemented

1. Step:

  1. login methods modify LoginController.java Adds HttpSession parameters, a successful login settings are saved variables

  2. Add filters in the config folder and override the pre-filtration

  3. Registered in the filter MyMvcConfig.java

  4. The revised successful jump page, the user name display

2. Modify the login method LoginController.java Adds HttpSession parameters, a successful login settings are saved variables

. 1 @PostMapping ( "/ User / Login" )
 2  public String Login (@RequestParam ( "username" ) String username,
 . 3                      @RequestParam ( "password" ) String password,
 . 4                      the Model Model , the HttpSession the session ) {
 . 5  
. 6      IF (! StringUtils.isEmpty (username) && "123456" .equals (password)) {
 7  
8          session.setAttribute ( "loginUser", username);
 9          // successful login! Prevent duplicate submission form, we redirect 
10          return "redirect: /main.html" ;
         // Login failed! Storing the error information 
13 is          model.addAttribute ( "MSG", "username password error" );
 14          return "index" ;
 15      }
 16 }

3. Add the filter in the config folder and override the pre-filtration

LoginHandlerInterceptor.java: user name is not allowed to log empty

 1 import org.springframework.web.servlet.HandlerInterceptor;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 public class LoginHandlerInterceptor implements HandlerInterceptor {
 7     //目标方法执行之前
 8     @Override
 9     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
10         Object user = request.getSession().getAttribute("loginUser");
11          IF (the User == null ) { // not logged in, return to the login page 
12              request.setAttribute ( "msg", "do not have permission, please login" );
 13              request.getRequestDispatcher ( "/ index.html" ) .forward (Request, Response);
 14              return  to false ;
 15          } the else {
 16              // log clearance 
. 17              return  to true ;
 18 is          }
 . 19      }
 20 is }

4. register the filter MyMvcConfig.java

  • addPathPatterns Method: Set the filtered file, / * all files under the directory resources

  • excludePathPatterns method: Do not filter files only

1  // register the interceptor 
2  @Override
 3  public  void addInterceptors (InterceptorRegistry Registry) {
 4      // register the interceptor and intercepts the request and for which requests excludePathPatterns addPathPatterns removed!
 5      // we need to filter static resource files, or style display out 
. 6      registry.addInterceptor ( new new LoginHandlerInterceptor ())
 . 7              .addPathPatterns ( "/ **"). excludePathPatterns ( "/ index.html", "/", "/ User / Login", "/ CSS / **" , "/ JS / **", "/ IMG / **" );
 . 8 }

5. Modify the success of the jump page, the user name display

dashboard.html:

th:text="${session.loginUser}"

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/zhihaospace/p/12387600.html
5.3