Spring Boot 安全框架 Spring Security & Shiro

1、 Spring Security

1.1、概述

Spring Security 是一个能够为基于 Spring 的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在 Spring 应用上下文中配置的 Bean ,充分利用了 Spring IoCDI (控制反转 Inversion of Control, DI:Dependency Injection 依赖注入)和 AOP (面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

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

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

  • Authentication :认证
  • Authorization :授权

1.2、配置使用

1.2.1、导入 jar
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
1.2.2、配置类
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
   

    /**
     * 认证方法
     * @param http
     * @throws Exception
     */
    @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.logout().logoutSuccessUrl("/");
    }

    /**
     * 授权方法
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
        /**
         * 版本为 spring boot 2.1.x 可以不用使用密码加密
         * 在 spring boot 2.1.x 之上版本需要使用密码加密
         * 加密方式 .passwordEncoder(new XXXPasswordEncoder())
         */
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("admin").password(new BCryptPasswordEncoder().encode("admin")).roles("vip1","vip2","vip3")
                .and().withUser("vip1").password(new BCryptPasswordEncoder().encode("admin")).roles("vip1")
                .and().withUser("vip2").password(new BCryptPasswordEncoder().encode("admin")).roles("vip1","vip2")
                .and().withUser("vip3").password(new BCryptPasswordEncoder().encode("admin")).roles("vip1","vip2","vip3");
    }
}
1.2.3、整合 thymeleaf
  • 导入包
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

spring boot 版本为 2.1.x 则使用 thymeleaf-extras-springsecurity4

  • 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">
    <title>Title</title>
</head>
<body>
<h1>index</h1>
<!--若已登录则显示注销链接-->
<div sec:authorize="isAuthenticated()">
    <a th:href="@{/logout}">logout</a>
</div>

<!--未登录显示登录链接-->
<div sec:authorize="!isAuthenticated()">
    <a th:href="@{/login}">login
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌尘吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值