[IDEA Sprintboot] Simple introduction: integrating SpringSecurity dependencies and integrating Thymeleaf framework

Table of contents:

1. [IDEA] Simple Getting Started: Request Database Table Data_Water w Blog-CSDN Blog

Table of contents

three,

1. Integrate Spring Security dependencies

2. Integrate the Thymeleaf framework

Solve the problem that static resources such as css styles cannot be accessed


three,

1. Integrate Spring Security dependencies

Spring Security is a security framework that can provide declarative security access control solutions for Spring-based enterprise application systems. Mainly used for login.

Let's then integrate the SpringSecurity dependencies,

(1) You only need to import SpringSecurity's Starter dependency:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

After importing dependencies, we directly start the SpringBoot application, and we can find that SpringSecurity has taken effect.

Spring Security will automatically generate a default user for us user, and the password is a string randomly generated when the project starts. You can see the default password from the startup console log

 Among them, de505234-578f-452d-b552-94a4a66a8755 is a randomly generated password, and we can use this user to log in.

Alternatively, you can directly configure the basic information of the user in the application.properties file,

spring.security.user.name=test
spring.security.user.password=123456

(2) Visit http://localhost:8088 with the browser, and log in with the default username and password generated by sprintboot.

The jump is successful.

(3) In the development stage, we often encounter the problem of Security permission interception. At this point, we can directly add the following configuration to the main startup class of Springboot to turn off Spring Security,

// 给springboot主方法中的注解去除如下类
@SpringBootApplication(exclude= {SecurityAutoConfiguration.class })

2. Integrate the Thymeleaf framework

Thymeleaf is a modern server-side Java templating engine for web and standalone environments.

The main goal of Thymeleaf is to bring elegant natural templating into your development workflow - HTML that renders correctly in browsers and can be used as a static prototype, enabling stronger collaboration among development teams. Thymeleaf is able to handle HTML, XML, JavaScript, CSS and even plain text.

The main goal of Thymeleaf is to provide an elegant and highly maintainable way of creating templates. To achieve this, it builds on the concept of natural templates, injecting their logic into template files in a way that does not affect the template as a design prototype. This improves design communication and bridges the understanding gap between front-end designers and developers.

Thymeleaf was also designed from the ground up (specifically HTML5) to allow the creation of fully validating templates.

(1) To integrate Thymeleaf, you only need to import the corresponding starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

(2) Then we only need to use it directly:

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

But note that this can only parse HTML pages normally, but we need to specify the path for static resources such as js and css, otherwise they cannot be accessed. Let's configure the access prefix of static resources in the configuration file: 

# 表示只有静态资源的访问路径为/root/**时,才会处理请求
spring.mvc.static-path-pattern=/static/**

Setting the application method is very simple, mainly involving two configuration items:

  • spring.mvc.static-path-pattern : According to the description and actual effect of the official website, it can be understood as the static file URL matching header, which is the beginning of the URL address of the static file.
    • Springboot defaults to: /**.
    • The meaning of the representative is what path we should use to access static resources. In other words, Spring Boot will only process static resource requests if the static resources meet the matching conditions.
  • spring.web.resources.static-locations : According to the description and actual effect of the official website, it can be understood as the actual static file address, that is, the actual static file matched after the static file URL.
    • Springboot defaults to:
        classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
      

Solve the problem that static resources such as css styles cannot be accessed

Created a static resource directory under the resources/ static /directory in SpringBoot , and then in the html file I initially referenced it as shown below.

<img th:src="@{/static/picture/auth-img1.png}" alt="">

However, no styles appear.

Reason analysis : This is because the default static resource path of Springboot is static, we don't need to add the /static/ prefix, so we need to use the correct way to reference, otherwise it will cause 404 problems.

<img th:src="@{/picture/auth-img1.png}" alt="">

(3) If the files under templates are still not accessible,

Solution : create a new config folder, and then create a WebConfiguration class in it,

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //这里是指在url上面打的内容
        registry.addResourceHandler("/**")
                //下面的是指可以对应resources文件下那些内容
                .addResourceLocations("classpath:/")
                .addResourceLocations("classpath:/templates/")
                .addResourceLocations("classpath:/static/**");
    }
}

(4) The current directory structure is shown in the figure below: 

Start the project again, successfully, without error.

Browser access http://localhost:8088/user/findAll, you can see,

Guess you like

Origin blog.csdn.net/qq_45956730/article/details/130487955