八、【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 :前端控制 ...
随机推荐
- Spring官网阅读(五)BeanDefinition(下)
上篇文章已经对BeanDefinition做了一系列的介绍,这篇文章我们开始学习BeanDefinition合并的一些知识,完善我们整个BeanDefinition的体系,Spring在创建一个bea ...
- Coursera课程笔记----Write Professional Emails in English----Week 1
Get to Know Basic Email Writing Structures(Week 1) Introduction to Course Email and Editing Basics S ...
- 【FPGA篇章七】FPGA系统任务:详述常用的一些系统函数以及使用方法
欢迎大家关注我的微信公众账号,支持程序媛写出更多优秀的文章 系统任务和系统函数是Verilog标准的一部分,都以字符"$"为开头.系统任务可划分为六类,下面分别给出一些常用任务的用 ...
- MinorGC前检查
- ActiveMQ 持久订阅者,执行结果与初衷相违背,验证离线订阅者无效,问题解决
导读 最新在接触ActiveMQ,里面有个持久订阅者模块,功能是怎么样也演示不出来效果.配置参数比较简单(配置没几个参数),消费者第一次运行时,需要指定ClientID(此时Broker已经记录离线订 ...
- 我的linux学习日记day3
ifconfig 查看网卡信息 uname 查看系统内核.版本信息 cat /etc/redhat-release uptime 查看系统负载信息 top命令的第一行信息 free 查看内存信息 f ...
- webpack指南(二)code spliting+懒加载
code spliting 把代码分离到不同的 bundle 中,然后可以按需加载或并行加载这些文件. 代码分离可以用于获取更小的 bundle,以及控制资源加载优先级,如果使用合理,会极大缩减加载时 ...
- Django分页之应用案例
项目文件: models.py(建表) from django.db import models # Create your models here. class Book(models.Model) ...
- Mybatis 强大的结果集映射器resultMap
1. 前言 resultMap 元素是 MyBatis 中最重要最强大的元素.它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC ...
- 服务治理:Spring Cloud Eureka
Spring Cloud Eureka主要负责完成微服务架构中服务治理功能. 服务治理是微服务架构中最为核心和基础模块,主要用来实现各个微服务实例的自动注册和发现. 服务注册 微服务实例启动后向注册中 ...