• (一)Spring Security 入门体验


    一、前言

    Spring Security 和 Apache Shiro 都是安全框架,为Java应用程序提供身份认证和授权。

    • 二者区别
    1. Spring Security:量级安全框架

    2. Apache Shiro:量级安全框架

    Spring Security:

    Spring Security 的前身是Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架。

    Spring Security  为基于J2EE企业应用软件提供了全面安全服务。特别是使用领先的J2EE解决方案-Spring框架开发的企业软件项目。人们使用Spring Security有很多种原因,不过通常吸引他们的是在J2EE  Servlet规范或EJB规范中找不到典型企业应用场景的解决方案。

    二、SpringBoot集成Spring Security入门体验

    基本环境 : springboot 2.4.1  JDK 11

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>cn.com.sercurity</groupId>
        <artifactId>cyy</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>cyy</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>11</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <spring-boot.version>2.4.1</spring-boot.version>
        </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-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
    
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.4.1</version>
                    <configuration>
                        <mainClass>cn.com.sercurity.cyy.CyyApplication</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <id>repackage</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>

    2、新建一个controller测试访问

    @RestController
    public class IndexController {
    
        @GetMapping("/index")
        public String index() {
            return "Hello World ~";
        }
    }

    3、创建login.html(注:在resources中新建static文件夹中存放)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Login</title>
    </head>
    <body>
    <h1>Spring Security</h1>
    <form method="post" action="" onsubmit="return false">
        <div>
            用户名:<input type="text" name="username" id="username">
        </div>
        <div>
            密码:<input type="password" name="password" id="password">
        </div>
        <div>
            <!--        <label><input type="checkbox" name="remember-me" id="remember-me"/>自动登录</label>-->
            <button onclick="login()">登陆</button>
        </div>
    </form>
    </body>
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        function login() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            // var rememberMe = document.getElementById("remember-me").value;
            $.ajax({
                async: false,
                type: "POST",
                dataType: "json",
                url: '/login',
                contentType: "application/json",
                data: JSON.stringify({
                    "username": username,
                    "password": password
                    // "remember-me": rememberMe
                }),
                success: function (result) {
                    console.log(result)
                    if (result.code == 200) {
                        alert("登陆成功");
                        window.location.href = "../home.html";
                    } else {
                        alert(result.message)
                    }
                }
            });
        }
    </script>
    </html>

    4、运行项目访问 http://127.0.0.1:8080/index

    温馨小提示:在不进行任何配置的情况下,Spring Security 给出的默认用户名为user 密码则是项目在启动运行时随机生成的一串字符串,会打印在控制台,如下图:

     当我们访问index首页的时候,系统会默认跳转到login页面进行登录认证

     认证成功之后才会跳转到我们的index页面

  • 相关阅读:
    预备作业2
    我期待的师生关系
    20162317袁逸灏 第四周实验报告
    《程序设计与数据结构》第4周学习总结
    《程序设计与数据结构》第3周学习总结
    20162317 《程序设计与数据结构》第2周学习总结
    学号20162317《程序设计与数据结构》第1周学习总结
    预备作业03
    预备作业02
    20162317袁逸灏
  • 原文地址:https://www.cnblogs.com/changyuyao/p/14206047.html
Copyright © 2020-2023  润新知