概述:

学习一个简单demo,会有很多疑问需要解决,是时候梳理一下概念。

1、SpringSecurity 模块组成

Spring Security 3.2 分为 11个模块

ACL:支持通过访问控制列表(access Control list, ACL)为域对象提供安全

Aspects(切面): 使用Spring Security注解时,会使用基于AspectJ的切面,而不是标准的SpringAOP

CAS Client :提供与Jasig的中心认证服务(Central Authentication Service, CAS)进行集成的功能

Configuration: 包含通过xml和java配置Spring Security 的功能支持

Core : 提供Spring Security基本库

Cryptography(加密) : 提供了加密和密码编码的功能

LDAP :支持基于LDAP进行认证

OpenID : 支持使用OpenID进行集中式认证

Remoting: 提供了对Spring Remoting 的支持

Tag Library: Spring Security的JSP标签库

Web : 提供了Spring Security基本Filter的Web安全性支持

1.2 过滤Web 请求

安全通过过滤来拦截验证,是否需要配置多个过滤器,其实只要配置一个代理过滤,它会自动找具体实现过滤器

方式一

<filter >

<filter-name>springSecurityFilterChain</filter-name>

<filter-class>

org.springframework.web.filter.DelegatingFilterProxy

</filter-class>

</filer>

方式二:

实现AbstractSecurityWebApplicationInitializer父类

package com.jack.config;import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer{}

1.3 Spring Security 配置 继承WebSecurityConfigurerAdapter

启动安全注解 @EnableWebSecurity

@Configuration
@EnableWebSecurity // 启动web安全控制
public class SecurityConfig extends WebSecurityConfigurerAdapter{@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{auth.inMemoryAuthentication().withUser("jack").password("123456").roles("USER");auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");auth.inMemoryAuthentication().withUser("dba").password("123456").roles("DBA");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')").antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')").and().formLogin();}}

重写configure入参不同,如下所示

WebSecurity、HttpSecurity、AuthenticationManagerBuilder

配置用户详细信息的方法

accountExpired(boolean) 定义账号是否已经过期

accountLocked(boolean) 定义账号是否已经锁定

and() 用来连接配置

authorities(GrantedAuthority...) 授予某个用户一项或多项权限

authorities(List <? extends GrantedAuthority>) 授予某个用户一项或多项权限

authorities(String ...) 授予某个用户一项或多项权限

credentialsExpired(boolean) 定义凭证是否已经过期

disabled(boolean) 定义账号是否已被禁用

password(String) 定义用户密码

roles(String...) 授予某个用户的一项或多项角色

1.4、基于数据库表进行认证

用到方法是 jdbcAuthentication()

@AutowiredDataSource dataSource;@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{auth.jdbcAuthentication().dataSource(dataSource);}

自定查询语句

@AutowiredDataSource dataSource;@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("select username, password, true "+"from Spitter where username=?").authoritiesByUsernameQuery("select username,'ROLE_USER' from Spitter where username=?").passwordEncoder(new StandardPasswordEncoder("532222"));}

1.5、基于LDAP 服务器

auth.ldapAuthentication().userSearchBase("ou=people").userSearchFilter("(uid={0})").groupSearchBase("ou=groups").groupSearchFilter("member={0}").passwordCompare().passwordEncoder(new Md5PasswordEncoder()).passwordAttribute("passcode");

如果是远程服务。通过contextSource().url("输入地址");

auth.ldapAuthentication().userSearchBase("ou=people").userSearchFilter("(uid={0})").groupSearchBase("ou=groups").groupSearchFilter("member={0}").contextSource().url("ldap://网址")

1.6 非关系型数据库,可以实现 UserDetailsService 接口

重写loadUserByUsername(String username) 方法

package com.jack.data;import java.util.ArrayList;
import java.util.List;import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;public class SpitterUserService implements UserDetailsService {private final SpitterRepository spitterRepository;public SpitterUserService(SpitterRepository spitterRepository) {this.spitterRepository = spitterRepository;}@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {Spitter spitter = spitterRepository.findByUsername(username);if(spitter != null){List<GrantedAuthority> authorities = new ArrayList <GrantedAuthority>();authorities.add(new SimpleGrantedAuthority("ROLE_SPITTER"));return new User(spitter.getUsername(),spitter.getPassword(),authorities);}throw new UsernameNotFoundException("User '"+ "username " + "' not found");}}

总结:

1、不关心数据如何来,只要返回一个User对象就行

配置 auth.userDetailsService(new SpitterUserService(spitterRepository));

1.7 拦截请求

@Override
protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')")
.and().formLogin();
}

antMatchers() 是Ant风格,也支持regex风格

.regexMatchers()

用来定义如何保护路径的配置方法

access(String) 如果给定的SpEL表达式计算结果为true, 就允许访问

anonymous() 允许匿名用户访问

authenticated() 允许认证过的用户访问

denyAll() 无条件拒绝所有访问

fullyAuthenticated() 如果用户是完整的话(不是通过Remember-me 功能认证的),就能访问

hasAnyAuthority(String...) 如果用户具备给定权限中的某一个的话,就允许访问

hasAnyRole(String...) 如果用户具备给定角色中的某一个的话,就允许访问

hasAuthority(String) 如果用户具备给定权限的话,就允许访问

hasIpAddress(String) 如果请求来自给定IP地址的话,就允许访问

hasRole(String) 如果用户具备给定角色的话,就允许访问

not() 对其他访问方法的结果取反

permitAll () 无条件允许访问

rememberMe() 如果用户是通过Remember-me功能认证的,就允许访问

1.8 强制通道的安全性

HTTPS 加密发送

1.9 防止跨站请求伪造

Cross-site request forgery (CSRF)

通过同步token来解决, 也就是在表单中包含_csrf域

总结:

Spring-Security 可以完成很多验证相关的事情,具体需要慢慢研究

第9章 保护Web应用----概念梳理相关推荐

  1. Web Service概念梳理

    计算机技术难理解的很多,Web Service 对我来说就是一个很难理解的概念:为了弄清它到底是什么,我花费了两周的时间,总算有了一些收获,参考了不少网上的资料,但有些概念说法不一.我以w3c和 一些 ...

  2. JavaWeb --第一章Web基本概念

    JavaWeb --第一章Web基本概念 文章目录 基本概念 前言 web开发: web应用程序 静态web 动态web web服务器 技术讲解 web服务器 基本概念 前言 web开发: web,网 ...

  3. WCF4.0进阶系列--第四章 保护企业内部的WCF服务(转)

    http://www.cnblogs.com/yang_sy/archive/2011/05/24/2054834.html [摘要] 安全是任何系统至关重要的一个方面,尤其当该系统由分布式的程序和服 ...

  4. 微机原理概念梳理(考研/保研面试)

    微型计算机原理,常见概念及知识点整理(保研面试用). 目录 第1章 微型计算机基础 第2章 Intel系类微处理器 第3章 80486微处理器的指令系统 第4章 汇编语言程序设计 第5章 存储系统 第 ...

  5. IA-32系统编程指南 - 第三章 保护模式的内存管理【1】

    第三章 保护模式的内存管理[1] [作者:lion3875 原创文章 参考文献<Intel 64 and IA-32 system programming guide>] IA-32保护模 ...

  6. [DOM Event Learning] Section 2 概念梳理 什么是事件 DOM Event

    [DOM Event Learning] Section 2 概念梳理 什么是事件 DOM Event 事件 事件(Event)是用来通知代码,一些有趣的事情发生了. 每一个Event都会被一个Eve ...

  7. 【数据结构总结】第一章:数据结构基本概念

    [数据结构总结]第一章:数据结构基本概念 本文主要是以思维导图的形式概括数据结构第一章的精华内容,基本不会用到文字性的内容,目的是为了给大家梳理每个重要的知识点的相关概念,方便大家在复盘的时候快速阅读 ...

  8. DreamFactory 第8章 保护您的DreamFactory环境

    DreamFactory 第8章 保护您的DreamFactory环境 DreamFactory平台建立在Laravel框架之上.Laravel是一个令人惊叹的基于PHP的框架,在短短几年内,它变得越 ...

  9. 使用这些 HTTP 头保护 Web 应用

    使用这些 HTTP 头保护 Web 应用 摘要: 安全是个大学问. 这是关于web安全性系列文章的第 三 篇,其它的可点击以下查看: Web 应用安全性: 浏览器是如何工作的 Web 应用安全性: H ...

  10. 【山外笔记-计算机网络·第7版】第10章:计算机网络重要概念

    本文下载地址: [学习笔记]第10章_计算机网络重要概念.pdf 教材:<计算机网络·第7版> 作者:谢希仁 时间:2020.04.17 第01章:计算机网络概述 1.计算机网络(可简称为 ...

最新文章

  1. pandas计算滑动窗口中的中位数实战(Rolling Median of a Pandas Column):计算单数据列滑动窗口中的中位数、计算多数据列滑动窗口中的中位数
  2. 算法导论之红黑树的学习
  3. OpenGL保守光栅化
  4. python pandas 拿取表格中两个列_在家憋着也是憋着,不如来学习一下python数据聚合的方法...
  5. mac好用的软件 小总结 Alfred
  6. Android FrameWork浅识
  7. 机器人开发--OS系统介绍
  8. Java学习需要多长时间?
  9. 1人30天44587行代码,分享舍得网开发经验【修订版】
  10. MV* 模式梳理与理解(还原真实的 MV* 模式)
  11. 初音未来音乐计算机教程,PSP《初音未来:歌姬计划》原创PV制作简易图文教程...
  12. 猿辅导、掌门教育悄然转身,发力素质教育
  13. 豪华金色粒子动态logo展示AE视频模板
  14. 队列的应用--火车车厢重排列
  15. PJzhang:贷款逾期与失信被执行人
  16. 自动控制原理笔记-根轨迹法
  17. logit回归模型_详解 Logit/Probit 模型中的 completely determined 问题
  18. 【测绘程序设计】——潮汐调和分析
  19. 如何在prometric(普尔文)网站上用信用卡预约微软考试
  20. 用MATLAB来做智能小车的建模与仿真

热门文章

  1. 计算机考研国家线好过,考研国家线真的很好过吗?
  2. python后端 工作 知乎_[Python]知乎后端实习生面试心得
  3. 1 12c语言,1.4.1 Oracle数据库12c中PL/SQL的新特性
  4. javascript当中onblur和onfocus用法
  5. browserquest php安装,H5多人联机网游《Browserquest》源码 node.js版本+php版本
  6. kali中清除历史命令
  7. 3DES加密(iOS,Android,Java)
  8. android 添加日历事件,android 本地日历插入事件
  9. crate部署(crateDB)
  10. ONLYOFFICE权限开发之二