Spring Security初识
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初识的更多相关文章
- spring security基本知识(三) 过滤详细说明
在我们前面的文章Spring Security 初识(一)中,我们看到了一个最简单的 Spring Security 配置,会要求所有的请求都要经过认证.但是,这并不是我们想要的,我们通常想自定义应用 ...
- 【OAuth2.0】Spring Security OAuth2.0篇之初识
不吐不快 因为项目需求开始接触OAuth2.0授权协议.断断续续接触了有两周左右的时间.不得不吐槽的,依然是自己的学习习惯问题,总是着急想了解一切,习惯性地钻牛角尖去理解小的细节,而不是从宏观上去掌握 ...
- 初识Spring Security
本文参考或者转自:http://haohaoxuexi.iteye.com/blog/2154299 1.新建Spring Security配置文件spring-security.xml:<?x ...
- Spring Security教程(一):初识Spring Security
一.概要 Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权.以 ...
- 214. Spring Security:概述
前言 在之前介绍过了Shiro之后,有好多粉丝问SpringSecurity在Spring Boot中怎么集成.这个系列我们就和大家分享下有关这方面的知识. 本节大纲 一.什么是SpringSecur ...
- Spring Security 4.2.3 Filters 解析
一. 熟悉一个模块的最快方法 1. 配置logback文件,打印相应的debug信息 2. 根据相应的信息,打断点查看执行结果 二.spring 使用 DelegatingFilterProxy 管理 ...
- Spring Security教程(二):通过数据库获得用户权限信息
上一篇博客中,Spring Security教程(一):初识Spring Security,我把用户信息和权限信息放到了xml文件中,这是为了演示如何使用最小的配置就可以使用Spring Securi ...
- spring security采用自定义登录页和退出功能
更新... 首先采用的是XML配置方式,请先查看 初识Spring security-添加security 在之前的示例中进行代码修改 项目结构如下: 一.修改spring-security.xml ...
- Spring Security OAuth2 开发指南
官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...
随机推荐
- Git-Runoob:Git 工作流程
ylbtech-Git-Runoob:Git 工作流程 1.返回顶部 1. Git 工作流程 本章节我们将为大家介绍 Git 的工作流程. 一般工作流程如下: 克隆 Git 资源作为工作目录. 在克隆 ...
- spring hibernate 事务整合 使用测试类 事务不自动提交的问题!!!
使用JUnit 测试hibernate 事务管理的时候应注意 ,测试类完成是默认回滚的. 所以只能查询数据库却不能增删改数据库. 应该在测试类上面加上注解 @Rollback(false) 表似默认 ...
- nginx启动脚本和配置文件
1.编写Nginx启动脚本,并加入系统服务 vim /etc/init.d/nginx并在其中写入如下内容:#!/bin/bash# chkconfig: - 30 21# description: ...
- apache-httpd2.2编译安装
1.下载源码包 wget http://mirrors.hust.edu.cn/apache/httpd-2.2.32.tar.gz2.解压源码包 tar -zxvf httpd-2.2.32.tar ...
- 系统分析与设计HW1
软件工程的定义 1993年,电气电子工程师学会(IEEE)给出了一个定义:"将系统化的.规范的.可度量的方法用于软件的开发.运行和维护的过程,即将工程化应用于软件开发中". 阅读经 ...
- .Net 逆向 Reflector之reflexil使用
网上下载了一款商用的教育培训类软件,是用.Net写的,标榜的是免费的,但是只能试用一个月,商家很精明,用此方法推广招揽客户,但是公司在这一块却没有预算购买,一开始就想着既然是商用软件,安全机制做的肯定 ...
- 使用python脚本部署mariadb主从架构
环境准备 一个脚本自动部署master服务 另一个部署slave服务 关闭主从节点的防火墙 以及事先设置好root远程登陆的权限. grant all on *.* to root@'%' iden ...
- sql语句实现行转列的3种方法实例
sql语句实现行转列的3种方法实例 一般在做数据统计的时候会用到行转列,假如要统计学生的成绩,数据库里查询出来的会是这样的,但这并不能达到想要的效果,所以要在查询的时候做一下处理,下面话不多说了,来一 ...
- 模态框——angular
ui-bootstrap-tpls.js库 $uibModal服务 $uibModalInstance服务 一.在angular中应用modal $uibModal 使用方法:直接注入到控制器中. . ...
- suitecrm配置(nginx设置)
suitecrm配置在nginx下的一些设置 server { listen 88; server_name 192.168.2.253; #charset koi8-r; #access_log / ...