某天,你的客户提出这样一个需求,在点击购买商品的时,如果用户没有注册,并且用户没有账号,这时用户去创建账户,然后要直接返回到想购买商品的付款页面。你会该如何基于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使用心得的更多相关文章

  1. spring boot + spring security +JWT令牌 +前后端分离--- 心得

    1.前言 观看这篇随笔需要有spring security基础. 心得: 1.生成token 的变化数据是用户名和权限拼接的字符串 ,其他的固定 2.生成的token是将登录通过的用户的权限拼接的字符 ...

  2. spring security源码分析心得

    看了半天的文档及源码,终于理出了spring-security的一些总体思路,spring security主要分认证(authentication)和授权(authority). 1.认证authe ...

  3. spring security 自动登录 --- 心得

    1.前言 仍然是使用cookie存储登录数据,但是存储的数据 由 spring security自动创建 ,当登出后自动删除cookie, 如果不登出也仍在生命周期内,关闭浏览器再打开将会自动登录,无 ...

  4. spring security +MySQL + BCryptPasswordEncoder 单向加密验证 + 权限拦截 --- 心得

    1.前言 前面学习了 security的登录与登出 , 但是用户信息 是 application 配置 或内存直接注入进去的 ,不具有实用性,实际上的使用还需要权限管理,有些 访问接口需要某些权限才可 ...

  5. 阿里架构师的这一份Spring boot使用心得:网友看到都收藏了

    阿里架构师的这一份Spring boot使用心得: 这一份PDF将从Spring Boot的出现开始讲起,到基本的环境搭建,进而对Spring的IOC及AOP进行详细讲解.以此作为理论基础,接着进行数 ...

  6. spring boot+spring security 使用随笔

    本人最近接受的新任务是从零搭一个管理系统的权限管理模块,从零开始学习了spring security,已完成绝大部分设计和开发,和大家分享一些学习和开发心得. 首先是数据库的设计,这里是 按照产品的需 ...

  7. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

  8. spring mvc 和spring security配置 web.xml设置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  9. SPRING SECURITY JAVA配置:Web Security

    在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...

随机推荐

  1. Spring 4 官方文档学习(十一)Web MVC 框架之themes

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-themeresolver ...

  2. 做asp.net的在别人眼中都是渣渣吗?

    做asp.net的在别人眼中都是渣渣吗?

  3. iperf/netperf网络性能测试工具、Wireshark网络包分析工具

    iperf   http://www.linuxidc.com/Linux/2014-05/101160.htm netperf  http://www.linuxidc.com/Linux/2013 ...

  4. Java任务调度开源框架quartz学习

    一.quartz学习 Java框架介绍:Quartz从入门到进阶 http://edu.yesky.com/edupxpt/233/2209233.shtml 1.例子:http://javacraz ...

  5. WPF 自定义命令 以及 命令的启用与禁用

    自定义命令:     在WPF中有5个命令类(ApplicationCommands.NavigationCommands.EditingCommands.ComponentCommands 以及 M ...

  6. 超全面的JavaWeb笔记day01<HTML等>

    1.html简介 - html的操作思想(*****) 2.文字标签和注释标签 3.标题标签.水平线标签和特殊字符 4.列表标签 5.图像标签(********) 6.路径介绍(相对路径*****) ...

  7. 使用Git Hooks实现开发部署任务自动化

    前言 版本控制,这是现代软件开发的核心需求之一.有了它,软件项目可以安全的跟踪代码变更并执行回溯.完整性检查.协同开发等多种操作.在各种版本控制软件中,git是近年来最流行的软件之一,它的去中心化架构 ...

  8. python2.0_s12_day9_事件驱动编程&异步IO

    论事件驱动与异步IO 事件驱动编程是一种编程范式,这里程序的执行流由外部事件来决定.它的特点是包含一个事件循环,当外部事件发生时使用回调机制来触发相应的处理.另外两种常见的编程范式是(单线程)同步以及 ...

  9. (DCloud)用这个来写H5,好像好厉害的样子哦

    HBuilder: http://www.dcloud.io MUI: http://dev.dcloud.net.cn/mui/getting-started/ http://dev.dcloud. ...

  10. 什么是LTE?

    LTE是英文Long Term Evolution的缩写.LTE也被通俗的称为3.9G,具有100Mbps的数据下载能力,被视作从3G向4G演进的主流技术.它改进并增强了3G的空中接入技术,采用OFD ...