Spring Security与Spring Boot集成

添加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency> <!-- h2 database测试时使用的数据库 -->
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

3.1 权限管理

基于角色的权限管理

什么是角色?

  • 代表一系列行为或责任的实体
  • 限定了角色能做什么,不能做什么
  • 用户账号往往与角色相关联

RBAC

  • 基于角色的访问控制(Role-Based Access Control)
  • 隐式访问控制
if(user.hasRole("Project Manager")){
// 显示报表按钮
} else {
// 不显示报表按钮
}
  • 显示访问控制(更加灵活)
if(user.isPermitted("projectReport:view:12345")){
// 显示报表按钮
} else {
// 不显示报表按钮
}

权限解决方案框架:

  • Apache Shiro

  • Spring Security

3.2 Spring Security实战

1)后台编码

  • 安全配置类
package com.fei.config;

import org.springframework.beans.factory.annotation.Autowired;
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; /**
* 安全配置类 Created by zxf on 2019年10月10日
*/
@EnableWebSecurity // 启用注解安全
public class SecurityConfig extends WebSecurityConfigurerAdapter { /**
* 自定义拦截策略
*/
@Override
protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll()// 都可以访问
.antMatchers("/users/**").hasRole("ADMIN")// 需要相应的角色才能访问
.and().formLogin()// 基于form表单登录验证
.loginPage("/login").failureUrl("/login-error");// 自定义登录界面
super.configure(http);
} /**
* 认证信息管理
*
* @param auth
* @throws Exception
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication()// 认证信息存储在内存中
.withUser("fei").password("123").roles("ADMIN");
} }
  • 控制器
package com.fei.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; /**
* 处理首页、登录
* Created by zxf on 2019年10月10日
*/
@Controller
public class MainController {
@GetMapping("/")
public String root() {
return "redirect:/index";
} @GetMapping("/index")
public String index() {
return "index";
} @GetMapping("/login")
public String login() {
return "login";
} @GetMapping("/login-error")
public String loginError(Model model) {
model.addAttribute("loginError", true);
model.addAttribute("errorMsg", "登录失败,用户名或密码错误!"); return "login";
}
}

Spring Security初识的更多相关文章

  1. spring security基本知识(三) 过滤详细说明

    在我们前面的文章Spring Security 初识(一)中,我们看到了一个最简单的 Spring Security 配置,会要求所有的请求都要经过认证.但是,这并不是我们想要的,我们通常想自定义应用 ...

  2. 【OAuth2.0】Spring Security OAuth2.0篇之初识

    不吐不快 因为项目需求开始接触OAuth2.0授权协议.断断续续接触了有两周左右的时间.不得不吐槽的,依然是自己的学习习惯问题,总是着急想了解一切,习惯性地钻牛角尖去理解小的细节,而不是从宏观上去掌握 ...

  3. 初识Spring Security

    本文参考或者转自:http://haohaoxuexi.iteye.com/blog/2154299 1.新建Spring Security配置文件spring-security.xml:<?x ...

  4. Spring Security教程(一):初识Spring Security

    一.概要 Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权.以 ...

  5. 214. Spring Security:概述

    前言 在之前介绍过了Shiro之后,有好多粉丝问SpringSecurity在Spring Boot中怎么集成.这个系列我们就和大家分享下有关这方面的知识. 本节大纲 一.什么是SpringSecur ...

  6. Spring Security 4.2.3 Filters 解析

    一. 熟悉一个模块的最快方法 1. 配置logback文件,打印相应的debug信息 2. 根据相应的信息,打断点查看执行结果 二.spring 使用 DelegatingFilterProxy 管理 ...

  7. Spring Security教程(二):通过数据库获得用户权限信息

    上一篇博客中,Spring Security教程(一):初识Spring Security,我把用户信息和权限信息放到了xml文件中,这是为了演示如何使用最小的配置就可以使用Spring Securi ...

  8. spring security采用自定义登录页和退出功能

    更新... 首先采用的是XML配置方式,请先查看  初识Spring security-添加security 在之前的示例中进行代码修改 项目结构如下: 一.修改spring-security.xml ...

  9. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

随机推荐

  1. 洛谷P4095新背包问题

    传送 这道题最最暴力的方法就是对于每一个询问都跑一边多重背包问题,但显然q不会那么友好的让我们用暴力过掉这道题. 考虑优化.我们可以先把裸的多重背包搞成二进制优化后的多重背包.但是复杂度依然无法接受. ...

  2. 使用事件CreateEvent注意事项 多进程同步的方法

    https://www.cnblogs.com/aakuang/p/3514658.html 使用事件CreateEvent注意事项   HANDLECreateEvent( LPSECURITY_A ...

  3. window.screenLeft&&window.screenTop&&window.screenX&&window.screenY

    http://blog.sina.com.cn/s/blog_14e2a237b0102w4i0.html window.screenLeft&&window.screenTop&am ...

  4. 2019暑假第二周(hadoop在个人电脑上的搭建)

    一,Hadoop和NoSQL数据库的学习,大多需要Linux环境. 搭建Linux环境可以分为两种方式: (1)在电脑上安装双操作系统,即同时安装Linux和Windows操作系统,在电脑启动的时候, ...

  5. idea中创建maven格式的文件方法

    其中新建的maven工程有时候不全或者出一些小问题导致新建类,或者其他文件时候找不到新建的快捷方式,下面就说一种快速设置

  6. python-应用OpenCV和Python进行SIFT算法的实现

    如下图为进行测试的q和h,分别验证基于BFmatcher.FlannBasedMatcher等的SIFT算法 代码如下: import numpy as np import cv2 from matp ...

  7. Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.3 Propositional Equivalences

    DEFINITION 1 A compound proposition that is always true,no matter what the truth values of the propo ...

  8. Go语言入门篇-命令 与 语法

    一.命令基础 1. go run : 用于运行命令源码文件(如:go run helloworld.go) 只能接受一个命令源码文件以及若干个库源码文件作为文件参数 其内部操作步骤: (1)先编译源码 ...

  9. Vim文本编辑工具

    4文本编辑工具Vim Vim是vi的升级版,编辑文本时vi不会显示颜色而vim会显示颜色. 安装vim工具 #yum  install  –y  vim-enhanced Vim有三种模式:一般模式. ...

  10. CentOS 7安装Python 2.6(与已有版本共存)

    1. 安装需要用到的包 yum install -y zlib-devel bzip2-devel openssl-devel xz-libs wget 2. 下载 Python 2.6.8 版本 w ...