Spring Security入门案例实战(spring-security)

1 文档

官方文档:https://spring.io/projects/spring-security/

Github:https://github.com/spring-projects/spring-security-samples

2 项目结构

3 主要代码

3.1 pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath />
    </parent>

    <groupId>org.example</groupId>
    <artifactId>spring_security</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

</project>

3.2 启动类

package com.zydgbbs.springsecurity;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringSecurityApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringSecurityApplication.class);
    }
}

3.3 请求路由类

package com.zydgbbs.springsecurity.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

    @GetMapping("/")
    public String index(){
        return "index";
    }
}

3.4 index.html页面

<html xmlns:th="https://www.thymeleaf.org">
<head>
  <title>Hello Security!</title>
</head>
<body>
<h1>Hello Security</h1>
<a th:href="@{/logout}">Log Out</a>
</body>
</html>

3.5 application.properties配置

# 默认端口8080
#server.port=8090

# 可以修改访问路径前缀
#server.servlet.context-path=/demo

# 自定义用户名和密码
#spring.security.user.name=admin
#spring.security.user.password=admin

4 访问测试

4.1 启动

# 启动时会把默认用户【user】的密码输出出来,方便登录
Using generated security password: bfbe3cdd-ade7-42b4-afe5-56cd8a35e553

4.2 登录

# 访问IP
http://localhost:8080/login
# 输入用户名和密码即可
Username:user
Password:bfbe3cdd-ade7-42b4-afe5-56cd8a35e553

4.3 资源页面

4.4 登出页面

5 坑

当我们访问登录页面时,会非常慢,甚至加载不出来,因为登录页面引用了一个国外的地址。https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css,需要配置代理才能下载下来。

【温馨提示】

点赞+收藏文章,关注我并私信回复【面试题解析】,即可100%免费领取楼主的所有面试题资料!