八、【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 :前端控制 ...
随机推荐
- maven的安装及环境变量配置
1.下载maven 2.解压至该路径 3. 新建环境变量MAVEN_HOME , 值为maven包点开路径 环境变量配置: 4. 编辑环境变量Path,追加%MAVEN_HOME%\bin\ 5.一路 ...
- ssm(spring,spring mvc,mybatis)框架
ssm框架各个技术的职责 spring :spring是一个IOC DI AOP的 容器类框架 spring mvc:spring mvc 是一个mvc框架 mybatis:是一个orm的持久层框架 ...
- 【HBase】集群搭建/安装部署
目录 第一步:下载对应的HBase安装包 第二步:上传压缩包并解压 第三步:修改配置文件 第四步:安装包分发到另外两台机器 第五步:三台机器创建软连接 第六步:三台机器添加环境变量 第七步:启动HBa ...
- iview input 禁止输入特殊字符 ,解决中文输入法中input把拼音输入
tips:解决了e.target中输入中文 会把拼音也输入的情况 1 html <FormItem label="角色名称" prop="roleName" ...
- Struts2-Tiles 2.5.2 升级指南和通配符拓展
最近工程从Struts2.3.18升级Struts2.5.2导致相关联的插件都需要升级到相同版本,其中tiles的变化最大. 1.web.xml上 listener org.apache.struts ...
- Dubbo对Spring Cloud说:来老弟,我要拥抱你
项目地址 https://github.com/yinjihuan/kitty-cloud 前言 Kitty Cloud 开源后有以为朋友在 GitHub 上给我提了一个 issues,问为什么项目中 ...
- vue钩子
全局钩子 const router = new VueRouter({ ... }) router.beforeEach((to, from, next) => { // ... }) 钩子是异 ...
- utf8mb4复杂昵称问题
wechat_ling wl_channel_consumer nickname wl_consumer nickname alter table wl_channel_consumer modify ...
- hdu6153KMP
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- Poj2965 冰箱的开关
#include<iostream> using namespace std; int flag; int step; ][]; ] = { }; ] = { }; void turn(i ...