关键词:
Spring Security
简介
spring security的核心功能为认证(Authentication),授权(Authorization),即认证用户是否能访问该系统,和授权用户可以在系统中进行哪些操作。
引入spring security组件
在 pom.xml 中加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
验证组件是否起到作用,现在不更改框架内的任何内容,启动项目,浏览器中依旧输入 http://localhost:8080 ,可看到如下界面,之前可以直接进入spring boot的初始界面,现在已经看不见了,spring security 导入后默认已经开启了验证,必须先登录验证通过后才能访问。
如果代码中不做任何设置,默认的账户是 user,默认的密码随着项目的启动,会打印在控制台中。
输入账号密码,即可进入默认的初始界面。
代码实战
为了最快最简单最直接的认识这个组件,直接把用户密码写入内存中,项目启动即存在,避免还有建表,实体类,数据库操作等与之无关的内容。命名使用最为简单粗暴的方式,排除一切干扰,用最少的精力掌握该组件的使用。
新增代码目录
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
SPRING BOOT !!!
</body>
</html>
error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
错误
</body>
</html>
UserController
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("/addUser")
@ResponseBody
String addUser() {
return "这是添加用户!!!";
}
@RequestMapping("/deleteUser")
@ResponseBody
String deleteUser() {
return "这是删除用户!!!";
}
@RequestMapping("/updateUser")
@ResponseBody
String updateUser() {
return "这是修改用户!!!";
}
@RequestMapping("/findAllUsers")
@ResponseBody
String findAllUsers() {
return "这是查询用户!!!";
}
}
UserSecurityConfig
package com.example.config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
//注解开启 Spring Security 安全认证与授权
@EnableWebSecurity
public class UserSecurityConfig extends WebSecurityConfigurerAdapter {
//用户认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//内存里面放着
auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
//添加用户,密码,角色
.withUser("zs").password("123456").roles("AAA")
//链式编程
.and()
.withUser("ls").password("123456").roles("BBB")
.and()
.withUser("ww").password("123456").roles("CCC", "primary")
.and()
.withUser("zl").password("123456").roles("primary");
}
//用户授权
@Override
protected void configure(HttpSecurity http) throws Exception {
/**
* permitAll():允许一切用户访问
* hasRole():url请求允许访问的角色
* hasAnyRole() : url请求允许访问的多个角色
* access():允许访问的角色,permitAll、hasRole、hasAnyRole 底层都是调用 access 方法
* access("permitAll") 等价于 permitAll()
*/
http.authorizeRequests().antMatchers("/").permitAll(); // "/":应用首页所以用户都可以访问
http.authorizeRequests()
.antMatchers("/user/addUser").hasRole("AAA") // 首斜杠"/"表示应用上下文,/user/addUser 请求允许 AAA 角色访问
.antMatchers("/user/deleteUser/**").hasAnyRole("AAA", "BBB") //"/user/deleteUser/**"允许 "AAA", "BBB" 角色访问,/**匹配任意
.antMatchers("/user/updateUser").hasAnyRole("AAA", "BBB", "CCC")//除了这种链式编程,也可以分开写
.antMatchers("/user/findAllUsers").access("permitAll");
http.authorizeRequests().anyRequest().authenticated();
/**
* formLogin:指定支持基于表单的身份验证
* 当用户没有登录、没有权限时就会自动跳转到登录页面(默认 /login)
* 当登录失败时,默认跳转到 /error
* 登录成功时会放行
*/
http.formLogin();
}
}
MyPasswordEncoder
package com.example.config;
import org.springframework.security.crypto.password.PasswordEncoder;
//密码编码,Spring Security 高版本必须进行密码编码,否则报错
public class MyPasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence charSequence) {
return charSequence.toString();
}
@Override
public boolean matches(CharSequence charSequence, String s) {
return s.equals(charSequence.toString());
}
}
亲测效果是
以用户名 zs 登录(其角色权限为AAA),可以进入系统,浏览器输入地址可以访问, localhost:8080,localhost:8080/user/addUser,localhost:8080/user/deleteUser,localhost:8080/user/updateUser,localhost:8080/user/findAllUsers
以用户名 ls 登录(其角色权限为BBB),可以进入系统,浏览器输入地址可以访问, localhost:8080,localhost:8080/user/deleteUser,localhost:8080/user/updateUser,localhost:8080/user/findAllUsers
以用户名 ww 登录(其角色权限为CCC),可以进入系统,浏览器输入地址可以访问, localhost:8080,localhost:8080/user/deleteUser,localhost:8080/user/updateUser,localhost:8080/user/findAllUsers
以用户名 zl 登录(其角色权限为CCC),可以进入系统,浏览器输入地址可以访问, localhost:8080,localhost:8080/user/updateUser,localhost:8080/user/findAllUsers
以用户名 admin 登录,不可以进入系统,因为系统中还没有该用户。
springboot学习——springboot快速整合redis
Redis缓存@[toc]简介redis是一个高性能的key-value数据库优势性能强,适合高度的读写操作(读的速度是110000次/s,写的速度是81000次/s)。支持较为丰富的数据类型(如二进制的Strings,Lists,Hashes,Sets,OrderedSets)一定的事物能力(要么执行... 查看详情
客快物流大数据项目(一百零九):springboot概述
文章目录SpringBoot概述一、什么是SpringBoot二、为什么要学习SpringBoot三、SpringBoot的特点SpringBoot概述一、什么是SpringBootSpringBoot是Spring项目中的一个子工程,与我们所熟知的Spring-framework同属于spring的产品:首页SpringBoot简介... 查看详情
springboot2.x快速构建和配置
这篇文章旨在快速了解springboot,能快速进入学习微服务而写的.基本相关的涉及都有所介绍但是都不深入,所以分类放在了springcloud里面了,现在开启了springboot的深入学习,后面有每个章节对springboot的深入介绍,现在也移到了springboot分... 查看详情
springboot学习——springboot简介
最近工作中需要使用到SpringBoot,但是以前工作中没有用到过SpringBoot,所以需要学习下SpringBoot。本系列笔记是笔者学习SpringBoot的笔记,有错误和不足之处,请不吝指教。 话不多说,直接进入... 查看详情
springboot学习笔记——thymeleaf(代码片段)
前置知识:SpringBoot学习笔记——SpringBoot简介与HelloWordSpringBoot学习笔记——源码初步解析SpringBoot学习笔记——配置文件yaml学习SpringBoot学习笔记——JSR303数据校验与多环境切换SpringBoot学习笔记——自动配置原理SpringBoot学习笔记... 查看详情
springboot框架学习4-springboot核心
本节主要:1:springboot为我们提供的starterpom都有哪些2:怎么添加xml配置文件3:日志相关本文是《凯哥陪你学系列-框架学习之springboot框架学习》中第四篇springboot框架学习4-springboot核心(3)声明:本文系凯哥Java(www.kaigejava.com)原创,... 查看详情
推荐一套springboot快速开发项目,干活快到飞起,2022接私活练手必备!
点击关注公众号,Java干货及时送达来源:https://gitee.com/pear-admin/Pear-Admin-Boot这里分享一款超赞的SpringBoot开源项目,基于SpringBoot生态,权限,工作流,快速开发平台。SpringBoot+Security+MyBatis+Thymeleaf+Activit 查看详情
springboot框架学习2-springboot核心
本节主要:1:解析springboot入口和@SpringBootApplication源码详解SpringBootApplication包含:@SpringBootConfiguration@ComponentScan@EnableAutoConfiguration本文是《凯哥陪你学系列-框架学习之springboot框架学习》中第二篇springboot核心(1)声明:本文系凯哥J... 查看详情
学习springboot(代码片段)
文章目录SpringBootSpringBoot的概述SpringBoot的特点SpringBoot的核心功能SpringBoot环境搭建SpringBoot核心配置文件applictaion.propertiesapplication.ymlyml的基本语法SpringBoot的使用SpringBoot注解@SpringBootApplication@SpringBootConf 查看详情
一个比springboot快44倍的java框架
...Java干货及时送达最近看到一个框架,官方号称可以比SpringBoot快44倍,居然这么牛逼,有这么神奇吗?今天带大家来认识一下。这个框架名叫:light-4j。官网简介:Afast,lightweightandmoreproductivemicroservicesframework 查看详情
springboot学习笔记——web开发探究(代码片段)
前置知识:SpringBoot学习笔记——SpringBoot简介与HelloWordSpringBoot学习笔记——源码初步解析SpringBoot学习笔记——配置文件yaml学习SpringBoot学习笔记——JSR303数据校验与多环境切换SpringBoot学习笔记——自动配置原理Web开发探究简介... 查看详情
一个比springboot快44倍的java框架
...处级干部选哪个?最近看到一个框架,官方号称可以比SpringBoot快44倍,居然这么牛逼,有这么神奇吗?今天带大家来认识一下。这个框架名叫:light-4j。官网简介:Afast,lightweightandmoreproductivemicroservicesfra 查看详情
springboot框架学习3-springboot核心
...nner3:全局配置文件本文是《凯哥陪你学系列-框架学习之springboot框架学习》中第三篇springboot框架学习3-springboot核心(2)声明:本文系凯哥Java(www.kaigejava.com)原创,未经允许,禁止转载!一:怎么手动关闭不需要的配置?在上一篇中,... 查看详情
超详细的springboot学习教程,springboot学习看这篇就够了
超详细的springBoot学习教程,springboot学习看这篇就够了https://blog.csdn.net/lin1214000999/article/details/105468338/ 查看详情
我的第一个springboot程序(springboot学习笔记之二)
第一个springboot程序写在前面:鉴于spring注解以及springMVC的配置有大量细节和知识点,在学习理解之后,我们将直接进入springboot的学习,在后续学习中用到注解及其他相关知识点时会再次理解。要运行起第一个Springboot特别简单,... 查看详情
springboot学习随笔
SpringBoot概述---------------------------------------------------------------------------------********************************-------------------------------------------------------------------- Sprin 查看详情
springboot学习笔记——自动配置原理(代码片段)
前置知识:SpringBoot学习笔记——SpringBoot简介与HelloWordSpringBoot学习笔记——源码初步解析SpringBoot学习笔记——配置文件yaml学习SpringBoot学习笔记——JSR303数据校验与多环境切换自动配置原理配置文件到底能写什么?怎么写?Spring... 查看详情
springboot学习笔记
一、什么是SpringBoot 描述:SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目。大多数SpringBoot项目只需要很少的配置文件。二、SpringBoot核心功能 1、独立运行Spring项目 Springboot可以... 查看详情