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="注销"

9、Spring Boot安全的更多相关文章

  1. 玩转spring boot——快速开始

    开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...

  2. 【微框架】之一:从零开始,轻松搞定SpringCloud微框架系列--开山篇(spring boot 小demo)

    Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...

  3. 玩转spring boot——开篇

    很久没写博客了,而这一转眼就是7年.这段时间并不是我没学习东西,而是园友们的技术提高的非常快,这反而让我不知道该写些什么.我做程序已经有十几年之久了,可以说是彻彻底底的“程序老炮”,至于技术怎么样?我 ...

  4. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  5. 玩转spring boot——AOP与表单验证

    AOP在大多数的情况下的应用场景是:日志和验证.至于AOP的理论知识我就不做赘述.而AOP的通知类型有好几种,今天的例子我只选一个有代表意义的“环绕通知”来演示. 一.AOP入门 修改“pom.xml ...

  6. 玩转spring boot——结合JPA入门

    参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...

  7. 玩转spring boot——结合JPA事务

    接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  8. 玩转spring boot——结合AngularJs和JDBC

    参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...

  9. 玩转spring boot——结合jQuery和AngularJs

    在上篇的基础上 准备工作: 修改pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  10. 玩转spring boot——MVC应用

    如何快速搭建一个MCV程序? 参照spring官方例子:https://spring.io/guides/gs/serving-web-content/ 一.spring mvc结合thymeleaf ...

随机推荐

  1. 手写webpack核心原理,再也不怕面试官问我webpack原理

    手写webpack核心原理 目录 手写webpack核心原理 一.核心打包原理 1.1 打包的主要流程如下 1.2 具体细节 二.基本准备工作 三.获取模块内容 四.分析模块 五.收集依赖 六.ES6 ...

  2. xlrd、xlwt常用命令

    # -*- coding: utf-8 -*- import xlrd import xlwt from datetime import date,datetime   def read_excel( ...

  3. nb-iot技术实现跟踪功能的应用

    在互联网和连接的世界里,nb-iot风靡一时.企业和个人正在利用nb-iot技术和nb-iot设备的可靠,快速连接能力,对其技术系统进行渐进式更改,并创建一个互联的"智能"世界. ...

  4. oracle truncate table recover(oracle 如何拯救误操作truncate的表)

     生产上肯定是容易脑袋发热,truncate一张表,立马的心跳加速,眼神也不迷糊了,搞错了,完了-- 那么,truncate表后,能不能进行恢复? truncate操作是比较危险的操作,不记录redo ...

  5. Linux 系统编程 学习 总结

    背景 整理了Liunx 关于 进程间通信的 很常见的知识. 目录 与 说明 Linux 系统编程 学习:000-有关概念 介绍了有关的基础概念,为以后的学习打下基础. Linux 系统编程 学习:00 ...

  6. 计算机网络:TCP协议建立连接的过程为什么是三次握手而不是两次?【对于网上的两种说法我的思考】

    网上关于这个问题吵得很凶,但是仔细看过之后我更偏向认为两种说的是一样的. 首先我们来看看 TCP 协议的三次握手过程 如上图所示: 解释一下里面的英文: 里面起到作用的一些标志位就是TCP报文首部里的 ...

  7. Python爬虫实战详解:爬取图片之家

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理 如何使用python去实现一个爬虫? 模拟浏览器请求并获取网站数据在原始数据 ...

  8. NOIP 2012 P1081 开车旅行

    倍增 这道题最难的应该是预处理... 首先用$set$从后往前预处理出每一个点海拔差绝对值得最大值和次大值 因为当前城市的下标只能变大,对于点$i$,在$set$中二分找出与其值最接近的下标 然后再$ ...

  9. 内网渗透 day12-免杀框架2

    免杀框架2 目录 1. IPC管道连接 2. 查看wifi密码 3. Phantom-Evasion免杀框架的运用 4. 自解压(sfx) 5. 数字签名 6. 资源替换 1. IPC管道连接 命名管 ...

  10. HTTP 抓包 ---复习一下

    1.connection 字段 2.accept 字段 3.user-agent 字段 4.host字段 等字段需要注意: HTTP事务的延时主要有以下:1).解析时延   DNS解析与DNS缓存 客 ...