Spring Security教程(3)---- 自定义登录页面

在项目中我们肯定不能使用Spring自己生成的登录页面,而要用我们自己的登录页面,下面讲一下如何自定义登录页面,先看下配置

[html]  view plain copy
  1. <sec:http auto-config="true">  
  2.       
  3.     <sec:intercept-url pattern="/app.jsp" access="ROLE_SERVICE"/>  
  4.     <sec:intercept-url pattern="/**" access="ROLE_ADMIN"/>  
  5.       
  6.     <sec:form-login login-page="/login.jsp" authentication-failure-url="/login.jsp"  
  7.         default-target-url="/index.jsp"/>  
  8.           
  9. </sec:http>  

使用form-login配置来指定我们自己的配置文件,其中

login-page:登录页面

authentication-failure-url:登录失败后跳转的页面

default-target-url:登录成功后跳转的页面


在登录页面中

表单提交地址为:j_spring_security_check

用户名的name为:j_username

密码的name为:j_password

提交方式为POST

重启Tomcat后,再次打开项目发现登录页面已经变成了我们自己的登录页面,如下图

Spring Security教程(3)---- 自定义登录页面_第1张图片

如果提示页面循环的错误,是因为没有设置登录页面不需要验证,增加如下配置就可以了

[html]  view plain copy
  1. <security:http pattern="/login.jsp" security="none" />    

输入用户名密码后跳转到了我们指定的页面


注:重启Tomcat有时候并不会使Session失效,在测试的时候可能会出现,明明重启了Tomcat可以访问资源时却没有跳到登录页面。所以需要重启浏览器再重试就可以了。

你可能感兴趣的:(Spring Security教程(3)---- 自定义登录页面)