Spring Boot学习笔记-Spring Security

Spring Boot学习

官网:https://spring.io/projects/spring-boot#overview

文档:https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/html/

参考视频:【狂神说Java】SpringBoot最新教程IDEA版通俗易懂_哔哩哔哩_bilibili

项目完整参考代码:lexiaoyuan/SpringBootStudy: My Spring Boot study notes (github.com)SpringBootStudy: 我的Spring Boot学习笔记 (gitee.com)

Spring Security

官网:https://spring.io/projects/spring-security

文档:https://docs.spring.io/spring-security/site/docs/5.3.2.BUILD-SNAPSHOT/reference/html5/

认识SpringSecurity

Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入 spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理!

记住几个类:

  • WebSecurityConfigurerAdapter:自定义Security策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity:开启WebSecurity模式

Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)。

“认证”(Authentication)

身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。

身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。

“授权” (Authorization)

授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。

这个概念是通用的,而不是只在Spring Security 中存在。

测试环境搭建

在这里插入图片描述

  • 新建一个controller包,在controller包下新建RouterController.java,用于跳转到各个页面
@Controller
public class RouterController {
    @GetMapping({"/", "/index"})
    public String index(){
        return "index";
    }

    @GetMapping("/toLogin")
    public String login(){
        return "views/login";
    }

    @GetMapping("/level1/{id}")
    public String level1(@PathVariable("id") int id) {
        return "views/level1/"+id;
    }

    @GetMapping("/level2/{id}")
    public String level2(@PathVariable("id") int id) {
        return "views/level2/"+id;
    }

    @GetMapping("/level3/{id}")
    public String level3(@PathVariable("id") int id) {
        return "views/level3/"+id;
    }

}
  • 运行项目,访问:http://localhost:8080/,测试各页面跳转是否正常,正常!

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

认证和授权

  • 引入Spring Security模块,在pom.xml中添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
  • 编写配置类,新建config目录,在config目录下新建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");
    
            // 开启登录页面,没有权限时默认到登录页面 /login
            http.formLogin();
        }
    
        // 认证
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    
             // 从内存中得到用户及相应的角色。    这些数据应该从数据库中读取
            //Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
            //要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
            //spring security 官方推荐的是使用BCrypt加密方式。
            auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                    .withUser("lexiaoyuan").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2")
                    .and()
                    .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
                    .and()
                    .withUser("beta").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
        }
    }
    
  • 运行项目,访问:http://localhost:8080/,想进入level1(或2或3)。或者直接访问:http://localhost:8080/login,发现会自动跳转到一个登录页面(框架自带的)。

在这里插入图片描述

  • 输入错误的用户名和密码(除上面定义的3个用户:lexiaoyuan、root、beta及对应的密码)会跳转到http://localhost:8080/login?error

在这里插入图片描述

  • 输入lexiaoyuan,123456,登录,再访问点击level中的链接,发现只能访问level1中的和level2中的,访问level3时会报错。

在这里插入图片描述
在这里插入图片描述

  • 同样的,输入其他设置好的用户后也只能访问对应的页面。测试成功!

注销及权限控制

  • 开启注销功能
// 授权
@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
    http.formLogin();

    // 开启注销功能
    http.logout();
}
  • 前端index.html中增加一个注销按钮,请求为/logout(源码中默认的请求地址)
<!--登录注销-->
<div class="right menu">
    <!--未登录-->
    <a class="item" th:href="@{/toLogin}">
        <i class="address card icon"></i> 登录
    </a>
    <a class="item" th:href="@{/logout}">
        <i class="sign-out icon"></i> 注销
    </a>
</div>
  • 测试一下。运行项目,访问:http://localhost:8080/login

在这里插入图片描述

登录之后,点击注销

在这里插入图片描述
在这里插入图片描述

点击Log Out按钮

在这里插入图片描述

重新跳到登录页面

  • 注销成功后跳转到首页
@Override
protected void configure(HttpSecurity http) throws Exception {
    // ...
    // 注销成功跳转到首页
    http.logout().logoutSuccessUrl("/");
}
  • 测试一下,重新运行项目,访问:http://localhost:8080/login,登录成功后,点击注销按钮,又跳转到首页。成功!

根据认证状态,修改导航栏

用户没有登录的时候,导航栏上只显示登录按钮,用户登录之后,导航栏可以显示登录的用户信息及注销按钮。

需要结合Thymeleaf + Spring Security提供的一些方法。

官方文档:https://www.thymeleaf.org/doc/articles/springsecurity.html

GitHub地址:https://github.com/thymeleaf/thymeleaf-extras-springsecurity

官方例子:https://github.com/thymeleaf/thymeleafexamples-springsecurity

  • 首先在pom.xml中引入新的依赖
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 -->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>
  • 修改index.html,增加命名空间
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
  • 修改index.html,通过sec:authorize="isAuthenticated()判断是否登录
<!--登录注销-->
<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>
        <a class="item" th:href="@{/logout}">
            <i class="sign-out icon"></i> 注销
        </a>
    </div>
</div>
  • 重新运行项目,访问:http://localhost:8080/,可以看到,只显示了登录按钮

在这里插入图片描述

  • 访问:http://localhost:8080/login登录成功后,可以看到,显示了登录用户及其拥有的角色

在这里插入图片描述

  • 再点击注销,确认注销后,又回到首页。

在这里插入图片描述
在这里插入图片描述

  • OK,这个需求完成!

根据用户拥有的不同角色,显示不同的页面

  • 继续修改index.html,通过sec:authorize="hasRole('vip1')"来完成角色功能认证
<!--sec:authorize="hasRole('vip1')"-->
<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>

<!--sec:authorize="hasRole('vip2')"-->
<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>

<!--sec:authorize="hasRole('vip3')"-->
<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>
  • 重新Build一下项目即可,测试:访问:http://localhost:8080/,可以看到,还没有登录,都没有显示

注:如果重新Build后,访问没有更新,可能是thymeleaf模板有缓存,可以在application.properties中设置,然后重新运行项目即可

spring.thymeleaf.cache=false

在这里插入图片描述

  • 访问:http://localhost:8080/login,登录成功后可以看到对应用户允许显示的页面。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  • ok,注销和权限控制完成!

记住我

  • 开启记住我
@Override
protected void configure(HttpSecurity http) throws Exception {
    // ...
    // 开启记住我
    http.rememberMe();
}
  • 访问:http://localhost:8080/login,会多一个记住我的按钮,输入用户名,密码,并勾选记住我,登录

在这里插入图片描述

  • 按F12,查看浏览器的cookie,可以看到,登录成功后,会将将cookie发送给浏览器保存

在这里插入图片描述

  • 关闭浏览器,再次访问http://localhost:8080/,会直接显示首页,不用登录

在这里插入图片描述

  • 点击注销并确认后,可以看到,cookie被删除了

在这里插入图片描述

定制首页

现在这个登录页面都是spring security 默认的,怎么样可以使用我们自己写的Login界面呢?

  • 开启自定义的登录页面
// 定制首页:.loginPage("/toLogin")
http.formLogin().loginPage("/toLogin");
  • 在前端指向我们自定义的请求请求
<a class="item" th:href="@{/toLogin}">
    <i class="address card icon"></i> 登录
</a>
  • 测试:访问:http://localhost:8080/,点击登录按钮,跳转到自己写的登录页面

在这里插入图片描述

  • 输入用户名和密码后,点击登录按钮,会报错,因为我们还没有写接受登录的请求

在这里插入图片描述

  • 接受登录请求:【方式一】: 修改login.html,通过post方式提交到/toLogin请求
<form th:action="@{/toLogin}" method="post">
    <div class="field">
        <label>Username</label>
        <div class="ui left icon input">
            <input type="text" placeholder="Username" name="username">
            <i class="user icon"></i>
        </div>
    </div>
    <div class="field">
        <label>Password</label>
        <div class="ui left icon input">
            <input type="password" name="password">
            <i class="lock icon"></i>
        </div>
    </div>
    <input type="submit" class="ui blue submit button"/>
</form>

注意:提交的表单中用户名和密码的input的name属性必须为usernamepassword(这是默认接受的name,需要匹配才可以),否则会跳转到http://localhost:8080/toLogin?error,页面依然在登录页面。

  • 在重新Build一下项目,访问:http://localhost:8080/toLogin,输入用户名和密码后可以看到,正常登录。

在这里插入图片描述

  • 接受登录请求:【方式二】: 在定制首页的后面继续设置
// 定制首页:.loginPage("/toLogin")
// 登录提交表单的请求:.loginProcessingUrl("/login")
http.formLogin()
        .loginPage("/toLogin")
        .usernameParameter("username")
        .passwordParameter("password")
        .loginProcessingUrl("/login");
  • 然后再修改login.html,走/login请求
<form th:action="@{/login}" method="post">
    <div class="field">
        <label>Username</label>
        <div class="ui left icon input">
            <input type="text" placeholder="Username" name="username">
            <i class="user icon"></i>
        </div>
    </div>
    <div class="field">
        <label>Password</label>
        <div class="ui left icon input">
            <input type="password" name="password">
            <i class="lock icon"></i>
        </div>
    </div>
    <input type="submit" class="ui blue submit button"/>
</form>
  • 在重新运行项目,访问:http://localhost:8080/toLogin,输入用户名和密码后可以看到,也可以正常登录。

在这里插入图片描述

  • 在自定义的页面实现记住我的功能,在login.html中增加一个记住我的多选框
<div class="field">
    <input type="checkbox" name="remember">记住我
</div>

注:如果name属性的值为"remember-me",则可以不用配.rememberMeParameter("remember")

  • 在开启记住我的方法后继续配置.rememberMeParameter("remember")
// 设置提交的记住我的input的name:.rememberMeParameter("remember"),默认是remember-me
http.rememberMe().rememberMeParameter("remember");
  • 重新运行项目,访问:http://localhost:8080/toLogin,可以看到,多了一个记住我的选择框,输入用户名和密码后,勾选记住我,登录,可以看到,成功登录。

在这里插入图片描述
在这里插入图片描述

  • 关闭浏览器,再访问:http://localhost:8080/,可以看到,会直接显示已登录后的页面。不用再重新登录。
    在这里插入图片描述
  • OK,定制首页完成!

附配置类完整代码

@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");

        // 开启登录页面,没有权限时默认到登录页面 /login
        // 定制首页:.loginPage("/toLogin")
        // 登录提交表单的请求:.loginProcessingUrl("/login")
        http.formLogin()
                .loginPage("/toLogin")
                .usernameParameter("username")
                .passwordParameter("password")
                .loginProcessingUrl("/login");

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

        // 使用自定义的login页面后,需要关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
        //http.csrf().disable();

        // 开启记住我
        // 设置提交的记住我的input的name:.rememberMeParameter("remember"),默认是remember-me
        http.rememberMe().rememberMeParameter("remember");
    }

    // 认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        // 从内存中得到用户及相应的角色。    这些数据应该从数据库中读取
        //Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
        //要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
        //spring security 官方推荐的是使用BCrypt加密方式。
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("lexiaoyuan").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
                .and()
                .withUser("beta").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值