SpringSecurity学习总结

学习目标:

全面掌握Spring Security各种细节

学习内容:


  1. Spring Security的默认登录页面
  2. 如何配置Spring Security
  3. sec标签的使用


前言

新手第一篇文章
这是基于狂神的SpringSecurity讲解后自己整理的笔记
主要用于自己巩固学过的知识


一、Spring Security的默认登录页面

Spring Security框架一般都是要和Thymeleaf一起整合使用的
当我们在pom里面导入了它们的依赖时

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

我们在访问前端页面时会有一个默认的登录页面用来拦截访问路径
用户名是user,密码是控制台上的密码

二、如何配置Spring Security

1.创建一个类

使用@EnableWebSecurity开启WebSecurity模式
然后继承WebSecurityConfigurerAdapter 自定义Security策略

2.授权(Authorization)

  • 设置哪些人可以访问哪些页面
  • 自定义登录页面
  • 开启记住我功能

设置谁可以访问

@Override
    protected void configure(HttpSecurity http) throws Exception {

        //首页所有人都可以访问,功能页只能对应的有权限的人才能访问,类似于拦截器
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");


        //login页面和logout页面都是springSecurity自带的
        //没有权限自动进入到登录界面,这个登录界面Login不是自己写的页面,是系统后台的页面
        //http.formLogin();
        http.formLogin()
                .loginPage("/toLogin")
                .usernameParameter("user")
                .passwordParameter("pwd");

        //开启注销功能(要在前端写跳转到这个注销地址/logout的标签)
        //注销成功默认跳到登录login页面
        //http.logout();
        http.csrf().disable();  //用来解决注销错误的Bug
        http.logout().logoutSuccessUrl("/");  //设置注销成功页面为首页

        //开启记住我功能,自定义从前端接收参数
        http.rememberMe().rememberMeParameter("remember");

    }

3.认证(Authentication)

  • 设置用户
  • 设置权限
@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //这是在内存中定义的数据,也可以在数据库中去拿
        //(type=Internal Server Error, status=500).密码未加密错误
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("Coder").password(new BCryptPasswordEncoder().encode("12345")).roles("vip1")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("12345")).roles("vip1", "vip2", "vip3");
    }

三、sec标签的使用


  • sec:authorize 类似于if判断,true现实,否则不显示
  • isAuthenticated() 是否认证了
  • sec:authentication 返回一些AuthenticationManagerBuilder auth的属性,比如用户名和权限
  • hasRole("") 是否拥有该权限

name和authorities都是Principal接口实现类里的属性
分别表示用户名和用户的权限,可以直接在配置中的认证(Authentication)中拿到

                <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>
                    <a class="item" th:href="@{/logout}">
                        <i class="iconfont icon-log-out"></i> 注销
                    </a>
                </div>                
            <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>

link

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值