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轻量级开源框架 入门篇: 添加数据详解 ...
随机推荐
- requests-证书验证
import requests #response = requests.get('https://www.12306.cn') #print(response.status_code) #以上会显示 ...
- Spring MVC的核心流程(步骤)
具体步骤: 1.客户端发送请求先要经过前端控制器,请求被Spring 前端控制器DispatcherServlet获取,如详细图第一步:DispatcherServlet对请求URL进行解析(比如我们 ...
- 【基础】在css中绘制三角形及相关应用
简言 本文简要阐述了用CSS边框的方法在页面上绘制三角形,包括几种典型的三角形绘制,还介绍了几个简单的应用场景.利用边框绘制三角形方法只是众多方案中的一种,大家根据项目实际,选用最适宜项目的方案. 1 ...
- vue的入门/简介
vue 特点 1. 响应的数据绑定/响应式编程 2. 组件化 vue优点 1. 轻量级的框架 2. 简单易学 3. 双向数据绑定 4. 组件化 5. 视图,数据,结构分离 6. 虚拟DOM 7. ...
- NoClassDefFoundError && ClassNotFoundException
两种错误都是涉及类加载问题,类层次结构如下: NoClassDefFoundError是系统错误,ClassNotFoundException是系统异常,可以捕获. NoClassDefFoundEr ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- Docker入门之---ENTRYPOINT和CMD
1. 前言 ENTRYPOINT 和CMD 都是Dockerfile 中的命令,两者都是用来:指定默认的容器的主进程的启动命令.那么接下来就介绍一下这两者在Dockerfile 中,以及Docker ...
- NOIP2014-5-17模拟赛
Problem 1 双色球(ball.cpp/c/pas) [题目描述] 机房来了新一届的学弟学妹,邪恶的chenzeyu97发现一位学弟与他同名,于是他当起了善良的学长233 "来来来,学 ...
- 2015 多校联赛 ——HDU5350(huffman)
Problem Description MZL is a mysterious mathematician, and he proposed a mysterious function at his ...
- 习题9-4 uva 1630
题意: 给你一串数字,要求你对其进行折叠使其长度最短. 折叠情况:全是一个字母 & 重复的字符串 AAAAAAAAAABABABCCD --> 9(A)3(AB)CCD NEE ...