SpringSecurity集成JWT实现后端认证授权保姆级教程-环境搭建篇

news2024/9/30 23:32:06

🍁 作者:知识浅谈,CSDN签约讲师,CSDN博客专家,华为云云享专家,阿里云专家博主
📌 擅长领域:全栈工程师、爬虫、ACM算法
💒 公众号:知识浅谈
🔥网站:vip.zsqt.cc

🤞为什么这个东西我要分多篇写,我想说的是这个真不简单🤞
无论是shiro还是SpringSecurity,想要熟悉的使用,我们都要来回的摸索,各种认证和授权类,完全看的一塌糊涂,我不能说讲的很明白,只能说按教程一步步来能带你实现SpringSecurity集成JWT实现后端认证授权。

正菜来了🛴🛴🛴

🎈肯定先创建项目

直接springboot创建
在这里插入图片描述

springboot选用2.7.6版本

在这里插入图片描述
创建后打开

🎈再来创建各种文件夹和文件

之所以创建文件夹,下边要用到
在这里插入图片描述

🎈再来引入各种依赖

pom.xml
如果自己项目路径和我的不一样:只需要引入下边的properties 和dependencies
有mysql依赖,redis依赖,security依赖,swagger依赖,相对来说比较全了

<?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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.7.6</spring-boot.version>
        <jwt.verison>4.3.0</jwt.verison>
        <fastjson.version>2.0.27</fastjson.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>${jwt.verison}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.10.1</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </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>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.demo.DemoApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

🎈安装mysql和redis

  • 安装Mysql(使用默认的3306端口)
    在mysql中创建test数据库
    设置账号:root 密码:password

  • 安装Redis 不用设置密码(生产环境需要设置密码,使用默认的6379端口)

🎈填写配置文件

application.yml

# 应用服务 WEB 访问端口
server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC  # 数据库连接URL
    username: root  # 数据库用户名
    password: password # 数据库密码
    driver-class-name: com.mysql.cj.jdbc.Driver  # MySQL驱动程序类名
    type: com.alibaba.druid.pool.DruidDataSource  # 指定使用Druid连接池
    druid:
      initial-size: 5  # 初始化时建立物理连接的个数
      max-active: 20  # 最大连接池数量
      min-idle: 5  # 最小连接池数量
      max-wait: 60000  # 获取连接时最大等待时间,单位毫秒
      validation-query: SELECT 1 FROM DUAL  # 测试连接是否可用的SQL语句
      test-while-idle: true  # 保持连接池活跃地状态
      time-between-eviction-runs-millis: 60000  # 检查空闲连接的时间间隔,单位毫秒
      min-evictable-idle-time-millis: 300000  # 连接在连接池中最小空闲时间,单位毫秒
      filters: stat,wall,log4j2  # 配置Druid监控和防御SQL注入的过滤器
  data:
    redis:
      host: 127.0.0.1
      port: 6379
      database: 0
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml # Mapper.xml文件所在路径
  typeAliasesPackage: com.example.demo.domain # 实体类包名
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 日志输出方式
    map-underscore-to-camel-case: true # 开启驼峰命名规则映射

🎈最后让项目跑起来

在这里插入图片描述

在这里插入图片描述
到这里项目就运行起来了
上图里运行程序之后有一个密码
Using generated security password: 248b72f4-198a-48ef-929e-1f3f2cd51c79
这个是springsecurity自带的密码,当我们访问接口的时候要用到的,默认用户名是user

🍚写个接口测试一下

在controller文件夹创建一个controller

package com.example.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = "测试类")
@RestController
@RequestMapping("/test")
public class TestController {


    @ApiOperation(value = "测试方法")
    @GetMapping("/test")
    public String test(){
      return "test";
    }

}

重新运行程序
访问 http://localhost:8080/test/test
但是直接默认跳到了登录页
http://localhost:8080/login
在这里插入图片描述
输入用户名 user 和后台生成的密码
在这里插入图片描述
到此就创建好后端项目基本框架了。

🍚总结

大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。
Writted By 知识浅谈

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1362925.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

HTML 使用 ruby 给汉字加拼音

使用 ruby 给汉字加拼音 兼容性 使用 ruby 给汉字加拼音 大家有没有遇到过要给汉字头顶上加拼音的需求? 如果有的话, 你是怎么解决的呢? 如果费尽心思, 那么你可能走了很多弯路, 因为 HTML 原生就有这样的标签来帮我们实现类似的需求. <ruby> ruby 本身是「红宝石」…

详解Keras3.0 Callbacks API : TensorBoard(可视化工具)

TensorBoard TensorBoard是TensorFlow提供的可视化工具。需要安装TensorFlow才能使用此回调。此回调记录TensorBoard的事件&#xff0c;包括&#xff1a;度量汇总图、训练图可视化、重量直方图、采样剖面。 keras.callbacks.TensorBoard(log_dir"logs",histogram_…

spring之推断构造方法

目录 源码流程(属于Bean生命周期中的实例化阶段) 1. AbstractAutowireCapableBeanFactory类中的createBeanInstance()方法去创建Bean 实例 2. 根据BeanDefinition加载类得到Class对象 3. 如果BeanDefinition绑定了一个Supplier,那就调用Supplier的get方法得到一个对象并直接…

【java爬虫】首页显示沪深300指数走势图以及前后端整合部署方法

添加首页 本文我们将在首页添加沪深300指数成立以来的整体走势数据展示&#xff0c;最后的效果是这样的 单独贴一张沪深300整体走势图 我感觉从总体上来看指数还是比较稳的&#xff0c;没有特别大的波动&#xff0c;当然&#xff0c;这只是相对而言哈哈。 首先是前端页面 &l…

Vue新手村(一)

目录 1、Vue简介——Vue的特点 2、Vue的第一个页面 3.Vue的简单使用介绍 3.1、{{ }}的使用 3.2、v-text和v-html 3.2.1、v-text和{{ }}的区别 3.2.2、v-html和v-text的区别 3.3、v-on【事件绑定】 3.3.1、绑定事件的语法 3.3.2、语法简化 3.3.3、传参 3.4、v-show和…

组合数据(Python实现)

一、主要目的&#xff1a; 1&#xff0e;熟悉组合数据的类型。 2&#xff0e;掌握列表、元组、字典、集合等组合数据的创建、访问方法。 3&#xff0e;掌握组合数据推导式的使用方法 4&#xff0e;熟悉组合数据的常见应用。 二、主要内容和结果展示&#xff1a; 1. 使用两…

二手买卖、废品回收小程序 在app.json中声明permission scope.userLocation字段 教程说明

处理二手买卖、废品回收小程序 在app.json中声明permission scope.userLocation字段 教程说明 sitemapLocation 指明 sitemap.json 的位置&#xff1b;默认为 ‘sitemap.json’ 即在 app.json 同级目录下名字的 sitemap.json 文件 找到app.json这个文件 把这段代码加进去&…

每日一题——LeetCode997

方法一 个人方法&#xff1a; 这题的意思就是1-n里面找到一个数&#xff0c;它不指向任何数&#xff0c;其他数都要指向它 找到没有指向任何数的那个idx&#xff0c;如果不存在这样的数那么就返回-1如果找到了这样的数&#xff0c;还要继续判断其它的所有数是否都指向它&…

斯坦福家用机器人开源项目

模仿学习在机器人技术领域取得了显著进展&#xff0c;它通过学习人类的演示来完成任务&#xff0c;并展示出了令人印象深刻的表现。然而&#xff0c;目前的研究大多集中在桌面操作上&#xff0c;缺乏实现通用移动性和灵活性所需的关键要素。 在这项研究中&#xff0c;开源项目…

chatglm部署问题

问题&#xff1a; ModuleNotFoundError: No module named ‘transformers_modules.’ 解决方法&#xff1a; 卸载重装 pip uninstall transformerspip install transformers -i https://mirror.baidu.com/pypi/simple

PySimpleGUI图形界面实例|PDF表格转换Excel文件

目录 实例要求 原始文件 输出文件 运行界面 完整代码 代码分析 遍历表格 布局界面 控件简介 写入表格 事件循环 实例要求 使用PySimpleGUI做一个把单位考勤系统导出的pdf文件合并输出Excel的应用&#xff0c;故事出自&#xff1a; https://hannyang.blog.csdn.net…

应用OpenCV绘制箭头

绘制箭头函数 方法&#xff1a;函数cv2.arrowedLine( ) 语法格式&#xff1a;cv2.arrowedLine(img, pt1, pt2, color[, thickness[, line_type[, shift[, tipLength]]]]) 参数说明&#xff1a; img&#xff1a;要画的直线所在的图像&#xff0c;也称为画布。。 pt1&#x…

Python从入门到网络爬虫(MySQL链接)

前言 在实际数据分析和建模过程中&#xff0c;我们通常需要从数据库中读取数据&#xff0c;并将其转化为 Pandas dataframe 对象进行进一步处理。而 MySQL 数据库是最常用的关系型数据库之一&#xff0c;因此在 Python 中如何连接 MySQL 数据库并查询数据成为了一个重要的问题…

【数据库】聊聊常见的索引优化-上

数据库对于现有互联网应用来说&#xff0c;其实是非常重要的后端存储组件&#xff0c;而大多数系统故障都是由于存储所导致的&#xff0c;而数据库是重中之重&#xff0c;所以为了比较好掌握SQL的基本优化手段&#xff0c;打算用两篇文章从基本的联合索引优化、group by/order …

Transformer-MM-Explainability

two modalities are separated by the [SEP] token&#xff0c;the numbers in each attention module represent the Eq. number. E h _h h​ is the mean&#xff0c; ∇ \nabla ∇A : ∂ y t ∂ A {∂y_t}\over∂A ∂A∂yt​​for y t y_t yt​ which is the model’s out…

动态编译 - Dynamically Compile and Load External Java Classes

文章目录 概述Code 概述 动态编译和加载外部Java类的核心流程可以概括为以下几个步骤&#xff1a; 读取源代码: 首先&#xff0c;需要获取到外部的Java源代码。这通常是通过读取文件、网络资源或者数据库中的源代码字符串来实现的。编译源代码: 接下来&#xff0c;需要使用Ja…

Oracle VM VirtualBox xx needs the Micrsoft Visual C++ 2019错误

错误展示 解决方法 重修安装 Visual C 文件 1、前往官网 C 中 Windows 编程概述 | Microsoft Learn 2、找到对应的包 左边导航栏依次选择&#xff1a; 部署本机桌面应用程序-----重新分发Visual C 文件-----最新受支持的Visual C可再发型程序包下载 根据自己电脑系统进行选…

数据结构:树详解

创建二叉树 给出了完整的先序遍历序列&#xff0c;子树为空用’#’表示&#xff0c;所以这样我们在通过先序遍历序列创建二叉树时我们直到先序遍历序列是先进行根结点&#xff0c;然后左子树最后右子树的顺序进行遍历的&#xff0c;所以对于完整的先序遍历序列我们可以直到先序…

指定日期D的年份和月份返回日期D所在的月份:(1)第一天是星期几?(2)该月共有多少天?calendar.monthrange(year,month)

【小白从小学Python、C、Java】 【计算机等考500强证书考研】 【Python-数据分析】 指定日期D的年份和月份 返回日期D所在的月份&#xff1a; &#xff08;1&#xff09;第一天是星期几&#xff1f; &#xff08;2&#xff09;该月共有多少天&#xff1f; calendar.monthrange(…