• Spring security 知识笔记【入门】


    一、生成spring boot项目文件

    二、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>
    
        <groupId>com.Eleven</groupId>
        <artifactId>Eleven</artifactId>
        <version>1.0-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.0.M2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <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>
        </dependencies>
    
    </project>

    二、Spring Boot启动文件

    package Eleven;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    @SpringBootApplication
    @ServletComponentScan   //扫描过滤器等servlet、filter注解
    public class ElevenApplication {
        public static void main(String[] args){
            SpringApplication.run(ElevenApplication.class,args);
        }
    }

    三、新建两个controller

    package Eleven.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    
    
    @RestController
    public class AuthenticationTestController {
        @GetMapping("/user")
        public String helloWorld(){
            return "This is a user page!";
        }
    
        @GetMapping("/admin/test/*")
        public String getAdminInfo(){
            return "This is Admin page!";
        }
    
    
    
    }

    四、启动后,访问controller路径,提示需要登录,默认用户user,默认的密码是动态的,已在控制台输出

    五、登录后可显示controller的内容

    六、自定义用户名、密码

    在配置文件(application.properties)中增加以下

    spring.security.user.name = admin
    spring.security.user.password = 123456
  • 相关阅读:
    设置maven阿里云镜像和公司私服并存
    idea favorites bookmarks标签收藏夹数据丢失bug
    C#后期绑定调用COM组件
    SQL Studio 1.0:一款轻便的SQL脚本工具兼容SQL Server、MySQL、Access2007
    SQL Studio 2.0: 新版发布
    SQLHelper用的不爽,试试CmdRunner吧
    Flask + uWSGI+ Linux 指南及避坑
    Flask + Pyinstaller 打包后运行报错 SystemError
    Url重写之UrlRewriter
    .net5 提取压缩包内指定文件内容无需解压
  • 原文地址:https://www.cnblogs.com/Eleven-Liu/p/11142746.html
Copyright © 2020-2023  润新知