Spring Security使用心得

某天,你的客户提出这样一个需求,在点击购买商品的时,若是用户没有注册,而且用户没有帐号,这时用户去建立帐户,而后要直接返回到想购买商品的付款页面。你会该如何基于Spring Security实现?java

  Spring Security 是为基于Spring的应用程序提供声明式安全保护的安全性框架。它可以在Web请求级别和方法调用级别处理身份验证和受权。由于基于Spring框架,因此Spring Security充分利用依赖注入(dependency injection)和AOP.git

  Spring Security提供了多种验证方式,最多见的有:XML配置和数据库验证方式。试想当咱们点击购买商品而且没有登陆的状况下,Spring Security 通常会跳到登陆,可是登录是由Spring帮咱们实现的,因此咱们跳到登录页面后也就丢失了与商品的联系,其实这中间Spring会经历多个步骤,最后帮你验证你输入的结果。在跳进登录页面前Spring会先调用默认 AuthenticationEntryPoint接口的方法,咱们经过重载这个方法能够实现咱们想进行的操做,这里就是保存用户点击的商品信息。github

 1 public class SimpleEntryPointHandler implements AuthenticationEntryPoint {
 2 
 3     @Override
 4     public void commence(HttpServletRequest request, HttpServletResponse httpServletResponse, AuthenticationException e) throws  ServletException {
 5         if (request.getRequestURI().contains("/payment")) {
 6             httpServletResponse.sendRedirect("/");
 7         }
 8         else{
 9             request.getSession().setAttribute("reserveItemId", request.getParameter("itemId"));
10             httpServletResponse.sendRedirect("/login");
11         }
12     }
13 }

 

spring-security部分配置: spring

 1     <beans:bean id="simpleEntryPointHandler" class="com.trailblazers.freewheelers.security.SimpleEntryPointHandler"/>
 2 
 3     <http auto-config="true" entry-point-ref="simpleEntryPointHandler">
 4         <intercept-url pattern="/admin" access="ROLE_ADMIN"/>
 5         <intercept-url pattern="/item" access="ROLE_ADMIN"/>
 6         <intercept-url pattern="/reserve" access="ROLE_ADMIN, ROLE_USER"/>
 7         <intercept-url pattern="/deliveryAddress" access="ROLE_ADMIN, ROLE_USER"/>
 8         <intercept-url pattern="/userProfile" access="ROLE_ADMIN, ROLE_USER"/>
 9         <intercept-url pattern="/userProfile/**" access="ROLE_ADMIN"/>
10         <intercept-url pattern="/payment" access="ROLE_ADMIN,ROLE_USER"/>
11         <form-login login-page="/login" default-target-url="/userProfile"
12                     authentication-failure-url="/loginfailed"/>
13         <logout logout-success-url="/" />
14         <access-denied-handler error-page="/403" />
15       </http>

 

  解决了如何在login以前进行一些操做,接下来就是解决如何在建立用户以后进行登录。Spring Security的验证是经过用户实现AuthenticationManager接口里的authenticate方法来验证的,常见的验证方法以前也提到了。因此当用户填好数据跳转到controller得时候咱们手动实现这个方法,就能手动操做spring security来帮咱们进行验证,具体以下:数据库

 

 1 @Component
 2 public class AutoLogin {
 3 
 4     @Autowired
 5     @Qualifier("authenticationManager")
 6     protected AuthenticationManager authenticationManager;
 7 
 8     public void autoLogin(Account account, HttpServletRequest request) {
 9         UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
10                 account.getEmailAddress(), account.getPassword());
11 
12         request.getSession();
13 
14         token.setDetails(new WebAuthenticationDetails(request));
15         Authentication authenticatedUser = authenticationManager.authenticate(token);
16 
17         SecurityContextHolder.getContext(). setAuthentication(authenticatedUser);
18     }
19 }

而后 Controller能够装配这个bean 调用authenticate就可达到登陆效果。编程

spring-security部分配置:安全

  此配置也就是数据库验证的测试provider,它制定了spring security验证手法,而且可以以bean的方式注入到java service里面,使其变得更加灵活。框架

 

1     <authentication-manager alias="authenticationManager">
2         <authentication-provider>
3             <jdbc-user-service data-source-ref="myDataSource"
4                                users-by-username-query="select email_address, password, enabled from account where upper(email_address)=upper(?)"
5                                authorities-by-username-query="select a.email_address, ar.role from account a,
6                                     account_role ar where a.account_name = ar.account_name and upper(a.email_address)=upper(?)"
7                     />
8         </authentication-provider>
9     </authentication-manager>

  最后,本例子只是Spring Security中的一部分,Spring Security将系统安全逻辑从业务中分离出来,充分利用面向切面编程的思想,因此是一个值得深刻研究的框架。经过这个切入点咱们也能够探寻spring security执行的整个流程,更了解它的精髓。ide

 

本例子代码:https://github.com/yeahyangliu/spring-security.git测试

相关文章
相关标签/搜索