chapter14:springboot与安全

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

Spring Boot与安全视频

Spring Security, shiro等安全框架。主要功能是”认证“和”授权“,或者说是访问控制。

认证(Authentication)是建立在一个声明主体的过程(一个主体一般指用户,设备或一些可以在你的应用程序中执行动作的其他系统)。

授权(Authorization)指确定一个主体是否允许在你的应用程序执行一个动作的过程。 为了抵达需要授权的店, 主体的身份已经有认证过程建立。

这里我们使用Spring Security练习。

1. pom.xml

创建springboot应用,导入spring-boot-starter-security等相关依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.crysw.security</groupId>
    <artifactId>springboot05-security</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot05-security</name>
    <description>springboot05-security</description>

    <properties>
        <java.version>1.8</java.version>
        <!--指定thymeleaf相关依赖的版本-->
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
        <thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version>
    </properties>

    <dependencies>
        <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>

        <!--可以在html中引用thymeleaf来获取授权和认证的相关信息-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. html页面

准备静态资源(html测试页面)
chapter14:springboot与安全,SpringBoot,spring boot,java

2.1 welcome.html

/请求转发到首页welcome.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:align="center">欢迎光临武林秘籍管理系统</h1>
    <!--没有认证-->
<div sec:authorize="!isAuthenticated()">
    <!--/login的get请求是security默认跳转登录页面的处理-->
    <!--<h2 align="center">游客您好,如果想查看武林秘籍,<a th:href="@{/login}">请登录</a></h2>-->
    <!--/userlogin的get请求是自定义跳转自己登录认证页面的处理-->
    <h2 th:align="center">游客您好,如果想查看武林秘籍,<a th:href="@{/userlogin}">请登录</a></h2>
</div>
 <!--只有登录认证了才展示注销按钮-->
<div sec:authorize="isAuthenticated()">
    <h2><span sec:authentication="name"></span>, 您好,您的角色有:<span
            sec:authentication="principal.authorities"></span></h2>
    <form th:action="@{/logout}" method="post">
        <input type="submit" value="注销"/>
    </form>
</div>

<hr>
<!--授权VIP1的模块展示-->
<div sec:authorize="hasRole('VIP1')">
    <h3>普通武林秘籍</h3>
    <ul>
        <li><a th:href="@{/level1/1}">罗汉拳</a></li>
        <li><a th:href="@{/level1/2}">武当长拳</a></li>
        <li><a th:href="@{/level1/3}">全真剑法</a></li>
    </ul>
</div>
<!--授权VIP2的模块展示-->
<div sec:authorize="hasRole('VIP2')">
    <h3>高级武林秘籍</h3>
    <ul>
        <li><a th:href="@{/level2/1}">太极拳</a></li>
        <li><a th:href="@{/level2/2}">七伤拳</a></li>
        <li><a th:href="@{/level2/3}">梯云纵</a></li>
    </ul>
</div>

<!--授权VIP3的模块展示-->
<div sec:authorize="hasRole('VIP3')">
    <h3>绝世武林秘籍</h3>
    <ul>
        <li><a th:href="@{/level3/1}">葵花宝典</a></li>
        <li><a th:href="@{/level3/2}">龟派气功</a></li>
        <li><a th:href="@{/level3/3}">独孤九剑</a></li>
    </ul>
</div>

</body>
</html>

2.2 login.html

请求/userlogin转发到自定义的登录页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 th:align="center">欢迎来到武林秘籍管理系统</h1>
<hr/>
<div th:align="center">
    <!--默认post形式的/login代表处理登录提交-->
    <!--如果定制loginPage,那么loginPage的post请求就是登录提交-->
    <form th:action="@{/userlogin}" method="post">
        用户名: <input type="text" name="uname"/>
        <br/>
        密码:<input type="password" name="pwd"/>
        <br/>
        <input type="checkbox" name="remember" id=""/> remember me
        <br/>
        <input type="submit" value="登录"/>
    </form>
</div>
</body>
</html>

2.3 level1模块页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a th:href="@{/}">返回</a>
<h1>罗汉拳</h1>
<p>罗汉拳站当秧,打起来不要慌</p>
</body>
</html>

其他模块页面一样,简单改一下内容即可。

3. security配置类

WebSecurityConfigurerAdapter为创建WebSecurityConfigurer实例提供了一个方便的基类。允许通过重写方法进行定制实现,自定义授权规则和认证规则。

@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 定制请求的授权规则
     *
     * @param http the {@link HttpSecurity} to modify
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll()
                // VIP1角色的用户才能访问level1的页面,其他同理
                .antMatchers("/level1/**").hasRole("VIP1")
                .antMatchers("/level2/**").hasRole("VIP2")
                .antMatchers("/level3/**").hasRole("VIP3");

        // 开启自动配置的登录功能
//        http.formLogin();
        http.formLogin().usernameParameter("uname").passwordParameter("pwd")
                .loginPage("/userlogin").loginProcessingUrl("/userlogin");
        // 1. 如果没有访问权限,转发到/login请求来到登录页;
        // 2. 重定向到/login?error表示登录失败。 更多详细规定
        // 3. 默认post形式的/login代表处理登录提交
        // 4.如果定制loginPage,那么loginPage的post请求就是登录提交

        // 开启自动配置的注销功能, 访问/logout表示用户注销,清空session; 注销成功后来到首页;
        http.logout().logoutSuccessUrl("/");
        // 开启记住我的功能, 将cookie发给浏览器保存,以后登录带上这个cookie,只要通过服务器端的验证就可以免登录
        // 如果点击”注销“,也会删除这个cookie
//        http.rememberMe();
        http.rememberMe().rememberMeParameter("remember");
    }

    /**
     * 定制认证规则
     *
     * @param auth the {@link AuthenticationManagerBuilder} to use
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("zhangsan").password("123456").roles("VIP1", "VIP2")
                .and().withUser("lisi").password("123456").roles("VIP2", "VIP3")
                .and().withUser("wangwu").password("123456").roles("VIP1", "VIP3");
    }
}

启动应用后,访问主页,需要先登录认证。可以看到张三有访问VIP1, VIP2权限的区域展示。
chapter14:springboot与安全,SpringBoot,spring boot,java

登录认证成功后,跳转到欢迎主页。chapter14:springboot与安全,SpringBoot,spring boot,java文章来源地址https://www.toymoban.com/news/detail-630245.html

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

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

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

相关文章

  • SpringBoot教程(一)|认识Spring Boot

    安得广厦千万间,大庇天下寒士俱欢颜,风雨不动安如山,呜呼,何时眼前突兀见此屋,吾庐独破受冻死亦足! Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需

    2024年01月16日
    浏览(34)
  • 【Spring Boot】Spring Boot项目中如何查看springBoot版本和Spring的版本

    在项目中查看默认版本有两种方式如下 Spring Boot 的最新版本支持情况: 版本 发布时间 停止维护时间 停止商业支持 3.0.x 2022-11-24 2023-11-24 2025-02-24 2.7.x 2022-05-19 2023-11-18 2025-02-18 2.6.x 2021-12-17 2022-11-24 2024-02-24 2.5.x 2021-05-20 已停止 2023-08-24 2.4.x 2020-11-12 已停止 2023-02-23 2.3.x 2020-05-

    2024年02月11日
    浏览(86)
  • SpringBoot教程(三) | Spring Boot初体验

    上篇文章我们创建了SpringBoot 项目,并且进行了简单的启动。整个项目了里其实我们就动了两个文件,一个是pom.xml负责管理springboot的相关依赖,一个是springBoot的启动类。 pom文件中通过starter的形式大大简化了配置,不像以前一样需要引入大量的依赖配置,搞不好还得解决冲突

    2024年01月16日
    浏览(36)
  • SpringBoot整理-Spring Boot与Spring MVC的区别

    Spring Boot 和 Spring MVC 是 Spring 框架的两个不同部分,它们在 Java Web 开发中扮演着各自独特的角色。理解它们之间的区别有助于更好地利用 Spring 生态系统进行有效的应用开发。 Spring MVC 定义:  Spring MVC 是基于 Model-View-Controller(模型-视图-控制器)设计模式的一个 

    2024年01月22日
    浏览(34)
  • 【SpringBoot】详细介绍Spring Boot中@Component

    在Spring Boot中,`@Component`是一个通用的注解,用于标识一个类是Spring框架中的组件。`@Component`注解是Spring的核心注解之一,它提供了自动扫描和实例化bean的功能。 具体来说, `@Component`注解的作用是将一个普通的Java类转化为Spring的组件。通过`@Component`注解标记的类会被Spring框

    2024年02月11日
    浏览(28)
  • Springboot 实践(13)spring boot 整合RabbitMq

    前文讲解了RabbitMQ的下载和安装,此文讲解springboot整合RabbitMq实现消息的发送和消费。 1、创建web project项目,名称为“SpringbootAction-RabbitMQ” 2、修改pom.xml文件,添加amqp使用jar包    !--  RabbitMQ --         dependency             groupIdorg.springframework.boot/groupId         

    2024年02月09日
    浏览(42)
  • 【SpringBoot】| Spring Boot 常见的底层注解剖析

    目录 一:Spring Boot 常见的底层注解 1. 容器功能 1.1 组件添加 方法一:使用@Configuration注解+@Bean注解 方法二:使用@Configuration注解+@Import注解  方法三:使用@Configuration注解+@Conditional注解  1.2 原生xml配置文件引入 @ImportResource注解 1.3 配置绑定 方法一:@Component注解 + @Configu

    2024年02月17日
    浏览(33)
  • 【SpringBoot】Spring Boot中类的实例化

    在Spring Boot中,类的实例化通常是由Spring框架处理的。Spring使用控制反转(IoC)和依赖注入(DI)的概念来管理类的实例化和依赖关系。 要在Spring Boot中实例化一个类,可以遵循以下几个步骤: 1. 在类上使用`@Component`、`@Service`、`@Repository`或`@Controller`等注解来标识这个类是一

    2024年02月10日
    浏览(24)
  • 【SpringBoot】| Spring Boot 概述和入门程序剖析

    目录 一:Spring Boot 入门 1. Spring能做什么? 2. SpringBoot特点 3. 如何学习SpringBoot 4. 创建Spring Boot项目 Maven的配置 入门案例: SpringBoot中几个重要的注解 5. 了解自动配置原理 依赖管理 自动配置 6. SpringBoot核心配置文件 多环境测试 自定义配置 7. SpringBoot中使用JSP(了解) 8. S

    2024年02月06日
    浏览(43)
  • 【Spring Boot】SpringBoot参数验证以及实现原理

    参数验证很重要,是平时开发环节中不可少的一部分,但是我想很多后端同事会偷懒,干脆不做,这样很可能给系统的稳定性和安全性带来严重的危害。 那么在Spring Boot应用中如何做好参数校验工作呢,本文提供了小技巧以及验证实现原理,你知道几个呢? Spring Boot 提供了内

    2023年04月16日
    浏览(45)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包