码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringBoot的Spring Security用户授权和认证


    目录

    1、在pom.xml里面导入SpringSecurity的依赖

    2、编写一个config,继承WebSecurityConfigureAdapter

    3、重写该类中授权操作的方法

    3.1、效果演示

    4、重写该类中的认证操作方法 

    ​4.1、认证方法的源码

    5、演示

    5.1、只有vip1的权限的演示

    5.2、具有全部权限的演示


    不需要改动原来代码就可以添加很多拦截操作。AOP原理

    在基本环境搭建好的前提下

    1、在pom.xml里面导入SpringSecurity的依赖

    2、编写一个config,继承WebSecurityConfigureAdapter

    创建SecurityConfig,继承WebSecurityConfigureAdapter,并添加注解@EnableWebSecurity

    3、重写该类中授权操作的方法

    1. // AOP 拦截器
    2. @EnableWebSecurity // 开启webSecurity模式 代表 SecurityConfig 被Spring托管了
    3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
    4. // 授权
    5. @Override
    6. protected void configure(HttpSecurity http) throws Exception {
    7. // 首页所有人可以访问,功能页只有对应有权限的人才能访问
    8. // 请求授权的规则
    9. http.authorizeRequests()
    10. .antMatchers("/").permitAll()
    11. .antMatchers("/level1/**").hasRole("vip1")
    12. .antMatchers("/level2/**").hasRole("vip2")
    13. .antMatchers("/level3/**").hasRole("vip3");
    14. // 没有权限默认回到登录页面,需要开启登录的页面
    15. // /login
    16. http.formLogin();
    17. }
    18. }

    3.1、效果演示

    4、重写该类中的认证操作方法 

    这个是内存中的权限认证

    1. // 认证,springboot 2.1.x 可以直接使用
    2. // 密码编码: PasswordEncoder 不用密码编码登录的时候会报错
    3. // 在Spring Security 5.0+ 新增了很多加密方法
    4. @Override
    5. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    6. // 这些都是从数据库中读取
    7. auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
    8. .withUser("zhoujie").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
    9. .and()
    10. .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
    11. .and()
    12. .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    13. }

    4.1、认证方法的源码

    5、演示

    5.1、只有vip1的权限的演示

    5.2、具有全部权限的演示

     

  • 相关阅读:
    h5中左边有侧边栏,如何将右边bootstrap的div的布局设置为两列
    Java 命令执行笔记
    Vue:object变化侦测
    分布式一致性算法 -- Paxos
    1337_树莓派上安装docker
    SpringSecurity系列一:08 oauth2认证:ClientCredentialsTokenEndpointFilter 过滤器
    集成正态云和动态扰动的哈里斯鹰优化算法
    【Java基础面试二】、为什么Java代码可以实现一次编写、到处运行?
    C++初阶(1)
    数字藏品NFT“无聊猿”BAYC的内忧与外患
  • 原文地址:https://blog.csdn.net/qq_43880100/article/details/127320188
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号