SpringBoot集成Spring Security(授权与认证)
⒈添加starter依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-thymeleaf</artifactId>
</dependency> <!--添加Thymeleaf Spring Security依赖-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
⒉使用配置类定义授权与定义规则
package cn.coreqi.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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; //@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { //定义授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
//定制请求授权规则
http.authorizeRequests()
.antMatchers("/css/**","/js/**","/fonts/**","index").permitAll() //不拦截,直接访问
.antMatchers("/vip1/**").hasRole("VIP1")
.antMatchers("/vip2/**").hasRole("VIP2")
.antMatchers("/vip3/**").hasRole("VIP3");
//开启登陆功能(自动配置)
//如果没有登陆就会来到/login(自动生成)登陆页面
//如果登陆失败就会重定向到/login?error
//默认post形式的/login代表处理登陆
http.formLogin().loginPage("/userLogin").failureUrl("/login-error");
//开启自动配置的注销功能
//访问/logout表示用户注销,清空session
//注销成功会返回/login?logout页面
//logoutSuccessUrl()设置注销成功后跳转的页面地址
http.logout().logoutSuccessUrl("/");
//开启记住我功能
//登陆成功以后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登陆
//点击注销会删除cookie
http.rememberMe();
} //定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//jdbcAuthentication() 在JDBC中查找用户
//inMemoryAuthentication() 在内存中查找用户 auth.inMemoryAuthentication().withUser("fanqi").password("admin").roles("VIP1","VIP2","VIP3")
.and()
.withUser("zhangsan").password("123456").roles("VIP1");
}
}
⒊编写控制器类(略)
⒋编写相关页面
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<div sec:authorize="isAuthenticated()">
<p>用户已登录</p>
<p>登录的用户名为:<span sec:authentication="name"></span></p>
<p>用户角色为:<span sec:authentication="principal.authorities"></span></p>
</div>
<div sec:authorize="isAnonymous()">
<p>用户未登录</p>
</div>
</body>
</html>
SpringBoot集成Spring Security(授权与认证)的更多相关文章
- Springboot集成Spring Security实现JWT认证
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 Spring Security作为成熟且强大的安全框架,得到许多大厂的青睐.而作为前后端分离的SSO方案,JWT ...
- Springboot WebFlux集成Spring Security实现JWT认证
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 在之前的文章<Springboot集成Spring Security实现JWT认证>讲解了如何在传统 ...
- SpringBoot集成Spring Security(7)——认证流程
文章目录 一.认证流程 二.多个请求共享认证信息 三.获取用户认证信息 在前面的六章中,介绍了 Spring Security 的基础使用,在继续深入向下的学习前,有必要理解清楚 Spring Sec ...
- SpringBoot集成Spring Security入门体验
一.前言 Spring Security 和 Apache Shiro 都是安全框架,为Java应用程序提供身份认证和授权. 二者区别 Spring Security:重量级安全框架 Apache S ...
- SpringBoot集成Spring Security(6)——登录管理
文章目录 一.自定义认证成功.失败处理 1.1 CustomAuthenticationSuccessHandler 1.2 CustomAuthenticationFailureHandler 1. ...
- SpringBoot集成Spring Security(4)——自定义表单登录
通过前面三篇文章,你应该大致了解了 Spring Security 的流程.你应该发现了,真正的 login 请求是由 Spring Security 帮我们处理的,那么我们如何实现自定义表单登录呢, ...
- SpringBoot集成Spring Security(2)——自动登录
在上一章:SpringBoot集成Spring Security(1)——入门程序中,我们实现了入门程序,本篇为该程序加上自动登录的功能. 文章目录 一.修改login.html二.两种实现方式 2. ...
- SpringBoot集成Spring Security(5)——权限控制
在第一篇中,我们说过,用户<–>角色<–>权限三层中,暂时不考虑权限,在这一篇,是时候把它完成了. 为了方便演示,这里的权限只是对角色赋予权限,也就是说同一个角色的用户,权限是 ...
- SpringBoot集成Spring Security
1.Spring Security介绍 Spring security,是一个强大的和高度可定制的身份验证和访问控制框架.它是确保基于Spring的应用程序的标准 --来自官方参考手册 Spring ...
- SpringBoot 集成Spring security
Spring security作为一种安全框架,使用简单,能够很轻松的集成到springboot项目中,下面讲一下如何在SpringBoot中集成Spring Security.使用gradle项目管 ...
随机推荐
- TiKV 源码解析系列文章(三)Prometheus(上)
本文为 TiKV 源码解析系列的第三篇,继续为大家介绍 TiKV 依赖的周边库 rust-prometheus,本篇主要介绍基础知识以及最基本的几个指标的内部工作机制,下篇会介绍一些高级功能的实现原理 ...
- 20 Zabbix 利用Scripts栏目对Hosts远程执行命令
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 20 Zabbix 利用Scripts栏目对Hosts远程执行命令 在Monitoring板块中, ...
- 自学Linux Shell3.4-文件处理命令touch cp mv rm
点击返回 自学Linux命令行与Shell脚本之路 3.4-文件处理命令touch cp mv rm 1. touch命令 一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将 ...
- emwin之2D图形绘制问题
@2018-09-03 [问题] 在 WM_PAINT 消息分支里绘制2D图形可以正常显示,而在外部函数或按钮按下事件的响应消息分支下等处,绘制2D图形则不显示. [解决] 在除消息WM_PAINT分 ...
- 老铁,这年头得玩玩这个:Git基本操作【github】
GitHub创建项目 本地创建项目 1.初始化配置,设置仓库人员的用户名和邮箱地址,这一步必不可少 git config --global user.name "uncleyong" ...
- hive1.1.0安装
0. 说明 已经安装好Hadoop-1.2.1. 安装好mysql. 1.解压安装包 先把安装包下载.然后解压: tar -zxvf apache-hive-1.1.0-bin.tar.gz 2.配置 ...
- 【codevs4829】数字三角形++
题目大意:给定一个数字三角形,求从 (1,1) 到第 N 行的路径经过的权值之和加上该路径上任意一个点的权值之和的最大值. 题解:任意加一条路径上的某个值,可以看成是多了一次选择的权利,即:在每次经过 ...
- 有趣的canvas
最近看了一本canvas的书,里面对canvas的一些基本知识讲的很详细.相比于一个div加点颜色,我臭屁的觉得使用canvas画长方形正方形圆形之类的是大才小用. 下面我放几个canvas还不错的功 ...
- 静态代码块、构造代码块、构造方法优先级(重点)-------java基础总结
package com.mon11.day11; /** * 类说明 : * @author 作者 : chenyanlong * @version 创建时间:2017年11月11日 */ publi ...
- ip更换
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...