Spring Security - 案例

Spring Security


目录

  • Spring Security
    • 简介
    • 案例
      • 1. `pom.xml`
        • 1.1 项目结构
        • 1.2 静态资源 - `templates`
        • 1.3 运行效果 - 初始效果
      • 2. 主程序
        • 2.1 启动类
        • 2.2 Controller
        • 2.3 配置类 - SecurityConfig
      • 3. 运行效果


简介

  • Spring Security是针对Spring项目的安全框架,也是SpringBoot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理!
  • 重要Java类
    • WebSecurityConfigurerAdapter:自定义Security策略
    • AuthenticationManagerBuilder:自定义认证策略
    • @EnableWebSecurity:开启WebSecurity模式
  • Spring Security的两个主要目标是“认证 - Authentication”和“授权 - Authorization”(访问控制)

案例

1. pom.xml


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
	  <groupId>com.murphygroupId>
    <artifactId>springsecurityartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>springsecurityname>
    
  <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.0.RELEASEversion>
        <relativePath/> 
    parent>
    
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
      	
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-securityartifactId>
        dependency>
        
        <dependency>
            <groupId>org.thymeleaf.extrasgroupId>
            <artifactId>thymeleaf-extras-springsecurity5artifactId>
            <version>3.0.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>
1.1 项目结构

Spring Security - 案例_第1张图片

1.2 静态资源 - templates
  • index.html
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>首页title>
    
    <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
    <link th:href="@{/css/qinstyle.css}" rel="stylesheet">
head>
<body>


<div class="ui container">

    <div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
        <div class="ui secondary menu">
            <a class="item"  th:href="@{/index}">首页a>

            
            <div class="right menu">
                
                <a class="item" th:href="@{/toLogin}">
                    <i class="address card icon">i> 登录
                a>
              	
                <a class="item" th:href="@{/logout}">
                    <i class="sign-out icon">i> 注销
                a>
            div>
        div>
    div>

    <div class="ui segment" style="text-align: center">
        <h3>Spring Security Studyh3>
    div>

    <div>
        <br>
        <div class="ui three column stackable grid">
            <div class="column">
                <div class="ui raised segment">
                    <div class

你可能感兴趣的:(Java,SSM,spring,spring,boot,java)