9、Spring Boot安全

1.Spring Security简介

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

  WebSecurityConfigurerAdapter:自定义Security策略

  AuthenticationManagerBuilder:自定义认证策略

  @EnableWebSecurity:开启WebSecurity模式

  应用程序的两个主要区域是'认证'和'授权'(或者访问控制)。

  '认证'和'授权'主要区域是Spring Security 的两个目标。

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

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

2.Spring Security使用

(1).创建工程

 

(2).引入SpringSecurity

<!--security-->

<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-security</artifactId>

</dependency>

(3).导入文件

(4).SpringSecurity配置类

  HttpSecurity配置登陆、注销功能

package com.hosystem.security.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.builders.WebSecurity;

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 MySecurityConfig extends WebSecurityConfigurerAdapter{

 

    //定义授权规则

    @Override

    protected void configure(HttpSecurity http) throws Exception {

//        super.configure(http);

 

        //定制请求的授权规则

        http.authorizeRequests().antMatchers("/").permitAll()

                .antMatchers("/level1/**").hasRole("VIP1")

                .antMatchers("/level2/**").hasRole("VIP2")

                .antMatchers("/level3/**").hasRole("VIP3");

 

        //开启自动配置登录功能

        http.formLogin();

        //1. /login到登录页

        //2. 重定向到/login?error表示登录失败

        //3. 更多详细规定

    }

 

    //定义认证规则

    @Override

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

//        super.configure(auth);

 

        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())

                .withUser("tom").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2")

                .and()

                .withUser("jack").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3")

                .and()

                .withUser("lucy").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP3");

 

 

    }

}

注:如果出现There is no PasswordEncoder mapped for the id “null”

    或者 Encoded password does not look like bcrypt(Bad credentials)基本都是springsecurity版本的问题。只需要使用passwordEncoder(new BCryptPasswordEncoder())替换原来的即可。

#老版本springsecurity

auth.inMemoryAuthentication().withUser("user").password("123456").roles("VIP1");

 

#新版本springsecurity

auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())

        .withUser("tom").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2");

(5).Thymeleaf提供的SpringSecurity标签支持

[1].引入thymeleaf-extras-springsecurity5

<!--springsecurity5-->

<dependency>

   <groupId>org.thymeleaf.extras</groupId>

   <artifactId>thymeleaf-extras-springsecurity5</artifactId>

</dependency>

[2].sec:authorize使用

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org"

     xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

<h1 align="center">欢迎光临武林秘籍管理系统</h1>

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

   <h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">请登录</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="注销"

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原理介绍,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。传统事务中回滚点的使...
今天小编给大家分享的是一文解析spring中事务的传播机制,相信很多人都不太了解,为了让大家更加了解,所以给大家总结了以下内容,一起往下看吧。一定会有所收获...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区别,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看吧。Spring Cloud Netfli...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。第一步:整合pom文件,在S...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。List 坑列表 = new ArrayList(2);...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓存的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇...
本篇内容主要讲解“Spring中的@Autowired和@Resource注解怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学...
今天小编给大家分享一下SpringSecurity怎么定义多个过滤器链的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家
这篇文章主要介绍“Spring的@Conditional注解怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring的@Con...
这篇文章主要介绍了SpringCloudGateway的熔断限流怎么配置的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringCloud&nb...
今天小编给大家分享一下怎么使用Spring解决循环依赖问题的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考
这篇文章主要介绍“Spring事务及传播机制的原理及应用方法是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Sp...
这篇“SpringCloudAlibaba框架实例应用分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价
本篇内容主要讲解“SpringBoot中怎么使用SpringMVC”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习...
这篇文章主要介绍“SpringMVC适配器模式作用范围是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringMVC
这篇“导入SpringCloud依赖失败如何解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家...
这篇文章主要讲解了“SpringMVC核心DispatcherServlet处理流程是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来
今天小编给大家分享一下SpringMVCHttpMessageConverter消息转换器怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以...
这篇文章主要介绍“Spring框架实现依赖注入的原理是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring框架...
本篇内容介绍了“Spring单元测试控制Bean注入的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下