八、【spring】web应用安全设计
内容
- Spring Security
- 使用Servlet规范中的Filter保护Web应用
- 基于数据库和LDAP进行认证
关键词
8.1 理解Spring Security模块
Spring Security:是为基于Spring的应用程序提供声明式安全保护的安全性框架。Spring Security提供了完整的安全性解决方案,它能够在Web请求级别和方法调用级别处理身份认证和授权。因为基于Spring框架,所以Spring Security充分利用了依赖注入(DI)和面向切面的技术。
通过两种角度解决安全问题
- 使用Servlet规范中的Filter保护Web请求并限制URL级别的访问。
- 使用Spring AOP保护方法调用——借助于对象代理和使用通知,能够确保只有具备适当权限的用户才能访问安全保护的方法。
8.1.1 理解模块
Security被分成以下11个模块
| 模块 | 描述 |
|---|---|
| ACL | 支持通过访问控制列表(access control list,ACL)为域对象提供安全性 |
| 切面(Aspects) | 一个很小的模块,当使用Spring Security注解时,会使用基于AspectJ的切面,而不是使用标准的Spring AOP |
| CAS客户端(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安全性支持 |
应用程序的类路径下至少要包含Core和Configuration两个模块
9.1.2 简单的安全配置
Spring Security借助Spring Filter来提高各种安全性功能
Spring Security配置
package test
import .......Configuration;
import .......EnableWebSecurity;
import .......WebSecurityConfigureAdapter;
@Configuration
// 启用Web安全性
@EnableWebSecurity
// 启用Web MVC安全性
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigureAdapter {
}
以上只是写了一个类扩展了WebSecurityConfigureAdapter类,但是要是启用还需要重载WebSecurityConfigureAdapter的三个方法。
| 方法 | 描述 |
|---|---|
| configure(WebSecurity) | 通过重载,配置Spring Security的Filter链 |
| configure(HttpSecurity) | 通过重载,配置如何通过拦截器保护请求 |
| configure(AuthenticationManageBuilder) | 通过重载,配置user_detail服务 |
虽然重载了以上的方法,但是问题依然存在,我们需要
- 配置用户存储
- 指定那些请求需要认证,哪些不需要,以及提供什么样的权限
- 提供一个自定义的登陆页面,替代原来简单的默认登录页
8.2 选择查询用户详细信息的服务
Spring Security提供了基于数据存储来认证用户,它内置了多种常见的用户存储场景,如内存,关系型数据库以及LDAP,也可以编写并插入自定义的用户存储实现。
8.2.1 使用基于内存的用户存储
扩展了WebSecurityConfigureAdapter,所以重载Configure方法,并以AuthenticationManageBuilder作为传入参数
package test.config
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.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.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManageBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER").and().withUser("admin").password("password").roles(""USER","ADMIN");
}
}
代码解析:
- configure()方法中使用类使用构造者风格的接口来构建认证配置。
- 调用withUser()方法为内存用户存储添加新的用户,该方法的UserDetailsManagerConfigurer.UserDetailsBuilder,
- 添加两个用户,user具有USER角色,admin具有USER和ADMIN角色。
- roles方法是authorities方法的简写形式。
配置用户详细信息的方法
| 方法 | 描述 |
|---|---|
| accountExpired(boolean) | 定义账号是否过期 |
| accountLocked(boolean) | 定义账号是否已经锁定 |
| and() | 用来连接配置 |
| authorities(GrantedAuthority) | 授予某个用户一项或多项权限 |
| authorities(List<? extends grantedAuthority>) | 授予某个用户一项或多项权限 |
| authorities(string ....) | 授予某个用户一项或多项权限 |
| credentialsExpired(boolean) | 定义凭证是否已经过期 |
| disabled(boolean) | 定义账号是否被禁用 |
| password(String) | 用户定义的密码 |
| roles(String ...) | 授予某个用户一项或多项角色 |
8.3 拦截请求
请求不是不拦截,也不是都拦截,而是需要适度的拦截
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/spitters/me").authenticated().antMatchers(HttpMethod.POST,"/spittles").authenticated().anyRequest().permitAll();
}
保护路径配置方法列表
| 方法 | 描述 |
|---|---|
| access(String) | 如果给定的SpEL表达式计算结果为True,就允许访问 |
| anonymous() | 允许匿名用户访问 |
| authenticated() | 允许认证过的用户访问 |
| denyAll() | 无条件拒绝所有访问 |
| fullyAuthenticated() | 如果用户是完整认证的话,就允许访问 |
| hasIpAddress(String) | 如果请求来自给定IP,允许 |
| hasRole(String) | 如果用户具备给定角色的话,就允许 |
| not() | 对其他访问求反 |
| permitAll() | 无条件允许访问 |
| rememberMe() | 如果用户是通过Remember-me功能认证的,允许 |
8.3.1 强制通道安全性
使用requireChannel()方法,借助这个方法可以为各种URL模式声明所请求的通道
```java
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/spitters/me").authenticated().antMatchers(HttpMethod.POST,"/spittles").authenticated().anyRequest().permitAll().and().requiresChannel().antMatchers("/spitters/form").requireSecure();<--需要HTTPS
.requiresInsecure().antMatchers("/").requireInSecure();<--使用HTTP
}
8.3.2 防止跨站请求伪造
跨站请求伪造(cross-site request forgery,CSRF)
Spring Security通过一个同步token的方式来实现CSRF防护的功能。它将会拦截状态变化的请求(非GET、HEAD等的请求)并检查CSRF_token。如果请求中不包含CSRF token或者与服务器的token不符合则会失败,并抛出csrfException异常。意味着在所有表单中必须在一个"_csrf"的域中提交token
<form method="POST" th:action="@{/spittle}">
...
</form>
当然也可以在代码中取消该功能
.csrf.disable();即可
八、【spring】web应用安全设计的更多相关文章
- 007-shiro与spring web项目整合【一】基础搭建
一.需求 将原来基于url的工程改成使用shiro实现 二.代码 https://github.com/bjlhx15/shiro.git 中的permission_shiro 三.去除原项目拦截器 ...
- 笔记42 Spring Web Flow——Demo(2)
转自:https://www.cnblogs.com/lyj-gyq/p/9117339.html 为了更好的理解披萨订购应用,再做一个小的Demo. 一.Spring Web Flow 2.0新特性 ...
- 《SSM框架搭建》三.整合spring web
感谢学习http://blog.csdn.net/zhshulin/article/details/37956105#,还是修改了spring到最新的版本和接口开发示例 根据前一篇日志,已经有了myb ...
- Spring Web应用的最大瑕疵
众所周知, 现在的Spring框架已经成为构建企业级Java应用事实上的标准了,众多的企业项目都构建在Spring项目及其子项目之上,特别是Java Web项目,很多都使用了Spring并且遵循着We ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块
spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...
- spring web.xml 难点配置总结
web.xml web.xml是所有web项目的根源,没有它,任何web项目都启动不了,所以有必要了解相关的配置. ContextLoderListener,ContextLoaderServlet, ...
- Spring web应用最大的败笔
第一篇 介绍下IOC DI Spring主要是业务层框架,现在已经发展成为一个完整JavaEE开发框架,它的主要特点是IoC DI和AOP等概念的融合,强项在面向切面AOP.推出之初因为Ioc/AOP ...
- 菜鸟学习Spring Web MVC之二
有文章从结构上详细讲解了Spring Web MVC,我个菜鸟就不引据来讲了.说说强悍的XP环境如何配置运行环境~~ 最后我配好的环境Tomcat.Spring Tool Suites.Maven目前 ...
- 4.Spring Web MVC处理请求的流程
- 1.Spring Web MVC有什么
Spring Web MVC使用了MVC架构模式的思想,将web层进行职责解耦. 同样也是基于请求驱动的,也就是使用请求-响应模型.它主要包含如下组件: DispatcherServlet :前端控制 ...
随机推荐
- 201771010113 李婷华《面向对象程序设计(Java)》第十二周总结
一.理论知识部分 1.Java的抽象口工具箱( Abstract WindowToolkit, AWT)包含在java.awt包中,它提供了许多用来设计GUI的组件类和容器类. 2.AWT库处理用户界 ...
- nginx脚本自动安装
nginx脚本自动安装 脚本功能: 自动安装nginx 自动判别系统是否安装nginx 自定义安装nginx路径 自定义安装nginx版本. #!/bin/bash #2019年10月30日16:00 ...
- xml(3)
xml的解析方式:dom解析和sax解析 DOM解析 使用jaxp进行增删改查 1.创建DocumentBuilderFactory工厂 2.通过DocumentBuilderFactory工厂创建D ...
- iOS中的事件响应链、单例模式、工厂模式、观察者模式
学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary iOS中事件传递和相应机制 i ...
- VS2019 使用
下载 官网下载:链接 安装 1.点击下载程序,会显示这个界面 2.点击“继续”,等待安装程序安装完成 4.安装程序下载安装验证完毕,将会提示进入这个界面 5.为了方便起见,这里仅展示安装C++功能,在 ...
- Vue中import用法
1. 引入第三方插件 第三方常用插件参考https://blog.csdn.net/vbirdbest/article/details/86527886 2. 导入 css 文件 import 'iv ...
- 力扣题解-面试题58 - II. 左旋转字符串
题目描述 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部.请定义一个函数实现字符串左旋转操作的功能. 比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转 ...
- Postman学习宝典(三)
Postman 入门3 - Newman Newman 官方帮助文档地址 Newman 安装 嗯,它需要安装,因为它不是音乐播放器!Newman是为Postman而生,专门用来运行Postman编写好 ...
- c++实现lower_bound和upper_bound
#include <bits/stdc++.h> using namespace std; int a[] = {0,1,3,3,5,6,7,8,9,20,21,21,21,30,41,4 ...
- 5.6 Go 常用函数
5.6 Go 常用函数 最正确的学习模块姿势: https://golang.org/pkg/ //golang官网 程序开发常用函数 strings处理字符串相关 统计字符串长度,按字节 len(s ...