SpringSecurity的简单概述以及配置SpringSecurity的默认登录页面
SpringSecurity的简单概述
是什么:SpringSecurity融合Spring技术栈,提供JavaEE应 用的整体安全解决方案;提供全面的安全服务
有什么用:可以进行身份验证,就是证明你是谁;也可以进行授权,就是你可以做什么
配置SpringSecurity的默认登录页面
- 搭建SpringSecurity环境
创建maven项目后,添加security-pom依赖
<!-- springboot项目都要继承boot父工程--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version></parent><properties><!-- 配置jdk版本 --><java.version>1.8</java.version></properties><dependencies><!-- 加入web相关的jar包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--以下为security需要的依赖包--><!--web安全模块--><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-web</artifactId></dependency><!--java配置模块--><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-config</artifactId></dependency><!-- 标签库 --><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-taglibs</artifactId></dependency></dependencies>
此时运行启动类(Application),页面显示如下登录页面(不同版本页面可能不同),说明SpringSecurity配置成功
加入SpringSecurityConfig配置类(SpringSecurity的配置基本都写在此类中)
给SpringSecurityConfig类添加@SpringBootConfiguration和@EnableWebSecurity注释,继承WebSecurityConfigurerAdapter类,并重写configure(HttpSecurity http)方法
@Configuration@EnableWebSecuritypublic class AppWebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//默认所有资源不放行,所以注释掉}
运行启动类,发现所有接口和静态页面都可以访问了,不在出现SpringSecurity提供的默认页面(注意:需要把配置类中:super.configure(http)注释掉才生效)
现在,对我们的页面和静态资源进行授权
@Configuration @EnableWebSecurity public class AppWebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//默认所有资源不放行,所以注释掉 + http.authorizeRequests().antMatchers("/css/**", "/login.html","/main").permitAll()//设置匹配的资源放行 + .anyRequest().authenticated();//剩余任何资源必须认证 }
现在去访问这些已放行的页面或接口都没问题,没放行的都访问不了(显示如下)
我们一般访问没有认证的页面时应该是跳转到登录页面的,而不是出现如上页面,所以我们应该自定义一个登录页面,修改配置类
@Configuration@EnableWebSecuritypublic class AppWebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//默认所有资源不放行,所以注释掉http.authorizeRequests().antMatchers("/css/**", "/login.html","/main").permitAll()//设置匹配的资源放行.anyRequest().authenticated();//剩余任何资源必须认证+ //指定默认的登录页面+ http.formLogin().loginPage("/login.html");}
- 再一次运行启动类,当访问没有权限的页面时会自动跳转到我们配置的登录页面
- 配置登录成功跳转的页面
@Configuration@EnableWebSecuritypublic class AppWebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//默认所有资源不放行,所以注释掉http.authorizeRequests().antMatchers("/css/**", "/login.html","/main").permitAll()//设置匹配的资源放行.anyRequest().authenticated();//剩余任何资源必须认证//指定默认的登录页面http.formLogin().loginPage("/login.html");+ http.formLogin().loginProcessingUrl("/login")//这个路径和登录表单上的要一样
+ .usernameParameter("username")
+ .passwordParameter("password")
+ .defaultSuccessUrl("/main", true);//登录成功后跳转的页面+ http.csrf().disable();}
- 现在可以进行登录,用户名为user, 密码为控制台上的密码(不同版本可能不同),登录成功,跳转至我们配置的成功页面
- 如果我们想要自己配置用户名密码,需要在配置类中添加一个重写方法
@Configuration@EnableWebSecuritypublic class AppWebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//super.configure(http);//默认所有资源不放行,所以注释掉http.authorizeRequests().antMatchers("/css/**", "/login.html","/main").permitAll()//设置匹配的资源放行.anyRequest().authenticated();//剩余任何资源必须认证//指定默认的登录页面http.formLogin().loginPage("/login.html");http.formLogin().loginProcessingUrl("/login")//这个路径和登录表单上的要一样.usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/main", true);//登录成功后跳转的页面http.csrf().disable();}+ @Override
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {+ // super.configure(auth);
+ auth.inMemoryAuthentication().withUser("zhangsan").password("123456").roles("role")
+ .and()
+ .withUser("lisi").password("123456").authorities("查看", "删除");}
- 登录如果报错:java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null,我们在添加一个配置类MyPasswordEncoder实现PasswordEncoder”
- 再次运行,问题解决
SpringSecurity的简单概述以及配置SpringSecurity的默认登录页面相关推荐
- SpringSecurity的简单应用(一)
java项目首先要提的就是jar包了,Springsecurity的jar下载地址:http://static.springsource.org/spring-security/site/downlo ...
- SpringSecurity+Mybatis实现用户自助注册登录(含角色),打造简单安全的注册登录页面。
#项目架构.功能点 架构: Springboot2.5.+ MySQL数据库8.0+(记录用户信息.角色清单.用户角色对照表) 持久层Mybatis 用户注册页面RegisterPage采用Thyme ...
- SpringSecurity的简单使用
目录 概述 HelloWorld 依赖 写一个Controller 访问接口 用户认证 设置登录的用户名和密码 通过配置文件 通过配置类 自定义编写实现类 查询数据库的用户认证 自定义登录页面 用户授 ...
- SpringSecurity的简单使用使用案列说明
SpringSecurity Spring Security是 Spring提供的安全认证服务的框架. 使用Spring Security可以帮助我们来简化认证 和授权的过程.官网:https://s ...
- springSecurity 学习(一)创建springSecurity项目
创建springsecurity项目的方式有很多,最简单的是 使用Spring Initializr工具, 点击下一步,输入项目基本信息,选择jdk版本 再下一步,选择要使用的组件 完成项目创建. 项 ...
- SpringSecurity权限管理系统实战—六、SpringSecurity整合JWT
文章目录 系列目录 前言 一.无状态登录 二.JWT介绍 1.什么是jwt 头部(Header) 载荷(Payload) 签名(Signature) 2.JWT工作流程 3.简单实现 三.整合JWT ...
- SpringSecurity前后端分离(包含token和验证码登录)
SpringSecurity前后端分离 从上至下操作,直接上手SpringSecurity 文章目录 SpringSecurity前后端分离 1.项目环境 maven依赖 数据库表 2.自定义User ...
- Springsecurity搭建自定义登录页面
Springsecurity搭建自定义登录页面 1.springSecurity的搭建 新建一个springboot的web项目,我这边只选中了web,建立后如下: image.png pom依赖: ...
- Spring-Security (学习记录二)--修改为自己的登录页面
目录 1.修改spring-security.xml配置文件 2.增加login.jsp页面 3.重启项目即可看到效果 1.修改spring-security.xml配置文件 <!--auto- ...
最新文章
- Android Jetpack - Emoji表情符号初探
- java序列化的作用
- react 调用 html5,React-Native Webview 和H5交互的两种方式
- Silverlight RIA Servcie 删除子对象实体提交错误的问题[解决]
- drupal linux安装,在Debian 10(Buster) Linux服务器中安装drupal 8.8.0的说明
- lottie 动画_使用After Effects和Lottie制作网络动画而不会损失质量
- 拍摄半身照,模特的手可以放哪儿?
- 软件项目经理新手上路16 - 后记,一切才刚刚开始
- Ubuntu下多版本OpenCV共存和切换
- 给控件做数字签名之二:生成证书文件
- 苹果有益让老iPhone变慢以迫使消费者购买新一代的iPhone?
- 【贪心算法】跳跃游戏
- OpenFileDialog/SaveFileDialog 中 Filter用法?
- HP刀片服务器系统Flex,HP刀片服务器系统Flex-10 VC配置与VMware vSphere网络设计
- Largest Submatrix (最大全1子矩阵)
- calcbusiness使用教程_Calc Business
- 一些版本管理常用的文档链接
- 中国家电闯入国外运营费成本比中国高
- matlab:实现“必应”的图片搜索功能并将图片保存
- 雷哥爱财,取之有道:4个月取了4000块