Spring Security教程第四部分-自定义登录页面

经过第三部分的描述,我们发现页面生成了一个默认的登录界面,下面先看看这个界面的源代码

使用firefox或者其它第三方浏览器都可以很容易看到页面代码,如下

<html><head><title>Login Page</title></head><body οnlοad='document.f.j_username.focus();'>
<h3>Login with Username and Password</h3><form name='f' action='/CptSecurity/j_spring_security_check' method='POST'>
 <table>
    <tr><td>User:</td><td><input type='text' name='j_username' value=''></td></tr>
    <tr><td>Password:</td><td><input type='password' name='j_password'/></td></tr>
    <tr><td colspan='2'><input name="submit" type="submit" value="Login"/></td></tr>
  </table>
</form></body></html>
可以看出来,这个登录界面像后台发送了一个j_spring_security_check的post请求

用户名和密码的name分别'j_username'和'j_password',通过表单提交请求

既然知道了页面提交的代码,那么我们自己改写登录界面就很简单了,代码如下

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登录页面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<link href="bootstrap-3.3.5-dist/css/bootstrap.css" rel="stylesheet">
	<link href="IdeCss/login.css" rel="stylesheet">
	<script src="jquery/jquery.js"></script>
	<script src="bootstrap-3.3.5-dist/js/bootstrap.js"></script>
  </head>
  
  <!-- 引用页面header.html和nav.html -->
  <body>
	
<body>
<div class="box">
		<div class="login-box">
			<div class="login-title text-center">
				<h1><small>登录</small></h1>
			</div>
			<div class="login-content ">
			<div class="form">
                         <!--设置表单的提交操作-->
                        <form action="${pageContext.request.contextPath}/CptSecurity/j_spring_security_check" method="post">
				<div class="form-group">
					<div class="col-xs-12  ">
						<div class="input-group">
							<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
							<input type="text" id="username" name='j_username' class="form-control" placeholder="用户名">
						</div>
					</div>
				</div>
				<div class="form-group">
					<div class="col-xs-12  ">
						<div class="input-group">
							<span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
							<input type="password" id="password" name='j_password' class="form-control" placeholder="密码">
						</div>
					</div>
				</div>
				<div class="form-group form-actions">
					<div class="col-xs-4 col-xs-offset-4 ">
						<button type="submit" class="btn btn-sm btn-info"><span class="glyphicon glyphicon-off"></span> 登录</button>
					</div>
				</div>
				<div class="form-group">
<!-- 					<div class="col-xs-4 link">
						<p class="text-center remove-margin"><small>忘记密码?</small> <a href="javascript:void(0)" ><small>找回</small></a>
						</p>
					</div>
					<div class="col-xs-4 link">
						<p class="text-center remove-margin"><small>还没注册?</small> <a href="javascript:void(0)" ><small>注册</small></a>
						</p>
					</div> -->
					<div class="col-xs-12">
						<p style="text-align:center;color:red">${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}</p>
					</div>
				</div>
			</form>
			</div>
		</div>
	</div>
</div>
</body>
</body>
</html>
将表单的提交地址,用户名和密码设置成和上面一样的,即可

最后,还需要修改applicationContext.xml里面的内容,让security的登录界面编程我们修改后的值,代码如下所示

<security:http auto-config="true">
		<!-- 指定登录页面 -->
		<security:form-login login-page="/IdeJsp/login.jsp"/>
</security:http>
重启tomcat,输入用户名和密码,即可,关于用户名和密码的设置可以参考上一节的配置文件设置,下一节将会介绍如何对数据库进行存储用户名和密码,最后登录页面如下所示



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值