当前位置: 首页 > news >正文

SpringBoot07:SpringSecurity

Security是什么?

是一个安全框架。可以用来做认证和授权

官网:Spring Security

SpringSecurity环境搭建

1、创建一个新的project

2、导入thymeleaf依赖

        <dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring5</artifactId></dependency><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-java8time</artifactId></dependency>

3、导入静态资源:Spring security教程案例素材(狂神说Java之SpringBoot教程集合版): 狂神SpringBoot教程IDEA版中p34中用到的页面素材,学习Spring security

4、在controller包下,编写RouterController


@Controller
public class RouterController {@RequestMapping({"/","/index","/index.html"})public String index(){return "index";}@RequestMapping("/toLogin")public String toLogin(){return "views/login";}@RequestMapping("/level1/{id}")public String level1(@PathVariable("id") int id){return "views/level1/"+id;}@RequestMapping("/level2/{id}")public String level2(@PathVariable("id") int id){return "views/level2/"+id;}@RequestMapping("/level3/{id}")public String level3(@PathVariable("id") int id){return "views/level3/"+id;}
}

5、显示结果
首页:

 登录页:

用户认证和授权 

1、导入security的starter

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

2、在config包下,编写一个类去继承WebSecurityConfigurerAdapter,并且加上注解@EnableWebSecurity


@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {//授权@Overrideprotected void configure(HttpSecurity http) throws Exception {//请求授权的规则http.authorizeHttpRequests()//首页所有人都可以访问,功能页只有对应权限的人才可以访问.antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("vip1").antMatchers("/level2/**").hasRole("vip2").antMatchers("/level3/**").hasRole("vip3");//没有权限默认会到登录页面,需要开启登录的页面http.formLogin();}//认证@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {//这些数据正常应该从数据库中读auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("qiu").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2").and().withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3").and().withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3");}
}

注销

        //开启注销功能,注销成功之后跳到首页http.logout().logoutSuccessUrl("/");

 

记住我

前端:

<input type="checkbox" name="remember">记住我

后端: 

        //开启记住我功能,自定义接受前端传过来的参数http.rememberMe().rememberMeParameter("remember");

原理是保存了一个cookie和一个session,默认保存14天

定制登录页

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

http://www.lryc.cn/news/472.html

相关文章:

  • C++ 浅谈之 STL Vector
  • 【个人作品】非侵入式智能开关
  • 数据存储技术复习(三)未完
  • ThinkPHP数据库迁移工具
  • 代理模式(Proxy Pattern)
  • Elasticesearch内存详解
  • SpringCloud之断路器聚合监控
  • 凭借这份《2022测试八股文》候选者逆袭面试官,offer拿到手软
  • 【i2c协议介绍】
  • 167. 两数之和 II - 输入有序数组
  • 编译与链接------《程序员的自我修养》
  • 5分钟搞懂 强缓存与协商缓存
  • Ts笔记第一天
  • Android 12 Activity启动流程
  • VCS®/VCSi™User Guide
  • MongoDB简介及SpringBoot整合
  • 读书思考:步步惊心的《技术陷阱》
  • 求你了,不要再在对外接口中使用枚举类型了!
  • Java开发学习(四十六)----MyBatisPlus新增语句之id生成策略控制及其简化配置
  • 章鱼哥听歌
  • 软件测试电商项目实战(写进简历没问题)
  • 算法导论—分治法思想、动态规划思想、贪心思想
  • Spring-Data-Jpa实现继承实体类
  • 多线程环境下的伪共享
  • 【Taylor and Francis】1/2区云计算、物联网、机器学习类,SCIEEI双检,审稿友好
  • CleanMyMac X4.12新版本下载及功能介绍
  • 大数据技术架构(组件)26——Spark:Shuffle
  • 关于Zebec生态的改进提案,即将上线的 Nautilus 链
  • Python数据可视化(三)(pyecharts)
  • 【Redis面试指南】