SpringBoot Security安装配置及Thymeleaf整合
作者:人间有妖气 发布时间:2023-11-27 16:18:41
标签:Spring,Boot,Security,安装,配置
功能:解决web站点的登录,权限验证,授权等功能
优点:在不影响站点业务代码,可以权限的授权与验证横切到业务中
1、要添加的依赖
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--security 和 thymeleaf 整合包-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、Security 下授权与验证的简单配置(Security下有登录,注销,记住我等功能,可以快速集成到自己的login页上)
Tis:如果template页中使用了 Frame页,默认是不能访问的,需要添加 http.headers().frameOptions().sameOrigin();
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
//请求授权的规则
http.authorizeRequests()
//.antMatchers("/tologin").permitAll() //登录页所有人都可以访问
//.antMatchers("/admin/**").hasRole("admin1")
.antMatchers("/admin/list").hasRole("admin1")
.antMatchers("/admin/role").hasRole("admin1")
.antMatchers("/admin/cate").hasRole("admin2")
.antMatchers("/admin/rule").hasRole("admin2");
// 项目里面使用了springSecurity spring Security下,X-Frame-Options默认为DENY,非spring Security环境下,X-Frame-Options的默认大多也是DENY,这种情况下,浏览器拒绝当前页面加载任何Frame页面
http.headers().frameOptions().sameOrigin();
//登录页(Security默认有一个登录页)
http.formLogin().permitAll()
.loginPage("/tologin") //指定自定义的登录页地址
.successForwardUrl("/admin/index") //登录成功跳转地址
.usernameParameter("username").passwordParameter("password");//匹配自定义登录页的name元素名称
// 开启注销功能,跳转到登录页
http.csrf().disable(); //退出失败可能能的原因
http.logout().logoutSuccessUrl("/tologin");
//开启记住我功能,cookie 默认保存14天
http.rememberMe()
.rememberMeParameter("remember");//匹配自定义登录页的name元素名称
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())//密码加密方式(有些版本的Security必须要指定)
.withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("admin1","admin2","admin3")
.and()
.withUser("yeqiu").password(new BCryptPasswordEncoder().encode("123")).roles("admin1")
.and()
.withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("admin2");
}
}
3、Security 和 Thymeleaf 页面整合(添加依赖:thymeleaf-extras-springsecurity)
<!--加入约束-->
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<!--
sec:authorize="isAuthenticated()" 用户是否登录
sec:authorize="hasAnyRole('admin1')" 是否具有某个角色
sec:authentication="name" 当前登录用户
sec:authentication="principal.authorities" 当前用户全部角色
-->
<div sec:authorize="isAuthenticated()">
<h2><span sec:authentication="name"></span>,您好 您的身份是
<span sec:authentication="principal.authorities"></span>
</h2>
</div>
<li sec:authorize="hasRole('admin2')">
<a onclick="xadmin.add_tab('权限管理','admin/rule')">
<cite>权限管理</cite>
</a>
</li>
来源:https://www.cnblogs.com/songl/p/14009866.html
0
投稿
猜你喜欢
- 本文实例讲述了Java中计算时间差的方法。分享给大家供大家参考。具体如下:假设现在是2004-03-26 13:31:40过去是:2004-
- 一、缩略图在浏览相册的时候,可能需要生成相应的缩略图。直接上代码:public class ImageUtil { private Logg
- 前端页面功能模块化拆分当一个系统的功能很多时,不可能所有功能模块的页面都写在一个页面里面,这时就需要将不同功能模块的页面拆分出去,就像模板一
- 前言最近一直被无尽的业务需求淹没,没时间喘息,终于接到一个能让我突破代码舒适区的活儿,解决它的过程非常曲折,一度让我怀疑人生,不过收获也很大
- 接触FFmpeg有一段时间了,它是音视频开发的开源库,几乎其他所有播放器、直播平台都基于FFmpeg进行二次开发。本篇文章来总结下采用FFm
- JAVA虽然是在C++基础上发展而来,但却对C++的许多缺陷有所改进,其中一个不得不提的就是字符串,我们知道,随着学习的深入,进入MFC时,
- java.sql.Timestamp(时间戳)继承父类:java.util.Date所有已实现的接口:Serializable, Clone
- 这篇文章需要对git具有一定的了解,并且知道如何安装git工具(其实就是上git官网下载个文件,安装)git这种版本控制工具有什么好处第一个
- 概述:App几乎都离不开与服务器的交互,本文主要讲解了flutter网络请求三种方式 flutter自带的HttpClient、 第三方库h
- AsyncTask什么是AsyncTaskAsyncTask是一个轻量级的异步任务类,它可以在线程池中执行后台任务,然后把执行的进度和结果传
- 引言在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在
- 前言1.Map里面只能存放对象,不能存放基本类型,例如int,需要使用Integer2.Map集合取出时,如果变量声明了类型,会先进行拆箱,
- 一、注解(annotations)列表@SpringBootApplication:包含了@ComponentScan、@Configura
- 1.短信平台购买次数地址https://market.aliyun.com/products/57000002/cmapi00046920.
- tcp客户端示例#include <errno.h> #include <sys/socket.h> #includ
- Spring 提供了自动代理机制,可以让容器自动生成代理,从而把开发人员从繁琐的配置中解脱出来 。 具体是使用 BeanPostProces
- Map 中ConcurrentHashMap是线程安全的,但不是所有操作都是,例如get()之后再put()就不是了,这时使用merge()
- 本文实例为大家分享了OpenCV实现人脸识别程序的具体代码,供大家参考,具体内容如下//Haar特征检测,人脸识别算法,是用xml作为训练后
- Spring框架的关键组件是面向方面编程(AOP)框架。面向方面的编程不仅打破程序逻辑分成不同的部分称为所谓的担忧。跨越多个点的应用程序的功
- 最近在学习springmvc,今天把springmvc 参数绑定给整理一下,也算个学习笔记吧!@RequestParam 绑定单个请求Req