zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    泛型技巧系列:如何提供类型参数之间的转换
    一些支离破碎的泛型反射技巧
    泛型技巧系列:类型字典和Type Traits
    Excel开发:简化工作表中选定区域的操作。
    趣味程序:打印自己代码的程序
    VBF BETA 1.5 发布了
    .NET 2.0 CER学习笔记
    随笔乱入,开心就好
    Cocos2dx for WindowsPhone:开发一个打地鼠游戏(下)
    跨平台网络游戏趋势和优势
  • 原文地址:https://www.cnblogs.com/Eleven-Liu/p/11142746.html
Copyright © 2011-2022 走看看