【SpringSecurity】入门

SpringSecurity简介

SpringSecurity demo搭建

1、创建maven项目并添加springbootweb依赖


<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>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.0version>
        <relativePath/> 
    parent>
    <groupId>com.cxmgroupId>
    <artifactId>spring-security-demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-security-demoname>
    <description>spring-security-demodescription>
    <properties>
        <java.version>11java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
    <repositories>
        <repository>
            <id>maven-aliid>
            <url>http://maven.aliyun.com/nexus/content/groups/public//url>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
                <updatePolicy>alwaysupdatePolicy>
                <checksumPolicy>failchecksumPolicy>
            snapshots>
        repository>
    repositories>
project>

2、引入SpringSecurity的依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-securityartifactId>
dependency>

3、补全启动项,新建测试Controller
创建一个controller用于测试

@RestController
@RequestMapping()
public class controller {
    @GetMapping("hello")
    public String sayHello(){
        return "hello";
    }
}

4、启动项目访问hello
【SpringSecurity】入门_第1张图片

我们发现访问测试接口,直接跳转到了登陆界面,这说明spring security 的默认配置生效了

输入用户名和密码登录:默认配置的用户名为user,密码在项目的控制台可以查看

在这里插入图片描述

登录后,就可以直接访问到我们的测试接口

【SpringSecurity】入门_第2张图片

到这里就成功的引入了Springsecurity。

你可能感兴趣的:(项目组件,java,maven,spring,boot)