spring-security3 进级篇I

在上一篇入门中,简单介绍了spring security3的用法,但现实中,登录页面都是用户自己定义的,而不是spring security3生产的,这个时候,我们可以自定义用户登录页面。通过分析spring security3生成的登录页面,我们可以看到,它是一个表单,表单的action,userName 和 password的name分别为 j_spring_security_check, j_username,j_password。

 

(1)针对这个情况,我们自定义登录页面login.jsp,内容如下:

 

 

Html代码   收藏代码
  1. < %@ page  language = "java"   contentType = "text/html; charset=UTF-8"   
  2.     pageEncoding = "UTF-8" % >   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  4. < html >   
  5. < head >   
  6. < meta   http-equiv = "Content-Type"   content = "text/html; charset=UTF-8" >   
  7. < title > login interface </ title >   
  8. </ head >   
  9. < body >   
  10.     < h3 > user login </ h3 >   
  11.     ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}  
  12.     < form   action = "${pageContext.request.contextPath}/j_spring_security_check"   method = "post" >   
  13.         userName:< input   type = "text"   name = "j_username" /> < br />   
  14.         password:< input   type = "password"   name = "j_password" /> < br />   
  15.         < input   type = "submit"   value = "登录" >   
  16.     </ form >   
  17. </ body >   
  18. </ html >   

 

 其 中,${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}为spring security框架自带的信息,通过配置国际化文件,实现信息的提示,展开spring security core 可以看到国际化的文件,用户可以修改其内容满足自己项目的需要,也可自定义自己的国际化文件,将spring security core 中的key-value中的value进行修改,如果采用spring自带的国际化文件,请在配置文件中写入如下的配置信息:

 

 

 

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < beans   xmlns = "http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   xmlns:aop = "http://www.springframework.org/schema/aop"   
  4.     xmlns:tx = "http://www.springframework.org/schema/tx"   xmlns:context = "http://www.springframework.org/schema/context"   
  5.     xmlns:jee = "http://www.springframework.org/schema/jee"   
  6.     xsi:schemaLocation ="  
  7.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
  8.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
  10.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  11.     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">   
  12.       
  13.     <!--****************************************************************************************-->   
  14.     <!--**********************国际化支持信息配置*************************************************-->   
  15.     <!--****************************************************************************************-->   
  16.     <!-- 国际化支持 -->   
  17.     <!-- Bean id 必须是“messageSource”,因为Spring 在装配系统Bean 时会根据这个名字进行查找-->   
  18.     < bean   id = "messageSource"   class = "org.springframework.context.support.ResourceBundleMessageSource" >       
  19.        < property   name = "basename"   value = "org.springframework.security.messages" />   
  20.      </ bean >    
  21. </ beans >   

 

 

(2)修改spring-security配置文件满足自定义方式

 

Java代码   收藏代码
  1. <?xml version= "1.0"  encoding= "UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.   xmlns:security="http://www.springframework.org/schema/security"   
  4.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.   xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  7.           http://www.springframework.org/schema/security   
  8.           http://www.springframework.org/schema/security/spring-security-3.1.xsd">   
  9.             
  10.     <!--对登录页面不进行拦截,在页面后面加*表示,该页面后面可能会带一些参数-->  
  11.     <security:http pattern="/login.jsp*"  security= "none" />  
  12.               
  13.     <!-- 保护应用程序的所有URL,只有拥有ROLE_USER才可以访问 -->  
  14.     <security:http auto-config="true" >  
  15.         <!-- login-page: 指定登录页面-->  
  16.         <security:form-login login-page="/login.jsp" />  
  17.         <security:intercept-url pattern="/**"  access= "ROLE_USER"  />  
  18.     </security:http>  
  19.       
  20.     <!--配置认证管理器,只有用户名为user,密码为opal的用户,角色为ROLE_USER可访问指定的资源 -->  
  21.     <security:authentication-manager>  
  22.         <security:authentication-provider>  
  23.                 <security:user-service>  
  24.                 <security:user name="user"   password= "opal"  authorities= "ROLE_USER" />  
  25.             </security:user-service>  
  26.         </security:authentication-provider>  
  27.     </security:authentication-manager>  
  28. </beans>  

 

 

(3)运行该程序,系统自动跳到用户自定义的界面,输错代码,点击登录,错误信息提示如下:



 (4)用户可以对密码进行加密,例如采用MD5的方式,假设用户密码为opal,其加密后为

22b5c9accc6e1ba628cedc63a72d57f8

 

 

 

此时,配置文件中的配置修改如下:

 

 

 

Xml代码   收藏代码
  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < beans   xmlns = "http://www.springframework.org/schema/beans"   
  3.   xmlns:security = "http://www.springframework.org/schema/security"   
  4.   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   
  5.   xsi:schemaLocation ="http://www.springframework.org/schema/beans  
  6.           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.           http://www.springframework.org/schema/security  
  8.           http://www.springframework.org/schema/security/spring-security-3.1.xsd">   
  9.             
  10.     <!--对登录页面不进行拦截,在页面后面加*表示,该页面后面可能会带一些参数-->   
  11.     < security:http   pattern = "/login.jsp*"   security = "none" />   
  12.               
  13.     <!-- 保护应用程序的所有URL,只有拥有ROLE_USER才可以访问 -->   
  14.     < security:http   auto-config = "true" >   
  15.         <!-- login-page: 指定登录页面-->   
  16.         < security:form-login   login-page = "/login.jsp" />   
  17.         < security:intercept-url   pattern = "/**"   access = "ROLE_USER"   />   
  18.     </ security:http >   
  19.       
  20.     <!--配置认证管理器,只有用户名为user,密码为opal的用户,角色为ROLE_USER可访问指定的资源 -->   
  21.     < security:authentication-manager >   
  22.         < security:authentication-provider >   
  23.             < security:password-encoder   hash = "md5" />   
  24.             < security:user-service >   
  25.                 < security:user   name = "user"    password = "22b5c9accc6e1ba628cedc63a72d57f8"   authorities = "ROLE_USER" />   
  26.             </ security:user-service >   
  27.         </ security:authentication-provider >   
  28.     </ security:authentication-manager >   
  29. </ beans >  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值