Spring Security使用心得
某天,你的客户提出这样一个需求,在点击购买商品的时,如果用户没有注册,并且用户没有账号,这时用户去创建账户,然后要直接返回到想购买商品的付款页面。你会该如何基于Spring Security实现?
Spring Security 是为基于Spring的应用程序提供声明式安全保护的安全性框架。它能够在Web请求级别和方法调用级别处理身份验证和授权。因为基于Spring框架,所以Spring Security充分利用依赖注入(dependency injection)和AOP.
Spring Security提供了多种验证方式,最常见的有:XML配置和数据库验证方式。试想当我们点击购买商品并且没有登录的情况下,Spring Security 一般会跳到登录,但是登陆是由Spring帮我们实现的,所以我们跳到登陆页面后也就丢失了与商品的联系,其实这中间Spring会经历多个步骤,最后帮你验证你输入的结果。在跳进登陆页面前Spring会先调用默认 AuthenticationEntryPoint接口的方法,我们通过重载这个方法可以实现我们想进行的操作,这里就是保存用户点击的商品信息。
public class SimpleEntryPointHandler implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse httpServletResponse, AuthenticationException e) throws ServletException {
if (request.getRequestURI().contains("/payment")) {
httpServletResponse.sendRedirect("/");
}
else{
request.getSession().setAttribute("reserveItemId", request.getParameter("itemId"));
httpServletResponse.sendRedirect("/login");
}
}
}
spring-security部分配置:
<beans:bean id="simpleEntryPointHandler" class="com.trailblazers.freewheelers.security.SimpleEntryPointHandler"/>
<http auto-config="true" entry-point-ref="simpleEntryPointHandler">
<intercept-url pattern="/admin" access="ROLE_ADMIN"/>
<intercept-url pattern="/item" access="ROLE_ADMIN"/>
<intercept-url pattern="/reserve" access="ROLE_ADMIN, ROLE_USER"/>
<intercept-url pattern="/deliveryAddress" access="ROLE_ADMIN, ROLE_USER"/>
<intercept-url pattern="/userProfile" access="ROLE_ADMIN, ROLE_USER"/>
<intercept-url pattern="/userProfile/**" access="ROLE_ADMIN"/>
<intercept-url pattern="/payment" access="ROLE_ADMIN,ROLE_USER"/>
<form-login login-page="/login" default-target-url="/userProfile"
authentication-failure-url="/loginfailed"/>
<logout logout-success-url="/" />
<access-denied-handler error-page="/403" />
</http>
解决了如何在login之前进行一些操作,接下来就是解决如何在创建用户之后进行登陆。Spring Security的验证是通过用户实现AuthenticationManager接口里的authenticate方法来验证的,常见的验证方法之前也提到了。所以当用户填好数据跳转到controller得时候我们手动实现这个方法,就能手动操作spring security来帮我们进行验证,具体如下:
@Component
public class AutoLogin { @Autowired
@Qualifier("authenticationManager")
protected AuthenticationManager authenticationManager; public void autoLogin(Account account, HttpServletRequest request) {
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
account.getEmailAddress(), account.getPassword()); request.getSession(); token.setDetails(new WebAuthenticationDetails(request));
Authentication authenticatedUser = authenticationManager.authenticate(token); SecurityContextHolder.getContext(). setAuthentication(authenticatedUser);
}
}
然后 Controller可以装配这个bean 调用authenticate就可达到登录效果。
spring-security部分配置:
此配置也就是数据库验证的测试provider,它制定了spring security验证手法,并且能够以bean的方式注入到java service里面,使其变得更加灵活。
<authentication-manager alias="authenticationManager">
<authentication-provider>
<jdbc-user-service data-source-ref="myDataSource"
users-by-username-query="select email_address, password, enabled from account where upper(email_address)=upper(?)"
authorities-by-username-query="select a.email_address, ar.role from account a,
account_role ar where a.account_name = ar.account_name and upper(a.email_address)=upper(?)"
/>
</authentication-provider>
</authentication-manager>
最后,本例子只是Spring Security中的一部分,Spring Security将系统安全逻辑从业务中分离出来,充分利用面向切面编程的思想,所以是一个值得深入研究的框架。通过这个切入点我们也可以探寻spring security执行的整个流程,更了解它的精髓。
本例子代码:https://github.com/yeahyangliu/spring-security.git
Spring Security使用心得的更多相关文章
- spring boot + spring security +JWT令牌 +前后端分离--- 心得
1.前言 观看这篇随笔需要有spring security基础. 心得: 1.生成token 的变化数据是用户名和权限拼接的字符串 ,其他的固定 2.生成的token是将登录通过的用户的权限拼接的字符 ...
- spring security源码分析心得
看了半天的文档及源码,终于理出了spring-security的一些总体思路,spring security主要分认证(authentication)和授权(authority). 1.认证authe ...
- spring security 自动登录 --- 心得
1.前言 仍然是使用cookie存储登录数据,但是存储的数据 由 spring security自动创建 ,当登出后自动删除cookie, 如果不登出也仍在生命周期内,关闭浏览器再打开将会自动登录,无 ...
- spring security +MySQL + BCryptPasswordEncoder 单向加密验证 + 权限拦截 --- 心得
1.前言 前面学习了 security的登录与登出 , 但是用户信息 是 application 配置 或内存直接注入进去的 ,不具有实用性,实际上的使用还需要权限管理,有些 访问接口需要某些权限才可 ...
- 阿里架构师的这一份Spring boot使用心得:网友看到都收藏了
阿里架构师的这一份Spring boot使用心得: 这一份PDF将从Spring Boot的出现开始讲起,到基本的环境搭建,进而对Spring的IOC及AOP进行详细讲解.以此作为理论基础,接着进行数 ...
- spring boot+spring security 使用随笔
本人最近接受的新任务是从零搭一个管理系统的权限管理模块,从零开始学习了spring security,已完成绝大部分设计和开发,和大家分享一些学习和开发心得. 首先是数据库的设计,这里是 按照产品的需 ...
- Spring Security OAuth2 开发指南
官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...
- spring mvc 和spring security配置 web.xml设置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...
- SPRING SECURITY JAVA配置:Web Security
在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...
随机推荐
- 在hibernate中查询单个对象的方法,get()、load()、
查询单个对象可以直接通过Session对象来做到,其中session这个对象提过了2种获得单个对象的方法,一个是get方法和load方法,我去看这个两个方法的时候发现这两个方法的参数是一样的,使用方式 ...
- Json序列化问题
之前Json字符反序列化为C#对象时 总是写一个实体类.. 如:{"a":5,"b":10} 这种json字符串 对应的实体类为: public class R ...
- c++获取cpu信息
原文地址:http://blog.csdn.net/jamesliulyc/article/details/2028958 1.什么是cpuid指令 CPUID指令是intel IA32架构下获得CP ...
- 接口日志记录AOP实现-LogAspect
使用spring aop日志记录 所需jar包 pom.xml <!-- logger begin --> <dependency> <groupId>org.sl ...
- SQL Server 查看数据库在数据缓存(data cache)中占用的空间大小
use master go select * from sys.dm_os_buffer_descriptors go --查看数据库在数据缓存(data cache)中占用的空间大小 --由于每个数 ...
- Ubuntu 14.04 Server i386 安装 Oracle11g_11.2.0.3 RAC
文档地址:doc 文档地址:doc
- C#实现大数相加
在C#中,我们经常需要表示整数.但是,c#的基本数据类型中,最大的long也只能表示-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807之间的数 ...
- 详解MathType中如何插入特殊符号
在论文写作中,经常会用到一些特殊符号,MathType公式编辑器支持插入特殊符号,并且数量繁多,可以满足用户的需求.本教程将详解MathType如何插入特殊符号. MathType中插入特殊符号的操作 ...
- 虚幻4 - ARPG实战教程(第一季)
在广受欢迎的的<虚幻4高速开发入门>视频教程之后.我收到了许多的反馈,当中大量的同学想要一个实战类的教程.于是,我花了一段时间准备之后,推出了新的一系列实战教程. 希望以深入浅出的方式.解 ...
- linux下更改vncserver的密码
Linux下VNC配置多个桌面和修改密码 1:vncserver2:iptables -I INPUT -p tcp --dport 5901 -j ACCEPT 客户端方式3:iptables ...