Spring Security介绍
    Spring Security是一个功能强大且可高度自定义的身份验证和访问控制框架。它是保护基于Spring的应用程序的框架。
    Spring Security是一个专注于为Java应用程序提供身份验证和授权的框架。与所有Spring项目一样,Spring Security的真正强大之处在于它可以轻松扩展以满足自定义要求

    • springboot版本 2.1.8 接下来的演示都采用的最新的SpringBoot的版本
    1. 导入pom文件 引入springsecurity的依赖
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>2.1.8.RELEASE</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>com.example</groupId>
    12. <artifactId>spring_security</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <name>spring_security</name>
    15. <description>Demo project for Spring Boot</description>
    16. <properties>
    17. <java.version>1.8</java.version>
    18. </properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.boot</groupId>
    22. <artifactId>spring-boot-starter-web</artifactId>
    23. </dependency>
    24. <dependency>
    25. <groupId>org.springframework.boot</groupId>
    26. <artifactId>spring-boot-starter-test</artifactId>
    27. <scope>test</scope>
    28. </dependency>
    29. <dependency>
    30. <groupId>org.projectlombok</groupId>
    31. <artifactId>lombok</artifactId>
    32. <optional>true</optional>
    33. </dependency>
    34. <dependency>
    35. <groupId>org.springframework.boot</groupId>
    36. <artifactId>spring-boot-starter-freemarker</artifactId>
    37. </dependency>
    38. <dependency>
    39. <groupId>org.springframework.boot</groupId>
    40. <artifactId>spring-boot-starter-security</artifactId>
    41. </dependency>
    42. </dependencies>
    43. <build>
    44. <plugins>
    45. <plugin>
    46. <groupId>org.springframework.boot</groupId>
    47. <artifactId>spring-boot-maven-plugin</artifactId>
    48. </plugin>
    49. </plugins>
    50. </build>
    51. </project>

    配置application.yml

    server:
      port: 8080
    spring:
      freemarker:
        enabled: true
        cache: false
        template-loader-path: classpath:/templates/
        suffix: .html
      security:
        user:
          name: user
          password: user
          roles: user,admin
    

    编写 hello.html login.html hello页面为展示页面 login页面为 登录界面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>helloworld</title>
    </head>
    <body>
            第一个Security应用
    </body>
    </html>
    

    编写helloworld控制层

    @Controller
    public class HelloWorld {
    
        @RequestMapping(value = "/hello")
        public String hello(){
            return "hello";
        }
    }
    

    在这个时候我们可以在地址栏输入http://localhost:8080/hello
    页面跳转到了security的默认配置页
    SpringSecurity之Hello-World - 图1
    输入yml文件配置的用户名和密码
    SpringSecurity之Hello-World - 图2