89.【SpringBoot-02】(四)

简介: 89.【SpringBoot-02】

8.在Resource资源目录下配置UserMapper.xml的实现。application.yaml

  • resource目录下切换文档需要是 ‘/’ 不是 ‘.’
  • classpath: 指的是我们设置的UserMapper.xml
spring:
  datasource:
      username: root
      password: 121788
      url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useonicode=true&characterEncoding=utf-8
      driver-class-name: com.mysql.jdbc.Driver
# 整合Mybatis 就相当于Mybatis的核心包一样
mybatis:
      type-aliases-package: com.jsxs.pojo.*
      mapper-locations: classpath:mybatis/mapper/*.xml

设置Controller层,不设置业务层了。我们只进行测试数据

package com.jsxs.controller;
import com.jsxs.mapper.UserMapper;
import com.jsxs.pojo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/jsxs")
public class UserController {
    @Resource
    private UserMapper userMapper;
//    查询全部的信息
    @RequestMapping("/all")
    public List<User> queryList(){
        List<User> users = userMapper.queryList();
        return users;
    }
//   通过id进行查询数据
    @RequestMapping("/id/{id}")
    public User queryById(@PathVariable("id") int id){
        User user = userMapper.UserById(id);
        return user;
    }
//  http://localhost:8080/jsxs/add?id=8&name=sdsds&pwd=2711111
    @RequestMapping("/add")
    public String addUser(User user){
        int i = userMapper.addUser(user);
        if (i>0){
            return "恭喜您,数据添加成功";
        }else {
            return "很抱歉,数据添加失败";
        }
    }
// 进行数据的修改
    @RequestMapping("/update")
    public String updateUser(User user){
        int i = userMapper.updateUser(user);
        if (i>0){
            return "恭喜您,数据更新成功";
        }else {
            return "很抱歉,数据更新失败";
        }
    }
//
    @RequestMapping("/delete/{id}")
    public String deleteUser(@PathVariable("id") int id){
        int i = userMapper.deleteUserById(id);
        if (i>0){
            return "恭喜您,数据删除成功";
        }else {
            return "很抱歉,数据删除失败";
        }
    }
}

(十五)、SpringSecurity (安全)

一个安全的框架,其实通过过滤器和拦截器也可以实现

首先我们看下它的官网介绍:Spring Security官网地址

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。它实际上是保护基于spring的应用程序的标准。

Spring Security是一个框架,侧重于为Java应用程序提供身份验证和授权。与所有Spring项目一样,Spring安全性的真正强大之处在于它可以轻松地扩展以满足定制需求

在用户认证方面,Spring Security 框架支持主流的认证方式,包括 HTTP 基本认证、HTTP 表单验证、HTTP 摘要认证、OpenID 和 LDAP 等。在用户授权方面,Spring Security 提供了基于角色的访问控制和访问控制列表(Access Control List,ACL),可以对应用中的领域对象进行细粒度的控制。

1.认识SpringSecurity

Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理!

记住几个类:

  • WebSecurityConfigurerAdapter:自定义Security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity:开启WebSecurity模式
    Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。
    认证:就是给某个人赋予什么样的角色(名片)
    授权: 就是某个人(角色/名片)能行驶什么样的权力

“认证”(Authentication)

身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。

身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。

“授权” (Authorization)

授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。

这个概念是通用的,而不是只在Spring Security 中存在。

2.实战使用

(1).搭建环境

1.我们首先导入静态资源

2.引入thymeleaf依赖需求

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.7.7</version>

3.设置路由转向Controller

  • 这里我们学习到一个新技术,就是我们可以使用ReusltFul风格对其页面数据进行跳转
@RequestMapping("/level1/{id}")
    public String toOne(@PathVariable("id") int id) {
        return "views/level1/"+id;
    }
package com.jsxs.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping({"/jsxs","/"})
public class RouterController {
    @RequestMapping({"/", "/index"})
    public String index() {
        return "index";
    }
    @RequestMapping("/toLogin")
    public String toLogin() {
        return "views/login";
    }
    //    vip1
//    这里我们使用几千前端传参实现对页面跳转的控制
    @RequestMapping("/level1/{id}")
    public String toOne(@PathVariable("id") int id) {
        return "views/level1/"+id;
    }
    //    这里我们使用几千前端传参实现对页面跳转的控制
    @RequestMapping("/level2/{id}")
    public String toTwo(@PathVariable("id") int id) {
        return "views/level2/"+id;
    }
    //    这里我们使用几千前端传参实现对页面跳转的控制
    @RequestMapping("/level3/{id}")
    public String toThree(@PathVariable("id") int id) {
        return "views/level3/"+id;
    }
}

(2).用户认证和授权

1.引入 SpringSecurity 依赖

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

2.新建一个配置文件,并且继承 WebSecurityConfigurerAdapter,并且要添加@EnableWebSecurity注解 (授权)

授权

package com.jsxs.config;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//    链式编程
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        首页所有人可以访问,但是功能页只有对应有权限的人才能访问
        http.authorizeRequests()
                .antMatchers("/").permitAll()
//                这里意思就是说: 在level1下的所有页面只有vip1才能进行访问
                .antMatchers("/level1/**").hasRole("vip1")
//                这里意思就是说: 在level2下的所有页面只有vip2才能进行访问
                .antMatchers("/level2/**").hasRole("vip2")
//                这里意思就是说: 在level3下的所有页面只有vip3才能进行访问
                .antMatchers("/level3/**").hasRole("vip3");
        super.configure(http);
    }
}

3.我们进行测试登入 如果我们没有权限就进入了登入页面。(系统自带非自写)

账户是: user 密码: 会显示在控制台

//        没有权限默认跳到登入页面
             http.formLogin();

4.输入账户密码正确无误后 会进入我们所需的页面

相关文章
|
JavaScript 前端开发 安全
JavaScript中的循环控制:while、do-while与for详解
【4月更文挑战第7天】本文探讨JavaScript的三种主要循环结构:while、do-while和for。while循环在满足条件时执行代码块,注意避免无限循环;do-while循环至少执行一次,适合先执行后判断的场景;for循环结合初始化、条件和迭代,适合遍历。理解每种循环的特点和适用场景,结合编程技巧,如使用break和continue,选择合适的循环方式,能提升代码效率和可读性。记得关注循环性能和避免不必要的计算。
306 0
|
存储 缓存 关系型数据库
MySQL的体系架构
MySQL的体系架构
|
算法 安全 Java
Java多线程基础-12:详解CAS算法
CAS(Compare and Swap)算法是一种无锁同步原语,用于在多线程环境中更新内存位置的值。
320 0
|
8月前
|
存储 NoSQL Linux
微服务2——MongoDB单机部署4——Linux系统中的安装启动和连接
本节主要介绍了在Linux系统中安装、启动和连接MongoDB的详细步骤。首先从官网下载MongoDB压缩包并解压至指定目录,接着创建数据和日志存储目录,并配置`mongod.conf`文件以设定日志路径、数据存储路径及绑定IP等参数。之后通过配置文件启动MongoDB服务,并使用`mongo`命令或Compass工具进行连接测试。此外,还提供了防火墙配置建议以及服务停止的两种方法:快速关闭(直接杀死进程)和标准关闭(通过客户端命令安全关闭)。最后补充了数据损坏时的修复操作,确保数据库的稳定运行。
517 0
|
存储 SQL 缓存
MySQL存储引擎如何完成一条更新语句的执行!
MySQL存储引擎如何完成一条更新语句的执行!
127 0
MySQL存储引擎如何完成一条更新语句的执行!
|
存储 Java
Java 五种内部类演示及底层原理详解
Java 五种内部类演示及底层原理详解
104 0
|
前端开发 JavaScript Java
计算机Java项目|视频网站系统
计算机Java项目|视频网站系统
106 0
|
索引 Python Go
【python学习】字符串详解,面试必问公司的问题
【python学习】字符串详解,面试必问公司的问题
|
存储 数据可视化 Java
SpringBoot整合定时器:定时任务不再硬编码,动态定时刷起来
前言 传统定时器是硬编码。但是有的时候业务上需要不断地调整 问题描述 我们开发了一个定闹钟的功能。这个功能肯定是定时器开发。但是这就存在一个问题这个定时是动态的。那么我们如何实现呢?请接着看
|
应用服务中间件 nginx
Nginx防盗链配置
Nginx防盗链配置