SpringBoot-08 SpringSecurity

创建了一个新项目,创建时选择导入starter-web

1.环境搭建

1.1 导入thymeleaf

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

1.2 导入静态资源

  • cssjs这样的静态资源导入到static文件夹下
  • 前端页面导入到templates文件夹下

如果需要静态资源,可以私信我或者发邮件 moyu_zc@163.com

1.3 关闭thymeleaf缓存

spring.thymeleaf.cache=false

1.4 测试运行



2.用户认证和授权

2.1 导入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.2 创建Config

创建一个config文件夹:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll() //对于主页面都可以登录
.antMatchers("/level1/**").hasRole("vip1") //对于level1文件夹下的页面需要vip1才能登录
.antMatchers("/level2/**").hasRole("vip2") //对于level2文件夹下的页面需要vip2才能登录
.antMatchers("/level3/**").hasRole("vip3"); //对于level3文件夹下的页面需要vip3才能登录
//如果没有权限,进入登录页面,这是Security内部自带的
http.formLogin();
}
}

2.3 认证

就是给予下面这些用户相应的权限。

//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zc").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("test").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3");
}

大家可以自行测试。

3.注销及权限控制

3.1 注销

1.开启注销功能

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll() //对于主页面都可以登录
.antMatchers("/level1/**").hasRole("vip1") //对于level1文件夹下的页面需要vip1才能登录
.antMatchers("/level2/**").hasRole("vip2") //对于level2文件夹下的页面需要vip2才能登录
.antMatchers("/level3/**").hasRole("vip3"); //对于level3文件夹下的页面需要vip3才能登录
//如果没有权限,进入登录页面,这是Security内部自带的
http.formLogin(); //注销功能
http.logout().logoutSuccessUrl("/");
}

2.添加注销按钮

<!--登录注销-->
<div class="right menu">
<!--未登录-->
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 注销
</a>
</div>

3.2 权限控制

springboot 2.1.x版本以上不兼容这个标签,最好使用2.0.7及其以下的

1.加入thymeleaf、springsecurity整合依赖

<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>

2.增加对应的头部文件

xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

3.修改前端页面

<!--登录注销-->
<div class="right menu">
<!--未登录-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" >
用户名:<span sec:authentication="name"></span>
</a>
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 注销
</a>
</div>
</div>

4.首页定制

4.1 登录页面

1.修改Config

http.formLogin().loginPage("/toLogin");

2.修改login页面的路径

<form th:action="@{/toLogin}" method="post">

如果想要自定义action,可以使用:

http.formLogin().loginPage("/toLogin").loginProcessingUrl("xxx");

如果前端form表单中的name与后端不一一对应,可以使用:

 http.formLogin().loginPage("/toLogin").usernameParameter("xxx").passwordParameter("xxx");

4.2 记住我

1.前端添加记住我选框

<div class="field">
<input type="checkbox" name="remember">
</div>

2.修改Config

http.rememberMe().rememberMeParameter("remember");

个人博客为:

MoYu's HomePage

MoYu's Gitee Blog

SpringBoot-08 SpringSecurity的更多相关文章

  1. boke练习: springboot整合springSecurity出现的问题,传递csrf

    boke练习: springboot整合springSecurity出现的问题,传递csrf freemarker模板 在html页面中加入: <input name="_csrf&q ...

  2. boke练习: springboot整合springSecurity出现的问题,post,delete,put无法使用

    springboot 与 SpringSecurity整合后,为了防御csrf攻击,只有GET|OPTIONS|HEAD|TRACE|CONNECTION可以通过. 其他方法请求时,需要有token ...

  3. SpringBoot使用SpringSecurity搭建基于非对称加密的JWT及前后端分离的搭建

    SpringBoot使用SpringSecurity搭建基于非对称加密的JWT及前后端分离的搭建 - lhc0512的博客 - CSDN博客 https://blog.csdn.net/lhc0512 ...

  4. SpringBoot整合SpringSecurity简单实现登入登出从零搭建

    技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...

  5. 【使用篇二】SpringBoot集成SpringSecurity(22)

    SpringSecurity是专门针对基于Spring项目的安全框架,充分利用了依赖注入和AOP来实现安全管控.在很多大型企业级系统中权限是最核心的部分,一个系统的好与坏全都在于权限管控是否灵活,是否 ...

  6. SpringBoot+JWT+SpringSecurity+MybatisPlus实现Restful鉴权脚手架

    若图片查看异常,请前往掘金查看:https://juejin.im/post/5d1dee34e51d4577790c1cf4 前言 JWT(json web token)的无状态鉴权方式,越来越流行 ...

  7. SpringBoot 集成SpringSecurity JWT

    目录 1. 简介 1.1 SpringSecurity 1.2 OAuth2 1.3 JWT 2. SpringBoot 集成 SpringSecurity 2.1 导入Spring Security ...

  8. SpringBoot整合SpringSecurity示例实现前后分离权限注解

    SpringBoot 整合SpringSecurity示例实现前后分离权限注解+JWT登录认证 作者:Sans_ juejin.im/post/5da82f066fb9a04e2a73daec 一.说 ...

  9. 9、SpringBoot整合之SpringBoot整合SpringSecurity

    SpringBoot整合SpringSecurity 一.创建项目,选择依赖 选择Spring Web.Thymeleaf即可 二.在pom文件中导入相关依赖 <!-- 导入SpringSecu ...

  10. SpringBoot整合SpringSecurity实现JWT认证

    目录 前言 目录 1.创建SpringBoot工程 2.导入SpringSecurity与JWT的相关依赖 3.定义SpringSecurity需要的基础处理类 4. 构建JWT token工具类 5 ...

随机推荐

  1. AST & js interpreter

    AST & js interpreter 抽象语法树 & Javascript 解析器 https://astexplorer.net/ https://esprima.org/dem ...

  2. Flutter使用WebSockets

    文档 注意是WebSockets而不是socket.io install dependencies: web_socket_channel: demo import 'dart:convert'; i ...

  3. GoEasy使用阿里云OSS出现的问题

    前言:本人使用goeasy来实现微信小程序里面和其他人的im临时对话窗口,想要实现可以同时发送语音和视频.图片.表情包的话,就要通过goeasy关联到阿里云的存储对象. 报错:The OSS Acce ...

  4. TYLER ADAMS BRADBERRY:人到中年,要学会戒掉这三点

    在一些国家的一些人当中,总会出现这样一个问题"中年危机".而到了中年,人与人间的差距似乎也变得越来越大.有人说,人到中年,是一个门槛,有的人迈过去了,有的人没迈过去.但是,其实实话 ...

  5. 在多线程编程中不要使用sleep()、usleep()函数

    这两个函数是非线程安全的,可能会造成程序卡死. 对于c++程序,建议使用std::this_thread::sleep_for()和std::this_thread::yield()代替. 纯c程序可 ...

  6. 运用Spock编写高质量单元测试

    单元测试作为提升代码质量的有效方法,目前在国内各大互联网公司的开发团队中,尤其是业务团队中却鲜少被使用.这主要由于大家对于单元测试有一些认知错误,或者没有正确的打开方式.至今我们团队在小剧场.零代码运 ...

  7. spring boot插件开发实战和原理

    本文转载自spring boot插件开发实战和原理 实战:编写spring boot插件 为什么要编写boot插件 因为我们在开发的时候需要提供一些共同的功能,所以我们编写个共同的jar包.开发人员在 ...

  8. 随手一记,关于Java日期时间格式化

    在Java中,我们大多数情况下格式化日期都是使用simpleDateFormat,比如把一个日期格式化成:2019-12-31的形式,我们一般定义模板为:yyyy-MM-dd的形式. 我们需要注意的是 ...

  9. Java自学第8期——多线程

    1.多线程: 操作系统支持同时运行多个任务,一个任务通常是一个程序,所有运行中的程序就是一个进程().程序内部包含多个顺序执行流,每个顺序执行流就是一个线程. 并发:两个或者多个事件在同一个时间段内交 ...

  10. 手把手教你gitlab汉化

    详细教程如下: 一.在Github上 https://gitlab.com/xhang/gitlab/-/tags 下载对应的版本到服务器中 这种-zh结尾的才是汉化包,下载速度可能比较慢,有条件的可 ...