Spring Security安全框架入门篇
一、Spring Security相关概念
1.1.、Spring Security介绍:
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架(简单说是对访问权限进行控制嘛)。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
1.2、Spring Security实现原理:
Spring Security对Web安全性的支持大量地依赖于Servlet过滤器。通过这些过滤器拦截进入请求,判断是否已经登录认证且具访问对应请求的权限。
要完成访问控制,Spring Security至少需要下面四个拦截器(调度器、认证管理器、权限资源关联器、访问决策器)进行配合完成:
<!-- mySecurityInterceptor这里我们把它命名为调度器吧 -->
<!-- 必须包含 authenticationManager,securityMetadataSource,accessDecisionManager 三个属性 -->
<!-- 我们的所有控制将在这三个类中实现 -->
<!-- 它继承AbstractSecurityInterceptor类并实现了Filter接口 -->
<bean id="mySecurityInterceptor" class="com.luo.Filter.MySecurityInterceptor">
<b:property name="authenticationManager" ref="authenticationManager" />
<b:property name="securityMetadataSource" ref="securityMetadataSource" />
<b:property name="accessDecisionManager" ref="accessDecisionManager" />
</bean>
<!-- 认证管理器,实现用户认证的入口 -->
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="myUserDetailService" />
</authentication-manager>
<!-- 在这个类中,你就可以从数据库中读入用户的密码,角色信息等 -->
<!-- 主要实现UserDetailsService接口即可,然后返回用户数据 -->
<bean id="myUserDetailService" class="com.luo.Filter.MyUserDetailService" />
<!-- 权限资源关联器,将所有的资源和权限对应关系建立起来,即定义某一资源可以被哪些角色访问 -->
<!-- 它实现了FilterInvocationSecurityMetadataSource接口 -->
<bean id="securityMetadataSource" class="com.luo.Filter.MyFilterInvocationSecurityMetadataSource" />
<!--访问决策器,决定某个用户具有的角色,是否有足够的权限去访问某个资源 -->
<!-- 它实现了AccessDecisionManager接口 -->
<bean id="accessDecisionManager" class="com.luo.Filter.MyAccessDecisionManager">
看完上面的配置,可能未必能够完全明白,下面我们再进一步说明。
(1)首先我们自定义一个过滤器(调度器,这里我们命名为mySecurityInterceptor),这个过滤器继承AbstractSecurityInterceptor类(这里先说明,本文但凡不是自定义的类或接口都是Spring Security提供的,无须深究)。 它至少包含 authenticationManager,accessDecisionManager,securityMetadataSource三个属性,我们的所有控制将在这三个类中实现。
(2)登录验证:自定义类MyUserDetailService实现UserDetailsService接口和其loadUserByUsername方法,这个方法根据用户输入的用户名,从数据库里面获取该用户的所有权限细信息(统称用户信息)。Spring Security的AuthenticationProcessingFilter拦截器调用authenticationManager,类MyUserDetailService拿到用户信息后,authenticationManager对比用户的密码(即验证用户),如果通过了,那么相当于通过了AuthenticationProcessingFilter拦截器,也就是登录验证通过。
(3)资源访问控制:MySecurityInterceptor继承AbstractSecurityInterceptor、实现Filter是必须的。登陆后,每次访问资源都会被MySecurityInterceptor这个拦截器拦截,它首先会调用MyFilterInvocationSecurityMetadataSource类的getAttributes方法获取被拦截url所需的权限,在调用MyAccessDecisionManager类decide方法判断用户是否够权限。
可能文字描述还是比较抽象,通过实例应该能让大家更加清楚其原理。
补充说明一下:
UserDetailsService在身份认证中的作用:
Spring Security中进行身份验证的是AuthenticationManager接口,ProviderManager是它的一个默认实现,但它并不用来处理身份认证,而是委托给配置好的AuthenticationProvider,每个AuthenticationProvider会轮流检查身份认证。检查后或者返回Authentication对象或者抛出异常。
验证身份就是加载响应的UserDetails,看看是否和用户输入的账号、密码、权限等信息匹配。此步骤由实现AuthenticationProvider的DaoAuthenticationProvider(它利用UserDetailsService验证用户名、密码和授权)处理。
因此,登录认证其实可以不实现UserDetailsService,而是实现AuthenticationProvider,然后在AuthenticationProvider里面获取用户输入的用户名和密码进行校验也是可以的。或者两者一起使用。
下面推荐两者一起使用的方式http://blog.sina.com.cn/s/blog_4adc4b090102uy2f.html
另外,只实现AuthenticationProvider而不实现UserDetailsService的方式,这类是重写AuthenticationProvider的authenticate方法的代码:
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String inputLoginId = authentication.getName(); //获取用户输入的用户名
String inputPasswd = authentication.getCredentials().toString(); /获取用户输入的密码
LOGGER.info("用户{}登录", inputLoginId);
try{
// 查询此用户信息
myUser myUser = null; //根据用户名到数据库里面查询用户数据
if (myUser == null ) {
throw new Exception("您输入的账号不存在");
}
if (myUser.getUserStatus() == UserStatus.locked) {
throw new Exception("您的账号已被锁定");
}
String encodedPassword = myUser.getLoginPasswd();
// 校验密码是否正确
boolean authenticated = verifyPassword(inputPasswd, encodedPassword);
if (authenticated) {
// 认证成功处理
updateLoginInfo(myUser.getLoginId(), 0, null);
} else {
// 认证失败处理
authenticateErrorProcess(portalUser);
}
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
for (MyRole myRole : myUser.allRoleList()) {
grantedAuths.add(new SimpleGrantedAuthority(myRole.getRoleCode()));
}
MyAuthUser authUser = new PortalAuthUser(inputLoginId, inputPasswd, true, true, true, true, grantedAuths);
authUser.setPortalUser(portalUser);
return new UsernamePasswordAuthenticationToken(authUser, null, authUser.getAuthorities());
}catch(Exception e){
LOGGER.warn("用户登录失败", e);
throw new Exception(" 请确认用户名或者密码是否正确);
}
}
二、Spring Security实例详细说明
本实例环境:eclipse + maven
本实例采用的主要技术:spring + springmvc + spring security
时间有限这里只对其访问控制原理进行了阐述,例子后面再补上,不过关于实例推荐参考博文:http://blog.csdn.net/u012367513/article/details/38866465,这篇文章写得非常详细!!!
这是春节前最后一篇博客了,过年回来还有另外的学习计划,可能这个例子的TODO有点遥遥无期啊……..哈哈
Spring Security安全框架入门篇的更多相关文章
- springboot集成spring security安全框架入门篇
一. :spring security的简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下 ...
- Spring Security极简入门三部曲(上篇)
目录 Spring Security极简入门三部曲(上篇) 写在前面 为什么要用Spring Security 数据库设计 demo时刻 核心代码讲解 小结 Spring Security极简入门三部 ...
- Spring Security极简入门三部曲(中篇)
目录 Spring Security极简入门三部曲(中篇) 验证流程 Authentication接口 过滤器链 AuthenticationProvider接口: demo时刻 代码讲解 小结 Sp ...
- spring boot(一):入门篇
构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring Boot(一):入门篇+前端访问后端
转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...
- Spring Security安全框架
今天来简单介绍一下Spring Security安全框架 简介 Spring Security 提供了基于javaEE的企业应有个你软件全面的安全服务.这里特别强调支持使用SPring框架构件的项目, ...
- Farseer.net轻量级开源框架 入门篇:使用前说明
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 框架性能测试 下一篇:Farseer.net轻量级开源框架 入门篇: 增.删.改. ...
- Farseer.net轻量级开源框架 入门篇:逻辑层的选择
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 入门篇:增.删.改.查操作演示 下一篇:Farseer.net轻量级开源框架 入门 ...
- Farseer.net轻量级开源框架 入门篇:分类逻辑层
导航 目 录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 缓存逻辑层 下一篇:Farseer.net轻量级开源框架 入门篇: 添加数据详解 ...
随机推荐
- 在CentOS 7+ 安装Kubernetes入门
TL;DR; 科学上网,科学上网,科学上网,重要的事情说三次.如果不会科学上网,这篇文章就没有看下去的意义.作为一个技术人员如果不愿意折腾,很难有所作为.作为一个单纯的技术人员,最好把心思放在技术上, ...
- CSS滚动条样式定制
效果图如下 <!DOCTYPE html> <!-- saved from url=(0061)http://www.xuanfengge.com/demo/201311/scrol ...
- python3全栈开发-什么是粘包、粘包现象、如何解决粘包
一.粘包现象 让我们基于tcp先制作一个远程执行命令的程序(1:执行错误命令 2:执行ls 3:执行ifconfig) 注意注意注意: res=subprocess.Popen(cmd.decode( ...
- [JSOI 2007]麻将
Description 题库链接 给你一副麻将,胡牌的条件是一对将和若干顺子和刻子组成.现在给你 \(m\) 张牌,牌共 \(n\) 种,问你听哪一张牌. \(1\leq n\leq 400,1\le ...
- [CEOI 2004]Sweet
Description 题面链接 有 \(n\) 种糖果.第 \(i\) 种糖果有 \(m_i\) 个.取出一些糖果,至少 \(a\) 个,但不超过 \(b\) 个.求方案数. \(1\leq n\l ...
- codefroces 946G Almost Increasing Array
Description给你一个长度为$n$的序列$A$.现在准许你删除任意一个数,删除之后需要修改最小的次数使序列单调递增.问最小次数.$1≤n≤200000$ExamplesInput55 4 3 ...
- ●BZOJ 1531 [POI2005]Bank notes
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1531 题解: 单调队列优化多重背包DP (弱弱的我今天总算是把这个坑给填了...) 令V[i ...
- POJ 3233 (矩阵)
题意:对于矩阵A,求A^1 + ...... + A^k 按照矩阵十大经典题的思路大致做了下. 在k为奇数时: A^( k / 2+1)+ 1) * (A^1 + ....... A^(k/2)) ...
- bzoj3944Sum
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5149 Solved: 1385[Submit][Status][Discuss ...
- hihocoder #1142 : 三分·三分求极值
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的 ...