• SpringBoot-28-springSecurity注销和权限控制


    8.5 springSecurity注销和权限控制

    • 继续上一节,在Security授权处设置开启注销,注意使用logoutSuccessUrl(“/”)方法
    package com.zk.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.WebSecurityConfiguration;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    
    /**
     * @author CNCLUKZK
     * @create 2022/8/3-23:07
     */
    //AOP思想,非拦截器
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        //链式编程--  授权规则
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //首页所有人可以访问,功能页只有对应有权限的人才能访问
            //请求授权的页面
            http.authorizeRequests()
                    .antMatchers("/").permitAll()  //首页所有人访问
                    .antMatchers("/level1/**").hasRole("vip1")
                    .antMatchers("/level2/**").hasRole("vip2")
                    .antMatchers("/level3/**").hasRole("vip3");
            //没有权限默认到登陆页,开启登陆页
            http.formLogin();
    
            http.httpBasic();
    
             //防止网站攻击工具:get易受攻击且是csrf攻击,改为post
            //新版本不用关闭
            //http.csrf().disable(); //springboot默认开启csrf是正确的,会防止跨站请求攻击,
            // 低版本的springboot需要关闭csrf,登陆失败存在的原因 或者设置为post方式注销
            
            //开启注销功能,注销跳到首页
            http.logout().logoutSuccessUrl("/");
        }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    退出失败问题:

    • get请求导致,设置为post方式注销
    • springboot默认开启csrf导致
    • springboot版本问题,新版本没问题
    • 然后导入security-thymeLeaf整合包,可以在thymeLeaf写security操作
    
    
        org.thymeleaf.extras
        thymeleaf-extras-springsecurity5
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 注意在index.html页面中引入的命名空间springsecurity5的命名空间是/extras/spring-security,这样才有sec命名提示
    
    
    • 1
    • 2
    • 在index.html里面用thymeLeaf操作security内容来实现根据security权限动态的显示菜单

    授权是给用户角色来授权

    sec:authorize=“”授权,通过这个用户是否有这个角色,来判断是否通过授权

    sec:authentication=“name” 认证信息

    
    
    
        
        
        首页
        
        
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 测试效果

    在这里插入图片描述

    下一篇:SpringBoot-29-springSecurity记住我及首页定制

  • 相关阅读:
    (成功最详细版本,自定义传参失败,跳转出现空白页面,校验文件失败)微信小程序扫码跳转小程序指定页面保姆级教程
    图表控件LightningChart使用教程:多线程数据可视化应用程序介绍
    FestDFS
    基于学习的决策树
    C# List<T>.IndexOf()方法的使用
    【论文阅读】Pay Attention to MLPs
    每日刷题记录 (三十一)
    头条文章采集工具-快速获取头条文章方法
    C语言获取文件长度
    【JVM】G1垃圾收集器
  • 原文地址:https://blog.csdn.net/weixin_42045639/article/details/126483961