博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spriingboot -- spring-security 对页面资源的认证与授权
阅读量:3921 次
发布时间:2019-05-23

本文共 2780 字,大约阅读时间需要 9 分钟。

1、引入依赖和版本:

1)、properties

1.8
3.0.9.RELEASE
2.2.2

2)、依赖

org.thymeleaf.extras
thymeleaf-extras-springsecurity4
3.0.2.RELEASE
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-security

2、配置 WebSecurityConfigurerAdapter 的相关属性:

package com.example.config;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;@EnableWebSecuritypublic class MySecurityConfig extends WebSecurityConfigurerAdapter{
@Override protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll() //访问任何资源都转跳到该路径,即首页 .antMatchers("/level1/**").hasRole("VIP1") //访问 **/level1/**路径下的资源都需要角色 VIP1, 相当于session一样; 如 page/level1/1 .antMatchers("/level2/**").hasRole("VIP2") .antMatchers("/level3/**").hasRole("VIP3"); // http.formLogin(); //当要跳转需要权限的以上资源或者"/login"时会跳转到默认登录界面 http.csrf().disable(); //将csrf关闭 http.formLogin().loginPage("/MyLogin"); //跳转到自己制定的登陆页面 http.formLogin().defaultSuccessUrl("/"); //登录成功跳转的页面 http.formLogin().failureUrl("/aaa"); //登录失败后跳转到自己制定的登陆失败页面 http.formLogin().loginProcessingUrl("/message"); //设置自定义页面form表单action路径 /* * 1、用户注销, 清空session * 2、注销成功会返回/login?logout 页面(默认) */ http.logout().logoutSuccessUrl("/"); //设置注销成功后返回的页面, 即首页 //开启 记住我, 并设置 checkbox 的 name, 向浏览器提交一个 cookie http.rememberMe().rememberMeParameter("remember"); System.out.println("WebSecurity设置成功..."); } //授权角色信息 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//将三个用户信息保存在内存中 auth.inMemoryAuthentication().withUser("huang").password("admin").roles("VIP1", "VIP2") .and().withUser("he").password("admin").roles("VIP2", "VIP3") .and().withUser("he2").password("admin").roles("VIP3", "VIP4"); } }

3、自定义的登录页面:

    
第一个HTML页面
Title自定义表单验证:
用户名:
密码:
记住我

4、首页(关于security的认证函数):

Insert title here

欢迎光临武林秘籍管理系统

游客您好,如果想查看武林秘籍 请登录

,您好,您的角色有:


普通武功秘籍

高级武功秘籍

转载地址:http://jyern.baihongyu.com/

你可能感兴趣的文章
代码改变世界,也改变了我
查看>>
2021,未来可期
查看>>
阿星Plus:基于abp vNext开源一个博客网站
查看>>
写给自己,2020的年终总结
查看>>
Flash 生命终止,HTML5能否完美替代?
查看>>
ML.NET生成器带来了许多错误修复和增强功能以及新功能
查看>>
微信适配国产操作系统:原生支持 Linux
查看>>
我的2020年终总结:新的角色,新的开始
查看>>
C# 9 新特性 —— 增强的模式匹配
查看>>
ASP.NET Core Controller与IOC的羁绊
查看>>
如何实现 ASP.NET Core WebApi 的版本化
查看>>
探索 .Net Core 的 SourceLink
查看>>
AgileConfig-如何使用AgileConfig.Client读取配置
查看>>
【gRPC】 在.Net core中使用gRPC
查看>>
整合.NET WebAPI和 Vuejs——在.NET单体应用中使用 Vuejs 和 ElementUI
查看>>
“既然计划没有变化快,那制订计划还有个卵用啊!”
查看>>
C#实现网页加载后将页面截取成长图片
查看>>
C# 在自定义的控制台输出重定向类中整合调用方信息
查看>>
【gRPC】ProtoBuf 语言快速学习指南
查看>>
C# 9 新特性 —— 补充篇
查看>>