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


猜你喜欢
- 本文所需的数据库初始文件,Hibernate常用操作的完整示例代码(包含所有Hibernate操作所需jar文件)提供下载学习:http:/
- 1.背景最近项目中有一个需求需要从用户输入的值找到该值随对应的名字,由于其它模块已经定义了一份名字到值的一组常量,所以想借用该定义。2.实现
- “无论是什么类型,所有的数据都是一系列的位,即一系列0和1。变量的含义是通过解释这些数据的方式来传达的。”——这句原话是书上翻译的,不过后一
- 单线程实现文件分割在老的FAT32文件系统中,最大的单个文件大小必须保存在4G内,对于经常看电影的我这个是不能允许的。不过现在Windows
- 最近需要用到微信的标签功能(如下图所示)。该功能可以添加已有标签,也可以自定义标签。也可以删除已编辑菜单。研究了一番。发现还是挺
- 需求读200+的CSV/EXCEL文件,按文件名称存到不同数据库前期准备环境maven + jdk8 + mysql代码展示pom文件<
- 算法的主题思想:1.优秀的算法因为能够解决实际问题而变得更为重要;2.高效算法的代码也可以很简单;3.理解某个实现的性能特点是一个挑战;4.
- notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。它是看不见的程序组件(Broadcast Receiver
- 1.生成自己的注解(为了确定在哪些位置使用)/** * 关闭patch delete的model处理,否则会报错 */@Target({El
- javabean与map的转换有很多种方式,比如:1、通过ObjectMapper先将bean转换为json,再将json转换为map,但是
- 目录一、Shiro简介 核心角色核心理念二、整合SpringBoot2框架 1、核心依赖2、Shiro核心配置3、域对象
- 本文实例讲述了Android获取手机系统版本等信息的方法。分享给大家供大家参考。具体如下:String phoneInfo = "
- 文件数据流在java语言中,进行文件输入和输出时,经常会使用到FileIntputStream和FileOutputStream两个文件数据
- 对象是使用new创建的,但是并没有与之相对应的delete操作来回收对象占用的内存。当我们完成对某个对象的使用时,只需停止对该对象的引用:将
- 摘要本文主要讲解mall通过整合SpringSecurity和JWT实现后台用户的登录和授权功能,同时改造Swagger-UI的配置使其可以
- 前言本文主要给大家介绍了关于C#中foreach遍历的用法以及c#使用foreach需要知道的一些事,分享出来供大家参考学习,下面话不多说了
- 本文实例为大家分享了Java实现二分查找的变种,供大家参考,具体内容如下普通二分查找:先回顾一下普通的二分查找注意:二分查找有这样一个问题:
- 配置绑定所谓配置绑定”就是把配置文件中的值与 JavaBean 中对应的属性进行绑定。通常,我们会把一些配置信息(例如,
- @Autowired加到接口上但获取的是实现类问题Spring的@Autowired加到接口上但获取的是实现类? &
- 一、环境准备准备开发环境创建一个Maven项目pom.xml添加依赖resources下添加spring的配置文件applicationCo