SpringBoot原理分析 | 安全框架:Security

这篇具有很好参考价值的文章主要介绍了SpringBoot原理分析 | 安全框架:Security。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

SpringBoot原理分析 | 安全框架:Security,spring boot,安全,spring

💗wei_shuo的个人主页

💫wei_shuo的学习社区

🌐Hello World !


Security

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

认证和授权

  • 依赖导入
 		<!--security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
  • 认证和授权config/SecurityConfig.java
thymeleafpackage com.wei.config;


import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@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();
    }

    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("weishuo").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordthymeleafEncoder().encode("123456")).roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }
}

注销和权限控制

  • Config/SecurityConfig.java
        //开启注销功能,注销成功跳转到首页
        http.csrf().disable();   //关闭csrf功能
        http.logout().logoutSuccessUrl("/");

security-thymeleaf整合

  • 导入依赖
    <!--security-thymeleaf整合包-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>
  • index.html:命名空间导入
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
  • index.html:配置登录、未登录显示,隐藏
<!--登录、注销-->
            <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>

            </div>
  • index.html:配置角色页面动态显示
 <div class="column" sec:authorize="hasRole('vip1')">……</div>
 <div class="column" sec:authorize="hasRole('vip2')">……</div>
 <div class="column" sec:authorize="hasRole('vip3')">……</div>

Remember me

  • Config/SecurityConfig.java
        //记住我功能,自定义接受参数
        http.rememberMe().rememberMeParameter("remember");

首页定制

没有权限默认跳到登录页面,登录页定制

  • .loginPage("/toLogin"):将用户重定向到指定的登录页面进行登录;
  • .usernameParameter("username"):从用户提交的表单中获取用户名时使用的参数名;
  • .passwordParameter("password"):从用户提交的表单中获取密码时使用的参数名;
  • .loginProcessingUrl("/login"):指定表单登录提交的 URL,用于处理用户提交的登录表单数据
//没有权限默认跳到登录页面,登录页定制
        http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("password")
            .loginProcessingUrl("/login");

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝


SpringBoot原理分析 | 安全框架:Security,spring boot,安全,spring文章来源地址https://www.toymoban.com/news/detail-541379.html

到了这里,关于SpringBoot原理分析 | 安全框架:Security的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringSecurity源码分析(一) SpringBoot集成SpringSecurity即Spring安全框架的加载过程

          Spring Security是一个强大的并且高度可定制化的访问控制框架。 它基于spring应用。 Spring Security是聚焦于为java应用提供授权和验证的框架。像所有的spring项目一样,Spring Security真正的强大在于可以非常简单的拓展功能来实现自定义的需求。       在分析SpringBoot集成的Sp

    2024年02月03日
    浏览(35)
  • SpringSecurity框架快速搭建(SpringBoot整合Security)

    目录 Common类 Config类 CorsConfig(解决跨域问题) RedisConfig (Redis数据库配置) Spring Security (配置安全功能的类) expression类(Expression 类通常用于权限控制和安全策略的定义) SGExpressionRoot(判断用户是否具有某个权限) Filter类 JwtAuthenticationTokenFilter(解析token看是否放行) Handler类

    2024年02月09日
    浏览(34)
  • 【Spring Security】安全框架学习(十二)

    6.0 其他权限校验方法 我们前面都是使用@PreAuthorize注解,然后在在其中使用的是hasAuthority方法进行校验。Spring Security还为我们提供了其它方法. 例如:hasAnyAuthority,hasRole,hasAnyRole,等。 这里我们先不急着去介绍这些方法,我们先去理解hasAuthority的原理,然后再去学习其他方法就

    2024年02月07日
    浏览(35)
  • 【Spring Security】安全框架学习(八)

    3.0 权限系统的作用 例如一个学校图书馆的管理系统,如果是普通学生登录就能看到借书还书相关的功能,不可能让他看到并且去使用添加书籍信息,删除书籍信息等功能。但是如果是一个图书馆管理员的账号登录了,应该就能看到并使用添加书籍信息,删除书籍信息等功能

    2024年02月09日
    浏览(47)
  • 安全框架Spring Security是什么?如何理解Spring Security的权限管理?

    大家好,我是卷心菜。本篇主要讲解Spring Security的基本介绍和架构分析,如果您看完文章有所收获,可以三连支持博主哦~,嘻嘻。 🎁 作者简介: 在校大学生一枚,Java领域新星创作者,Java、Python正在学习中,期待和大家一起学习一起进步~ 💗 个人主页:我是一棵卷心菜的

    2024年02月02日
    浏览(43)
  • Shiro和Spring Security安全框架对比

    Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来越多,因为它相当简单。与Spring Security对比,Shiro可能没有Spring Security做的功能强大,但是在实际工作时可能并不需要那么复杂的东西,所以使用小而简单的Shiro就足够了。下面对这两个安全框架进行了对比,可以

    2024年02月10日
    浏览(29)
  • spring boot中常用的安全框架 Security框架 利用Security框架实现用户登录验证token和用户授权(接口权限控制)

    spring boot中常用的安全框架 Security 和 Shiro 框架 Security 两大核心功能 认证 和 授权 重量级 Shiro 轻量级框架 不限于web 开发 在不使用安全框架的时候 一般我们利用过滤器和 aop自己实现 权限验证 用户登录 Security 实现逻辑 输入用户名和密码 提交 把提交用户名和密码封装对象

    2024年02月06日
    浏览(39)
  • 【Spring Security】让你的项目更加安全的框架

    🎉🎉欢迎来到我的CSDN主页!🎉🎉 🏅我是Java方文山,一个在CSDN分享笔记的博主。📚📚 🌟推荐给大家我的专栏《Spring Security》。🎯🎯 👉点击这里,就可以查看我的主页啦!👇👇 Java方文山的个人主页 🎁如果感觉还不错的话请给我点赞吧!🎁🎁 💖期待你的加入,一

    2024年02月04日
    浏览(45)
  • Spring Security的API Key实现SpringBoot 接口安全

    Spring Security 提供了各种机制来保护我们的 REST API。其中之一是 API 密钥。API 密钥是客户端在调用 API 调用时提供的令牌。 在本教程中,我们将讨论如何在Spring Security中实现基于API密钥的身份验证。 API Keys 一些REST API使用API密钥进行身份验证。API密钥是一个标记,用于向API客户

    2024年03月15日
    浏览(47)
  • Spring Boot 优雅集成 Spring Security 5.7(安全框架)与 JWT(双令牌机制)

    本章节将介绍 Spring Boot 集成 Spring Security 5.7(安全框架)。 🤖 Spring Boot 2.x 实践案例(代码仓库) Spring Security 是一个能够为基于 Spring 的企业应用系统提供声明式的安全访问控制解决方案的安全框架。 它提供了一组可以在 Spring 应用上下文中配置的 Bean,充分利用了 Spring

    2024年02月12日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包