006.springboot-03 整合SpringSecurity安全框架

SpringSecurity(安全)

在web开发中,安全第一位。 基础使用的有: 过滤器、拦截器。

  • 功能权限

  • 访问权限

mvc-> spring->springboot 框架思想

Security 采用 AOP 横向切面思想

认识SpringSecurity

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

记住几个类:

  • WebSecurityConfigurerAdapter:自定义Security策略

  • AuthenticationManagerBuilder:自定义认证策略

  • @EnableWebSecurity:开启WebSecurity模式

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

“认证”(Authentication)

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

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

 

“授权” (Authorization)

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

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

 

导入 Security依赖

<!--spring安全框架-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

创建配置类,配置security属性

@EnableWebSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
   }
}

在配置类中设置一些。授权规则、请求权限

//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()
                // 设置 自定义登陆页面路径
                .loginPage("/tologin")
                // 设置 登陆表单提交路径
                .loginProcessingUrl("/userlogin")
                // 设置 用户名 name值
                .usernameParameter("user")
                // 设置 密码 name值
                .passwordParameter("pwd");
        // 开启记住我功能,默认保存两周时间
        http.rememberMe().
                // 设置 记住我 name值
                rememberMeParameter("remember");

        // 开启注销
        // 默认请求为 logout路径
        http.logout()
                // 注销成功后跳转的请求
                .logoutSuccessUrl("/");

        // 关闭跨站伪造请求
        http.csrf().disable();
    }

    // 认证 springboot 2.1.x 可以直接使用
    // 密码编码 passwordEncoder
    // 在 Spring 5.0+ 新增了很多加密方法
    // 认证规则
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 在内存中 配置 用户信息,角色信息
        auth.inMemoryAuthentication()
                // 设置 密码格式编码
                .passwordEncoder(new BCryptPasswordEncoder())
                .withUser("connor").password(new BCryptPasswordEncoder().encode("123")).roles("vip3", "vip2")
                // 通过and() 进行多个拼接
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("root")).roles("vip1", "vip2", "vip3")
                .and()
                .withUser("test").password(new BCryptPasswordEncoder().encode("test")).roles("vip1");
    }
}

想通过在 页面中根据不同的权限显示 其对应的用户可看见的 操作列表

通过 thymeleaf和springSecurity整合,在页面上即可实现。

导入 整合依赖

 

注意,此时的springboot版本应下降到 <spring-boot.version>2.0.9.RELEASE</spring-boot.version>

<!-- thymeleaf 整合 springSecurity
        https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity4</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

在页面上通过 security标签库 操作

<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

判断当前是否已经认证登陆

<div sec:authorize="isAuthenticated()">

获取 当前登陆的 用户名

<span sec:authentication="name"></span>

获取该用户的所拥有角色

<span sec:authentication="principal.Authorities"></span>

注销用户,实际调用的是security的注销方法。必须是 post请求

<form th:action="@{/logout}" th:method="post">
    <input type="submit" value="注销">
</form>

本版本没有使用操作数据库步骤,只是简单的使用缓存中的数据充当持久层。

 

 

 

原文地址:https://www.cnblogs.com/mt-blog/p/13374574.html