Extensions to be mastered in the SpringBoot project

A SpringBoot static resource access path:

(1) Default access path for static resources:

"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"

(2) Custom static resource path

spring.resources.static-locations=classpath:/coding/,classpath:/glp/

General:
css, js, etc. are placed in the static folder and
html is placed in the templates folder

(3) Website homepage:
all index.htmlpages under the static resource folder ; mapped by /**.
For example, if I visit http://localhost:8080/, I will find index.html under the static resource folder

(4) Website icon:
Spring Boot searches in the configured static content location favicon.ico. If such a file exists, it will automatically be used as the favicon of the application.

#关闭默认图标
spring.mvc.favicon.enabled=false

Two template engine Thymeleaf

SpringBoot does not support by default. The default is not to support jsp, and Thymeleafthe idea is the same as JSP, which is to define a template to import data and then generate a page, but the syntax is different.

Need to import Thymeleaf dependencies

Official document: https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter

The default prefix of thymeleaf is classpath:/templates/, so we only need to put the html page under templates in the classpath, and thymeleaf can automatically render it for us. Nothing needs to be configured to use thymeleaf, just put it in the specified folder.

(1) Write a TestController

@RequestMapping("/t1")
public String test1(Model model){
    
    
   //存入数据
   model.addAttribute("msg","Hello,Thymeleaf");
   //classpath:/templates/test.html
   return "test";
}

(2) Write a test page test.html and place it in the templates directory. To
use thymeleaf, you need to import namespace constraints in the html filexmlns:th="http://www.thymeleaf.org"

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
   <meta charset="UTF-8">
   <title>狂神说</title>
</head>
<body>
<h1>测试页面</h1>
<!--th:text就是将div中的内容设置为它指定的值,和之前学习的Vue一样-->
<div th:text="${msg}"></div>
</body>
</html>

Grammar learning of thymeleaf: Https://www.thymeleaf.org/
Use any th:attrto replace the value of the native attribute in Html

The page is cached, so we need to disable the cache of the template engine

#禁用模板缓存
spring.thymeleaf.cache=false

Three extension configuration of SpringMVC

(1) Write SpringMVC configuration classes to implement WebMvcConfigurerinterfaces.

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    
    

  //自己定义一个视图类
   @Override
   public void addViewControllers(ViewControllerRegistry registry) {
    
    
       // 浏览器发送/test ,就会跳转到test页面;
       registry.addViewController("/test").setViewName("test");
  }
}

(2) The basic configuration of
SpringMVC SpringMVC uses @RequestBody and @ResponseBody to serialize/deserialize json data (request/response json data processing) configuration

# 美化JSON数据格式
spring.jackson.serialization.indent-output=true
# 设置JSON数据的日期格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
# JSON数据属性为null时不返回
spring.jackson.default-property-inclusion=non_null

(3) When SpringMVC uses @RequestParam (request parameter), the date format
defaults to yyyy/MM/dd

spring.mvc.date-format=yyyy-MM-dd HH:mm:ss

Application context path, the root path of the project

server.servlet.context-path = /lucky

(4) Login interceptor

1) Custom interceptor:

public class LoginHandlerInterceptor implements HandlerInterceptor {
    
    
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) throws Exception {
    
    
       // 获取loginUser 信息进行判断
       Object user = request.getSession().getAttribute("loginUser");
       if (user == null){
    
     // 未登录,返回登录页面
           request.setAttribute("msg","没有权限,请先登录");
         
 request.getRequestDispatcher("/index.html").forward(request,response);
           return false;
      }else {
    
    
           // 登录,放行
           return true;
      }
  }
}

2) Register the interceptor to our SpringMVC configuration class

@Override
public void addInterceptors(InterceptorRegistry registry) {
    
    
   // 注册拦截器,及拦截请求和要剔除哪些请求!
   // 我们还需要过滤静态资源文件,否则样式显示不出来
   registry.addInterceptor(new LoginHandlerInterceptor())
      .addPathPatterns("/**")
      .excludePathPatterns("/index.html","/","/user/login","/asserts/**");
}

Four use Restful style to realize our crud operation

Insert picture description here

Five integrated Swagger

It can automatically generate interface documents and client-side server-side code to achieve the consistency of call-side code, server-side code and interface documentation. Introducing Springfox into the project, you can scan the relevant code, generate the description file, and then generate the interface document and client code consistent with the code. This form of generating interface documents through code is particularly important and efficient in subsequent projects that require continuous iteration.

Six asynchronous tasks

Seven timed tasks

Eight mail sending

Nine to solve security problems: Shiro, SpringSecurity

Spring Security (Permission Framework) is a powerful and highly customizable authentication and access control framework. Generally speaking, the security of web applications includesUser authentication (Authentication) and user authorization (Authorization) Two parts.

  • User authentication refers to verifying whether a user is a legal subject in the system, that is, whether the user can access the system. User authentication generally requires the user to provide a user name and password. The system completes the authentication process by verifying the user name and password.
  • User authorization refers to verifying whether a user has the authority to perform a certain operation. In a system, different users have different permissions.

Apache Shiro is a Java security (permission) framework.
Shiro can easily develop good enough applications, which can be used not only in the JavaSE environment, but also in the JavaEE environment. Shiro can complete, authentication, authorization, encryption, session management, Web integration, caching, etc.

Guess you like

Origin blog.csdn.net/glpghz/article/details/108697133