首页 > 其他分享 >12、注销及权限认证

12、注销及权限认证

时间:2022-09-25 15:12:33浏览次数:52  
标签:password 12 http BCryptPasswordEncoder Level antMatchers new 注销 权限

thymeleaf和springsecurity整合包

 <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

导入命名空间

<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">  <!-- 主要是这行代码 官方建议使用 --

securityConfig.java

@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")
//                csrf--跨站请求伪造,springboot默认开启,手动关闭
                .and().csrf().disable();

        // 没有权限默认跳到登录页面
        http.formLogin();
        // 注销
       http.logout().logoutSuccessUrl("/");
    }
    // 认证

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 这些正常在数据库里面写
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }
}

index.html

点击查看代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">  <!-- 主要是这行代码 官方建议使用 -->>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>首页</title>
    <!--semantic-ui-->
    <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
    <link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">
</head>
<body>

<!--主容器-->
<div class="ui container">

    <div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
        <div class="ui secondary menu">
            <a class="item"  th:href="@{/index}">首页</a>

            <!--登录注销-->
            <div class="right menu">
                <!--如果未登录-->
               <div sec:authorize="!isAuthenticated()">
                   <a class="item" th:href="@{/toLogin}">
                       <i class="address card icon"></i> 登录
                   </a>
               </div>
<!--如果已经登录,用户名+注销-->
                <div sec:authorize="isAuthenticated()">
                    <a class="item">
                        用户名:<span sec:authentication="name"></span>
                        角色: <span sec:authentication="principal.authorities"></span>
                    </a>
                </div>
                <div sec:authorize="isAuthenticated()">
                <a class="item" th:href="@{/logout}">
                    <i class="sign out icon"></i> 注销
                </a>
                </div>
                <!--已登录
                <a th:href="@{/usr/toUserCenter}">
                    <i class="address card icon"></i> admin
                </a>
                -->
            </div>
        </div>
    </div>

    <div class="ui segment" style="text-align: center">
        <h3>Spring Security Study by 秦疆</h3>
    </div>

    <div>
        <br>
        <div class="ui three column stackable grid">
<!--            对于角色页面展示-->
            <div class="column" sec:authorize="hasRole('vip1')">
                <div class="ui raised segment">
                    <div class="ui">
                        <div class="content">
                            <h5 class="content">Level 1</h5>
                            <hr>
                            <div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div>
                            <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
                            <div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
                        </div>
                    </div>
                </div>
            </div>
<!--对应角色页面展示-->
            <div class="column" sec:authorize="hasRole('vip2')">
                <div class="ui raised segment">
                    <div class="ui">
                        <div class="content">
                            <h5 class="content">Level 2</h5>
                            <hr>
                            <div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div>
                            <div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
                            <div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div>
                        </div>
                    </div>
                </div>
            </div>
<!--对应角色权限页面展示-->
            <div class="column" sec:authorize="hasRole('vip3')">
                <div class="ui raised segment">
                    <div class="ui">
                        <div class="content">
                            <h5 class="content">Level 3</h5>
                            <hr>
                            <div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div>
                            <div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
                            <div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    
</div>


<script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script>

</body>
</html>

标签:password,12,http,BCryptPasswordEncoder,Level,antMatchers,new,注销,权限
From: https://www.cnblogs.com/ahhh7931/p/16727883.html

相关文章

  • 第312场周赛
    T1:简单排序,sort一下即可T2:寻找连续子数组按位与值最大前提下,最长长度。非常不明显,最大按位与就是最大值,则最长长度则连续最大值的长度最大值(md仔细分析呀)T3:寻找下标满......
  • CSP201912_3
    CSP201912_3目录CSP201912_3题目思路Code题目化学方程式思路大模拟,考虑先整体以等号为界,将方程式分为左串与右串。分别针对两个子串,以加号为界分离成独立的化合物,在......
  • 20201220蔡笃俊《信息安全系统设计与实现》第七、八章学习笔记
    一、任务内容自学教材第7,8章,提交学习笔记(10分)知识点归纳以及自己最有收获的内容(3分)问题与解决思路(2分)实践内容与截图,代码链接(3分)...(知识的结构化,知识的完整性等,提......
  • 学习记录12异常机制
    什么是异常软件程序在运行过程中,非常可能遇到刚刚提到的这些异常问题,我们叫异常,英文是:Exception,意思是例外。这些,例外情况,或者叫异常,怎么让我们写的程序做出合理的处理。......
  • php mysql创建库 创建用户 并授权用户可使用的库 - mysql权限管理例子
    以下是php代码,可以复制起来测试测试 $dbname="ceshi1222_com";$username="user222";//创建库$sql="CREATEDATABASE{$dbname}DEFAULTCHARACTERSETutf8COLLAT......
  • mysql root用户远程登录并获取所有权限
    远程登录必须要满足三个条件1:服务器要对外开放3306端口2:要有远程操作的权限端口就不说了 主要记录一下权限的设置方法依次执行下面四条命令在本机先使用root用户......
  • [JSOI2012]玄武密码
    题目对于每一段文字tt,求出其最长的前缀pp,满足pp是ss的子串,其中ss是字串。题解我们可以用ac自动机来做,先把所有字串建个ac自动机,然后用母串在上面跑,把那些点都进行......
  • Mall 动态权限学习参考
    代码仓库https://github.com/Rain-with-me/JavaStudyCode/tree/main/4-springboot-security-dynic本文查看Mall的动态权限管理动态权限的思路路由拦截思路把......
  • 力扣1912——设计电影租借系统
    1912.设计电影租借系统难度困难你有一个电影租借公司和 n 个电影商店。你想要实现一个电影租借系统,它支持查询、预订和返还电影的操作。同时系统还能生成一份当......
  • 20th 2022/7/18 模拟赛总结12
    这次嗯,题目真是没有半点水分,干巴巴一片T1T3省选模拟,T2NOIP,恐怖的是T4???这次估计上紫赛时T1-T4-T2-T3首先读题很久,30min过,然后着手T1,找规律,没有半分,只用仅有的数论知识......