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项目管 ...
随机推荐
- 自学Aruba2.1-Aruba Web UI --Dashbord面板介绍
点击返回:自学Aruba之路 自学Aruba2.1-Aruba Web UI --Dashbord面板介绍 本文所有设计的的controller版本信息如下: Aruba7205 V6.4.4.16 ...
- android关闭日志
我们在开发时,经常会输出各种日志来debug代码.但是等到应用发布的apk运行时不希望它输出日志. 关闭输出日志Log.v(),Log.i(),Log.w(),Log.v(),Log.e()等 原理: ...
- 做一个懒COCOS2D-X程序猿(一)停止手打所有cpp文件到android.mk
前言:”懒”在这里当然不是贬义词,而是追求高效,拒绝重复劳动的代名词!做一个懒COCOS2D-X程序猿的系列文章将教会大家在工作中如何偷懒,文章篇幅大多较短,有的甚至只是几行代码,争取把懒发挥到极致! ...
- pacman安装软件包出现损坏
状况 File .pkg.tar.xz is corrupted (invalid or corrupted package (PGP signature)).Do you want to delet ...
- HDU 3338 Kakuro Extension (网络流,最大流)
HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...
- layui 批量上传文件 + 后台 用servlet3.0接收【我】
前台代码: [主要参照layui官方 文件上传示例 https://www.layui.com/demo/upload.html] <!DOCTYPE html> <html> ...
- 中南大学2018年ACM暑期集训前期训练题集(入门题) J : A Simple Problem
毒瘤哇!为什么要用long long 啊!!!这个题没有加法操作啊,为什么会爆int啊!!!! 思路: http://www.cnblogs.com/buerdepepeqi/p/9048130.ht ...
- centos中文字符集,中文日志
# CentOS 7 $ firewall-cmd --zone=public --add-port=80/tcp --permanent # nginx 端口 $ firewall-cmd --zo ...
- linu查看当前目录下文件大小
查看当前目录下文件大小 [root@izm5e33l0ge76tvdj3qtnfz var]# du -sh * .0K adm 190M cache .0K crash 24K db .0K emp ...
- Linux集群部署自定义时间同步服务器(ntpd)
Linux集群部署自定义时间同步服务器(ntpd) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 身为一名运维人员,在搭建集群的时候,第一步需要做的就是同步每个机器的时间,尤其是在 ...