springboot高级篇(认证,授权)
认证,授权
- 1,项目
- 1.1登陆
- 1.2,注销
- 1.3,认证授权
- 1.4,记住我功能
认证:证明你是谁,携带用户名和密码。系统查验是你这个人的过程。
授权:认证以后你能干什么,访问资源的权限
1,项目
1.1登陆
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
编写配置类
package com.atguigu.security.config;import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {@Overridepublic void configure(HttpSecurity http) throws Exception {//定制请求规则,访问首页允许所有人可以访问http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");//开启登录功能http.formLogin();}@Overridepublic void configure(AuthenticationManagerBuilder auth)throws Exception{// auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("ljs").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3");// auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("lisi").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP3");// auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2");auth.inMemoryAuthentication().withUser("zhangsan").password("123456").roles("VIP1","VIP2").and().withUser("lisi").password("123456").roles("VIP1","VIP3").and().withUser("wangwu").password("123456").roles("VIP2","VIP3");}}
1.2,注销
config里
//开启注销功能(包括跳转规则)
http.logout().logoutSuccessUrl("/");
welcome.html
请登录下写
<form th:action="@{/logout}" method="post"><input type="submit" value="注销"/></form>
启动项目可以使用注销按钮了
1.3,认证授权
1,用户未登陆,显示登陆,用户登陆显示注销
2,权限控制,对应权限展示对应页面内容
pom.xml
<properties><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version><thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version></properties><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity4</artifactId></dependency>
页面
welcome.html改命名空间,使用模板引擎
xmlns:sec=“http://www.thymeleaf.org/thymeleaf-extras-springsecurity4”
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<!--权限登录的修改处-->
<!--没有登录状态的-->
<div sec:authorize="!isAuthenticated()"><h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录</a></h2>
</div>
<!--有登录状态的-->
<div sec:authorize="isAuthenticated()"><form th:action="@{/logout}" method="post"><input type="submit" value="注销"/></form>
</div>
<hr>
<!--不同角色不同权限的修改-->
<div sec:authorize="hasRole('VIP1')"><h3>普通武功秘籍</h3><ul><li><a th:href="@{/level1/1}">罗汉拳</a></li><li><a th:href="@{/level1/2}">武当长拳</a></li><li><a th:href="@{/level1/3}">全真剑法</a></li></ul>
</div>
<div sec:authorize="hasRole('VIP2')"><h3>高级武功秘籍</h3><ul><li><a th:href="@{/level2/1}">太极拳</a></li><li><a th:href="@{/level2/2}">七伤拳</a></li><li><a th:href="@{/level2/3}">梯云纵</a></li></ul>
</div><div sec:authorize="hasRole('VIP3')"><h3>绝世武功秘籍</h3><ul><li><a th:href="@{/level3/1}">葵花宝典</a></li><li><a th:href="@{/level3/2}">龟派气功</a></li><li><a th:href="@{/level3/3}">独孤九剑</a></li></ul>
</div>
重启项目实现授权功能基本完成
1.4,记住我功能
登陆一次,下次重启浏览器还是登陆状态
config
//开启记住我功能http.rememberMe();
重启项目,点登陆的时候发现多了一个单选框。选择它就好
cookie默认保存14天
//运行流程:
1,登陆成功以后,将cookie发给浏览器保存,以后访问这个页面带上这个cookie,通过检查就免登陆
2,点击注销会删除cookie
之前登陆都进的是系统登陆页面。怎么进定制登陆页面
可以自己写一个页面
修改处
1,
//开启登录功能http.formLogin().loginPage("/userlogin");
2,
welcome.html
<a th:href="@{/userlogin}">
请登录
3,修改login.html
<form th:action="@{/userlogin}" method="post">
自定义尽量改详细彻底点
config
//开启登录功能http.formLogin().loginPage("/userlogin").usernameParameter("user").passwordParameter("pwd").loginPage("/userlogin");//如果没有权限就回到登录页面
login.html
用户名:<input name="user"/><br/>密码:<input name="pwd"><br/>
自定义登陆页加记住我功能
login.html
加一个
<input type="checkbox" name="remember"/>记住我<br/>
修改config
http.rememberMe().rememberMeParameter("remember");
springboot高级篇(认证,授权)相关推荐
- 【SpringBoot高级篇】springboot实现上传docdocx文件格式转html在线预览v2.0
[SpringBoot高级篇]springboot实现上传doc&docx文件格式转html在线预览v2.0 pom 上传路径工具类 SpringMvc虚拟路径映射 doc转html工具类 d ...
- 【SpringBoot高级篇】SpringBoot集成Elasticsearch搜索引擎
[SpringBoot高级篇]SpringBoot集成Elasticsearch搜索引擎 1. 什么是Elasticsearch? 2. 安装并运行Elasticsearch 2.1 拉取镜像 2.2 ...
- 【SpringBoot高级篇】SpringBoot集成jasypt 配置脱敏和数据脱敏
[SpringBoot高级篇]SpringBoot集成jasypt数据脱敏 配置脱敏 使用场景 配置脱敏实践 数据脱敏 pom yml EncryptMethod EncryptField Encry ...
- 【SpringBoot高级篇】SpringBoot集成MongDB数据库
[SpringBoot高级篇]SpringBoot集成MongDB数据库 MongoDB是什么 ? 主要特点 MongoDB管理 工具 MongoDB 基本概念 数据库 文档(Document) 集合 ...
- SpringBoot高级篇MongoDB之修改基本使用姿势
原文: 190218-SpringBoot高级篇MongoDB之修改基本使用姿势 本篇依然是MongoDB curd中的一篇,主要介绍document的更新,主要内容如下 常见类型成员的修改 数组类型 ...
- SpringBoot高级篇JdbcTemplate之数据查询上篇
前面一篇介绍如何使用JdbcTemplate实现插入数据,接下来进入实际业务中,最常见的查询篇.由于查询的姿势实在太多,对内容进行了拆分,本篇主要介绍几个基本的使用姿势 queryForMap que ...
- springboot高级篇及springboot1.5转springboot2.17所遇到的坑
SpringBoot的高级教程 一.SpringBoot缓存 缓存的场景 临时性数据存储[校验码] 避免频繁因为相同的内容查询数据库[查询的信息] 1.JSR107缓存规范 用的比较少 Java Ca ...
- SpringBoot整合Shiro(认证+授权)
文章目录 Shiro框架简介 Spring Boot整合shiro环境搭建 Shiro实现登录拦截 Shiro框架简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份认证丶授权丶 ...
- [认证授权] 5.OIDC(OpenId Connect)身份认证授权(扩展部分)
[认证授权] 5.OIDC(OpenId Connect)身份认证授权(扩展部分) 原文:[认证授权] 5.OIDC(OpenId Connect)身份认证授权(扩展部分) 在上一篇[认证授权] 4. ...
最新文章
- 灵图天行者9 pc版_原神PC预下载现已开启
- ftp定期任务linux,Linux FTP服务器搭建和crontab计划任务制定
- JAVA通信编程(五)——串口通讯的补充说明
- ❤️520要来啦,快去给心仪的她写表白代码趴!(python)❤️
- Python2.7.16安装(Win10)
- java程序包r不存在_java - 从命令行使用Gradle构建时,“程序包R不存在”错误 - 堆栈内存溢出...
- 对+=赋值运算符的认识
- WINDOWS获得当前执行程序路径的办法
- Linux系统下如何使用中文输入法
- FPGA实现cameralink高清相机解码
- 读书笔记 摘自:《亲密关系:通往灵魂的桥梁(张德芬译)》的笔记(作者: 【加】克里斯多福·孟)
- linux免安装mysql_Linux 配置mysql 免安装版。
- MPAndroidChart的BarChart用法
- 如何把Word的默认页面颜色由白色改为绿色
- PHP将word文件转为图片预览
- 逆向破解思路和获取app的代码,脱壳操作(三)
- Visual Paradigm使用技能
- Email邮件发送设置 工具开发整理(网易邮箱、Mailgun为例) 上篇
- 深度学习与“免费”GPU
- Cisco三层交换机配置命令及解释