Servlet & JSP - Form-based Authentication
基本认证和摘要认证都只能使用浏览器自带的登录框而不能使用自定义的登录页面。如果必须使用自定义的登录页面,则可以选择基于表框的认证方式。
基于表框的认证的配置与基本认证和摘要认证的差别在于部署描述符中 <login-config/> 元素的设置不同:
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/login.html</form-error-page>
</form-login-config>
</login-config>
基于表单的认证方式需将 auth-method 置为 FORM 同时配置 form-login-config 元素。form-login-config 元素有两个子元素:form-login-page 元素与 form-error-page 元素。当访问受保护的资源时,如果用户还未认证,servlet 容器则发出 form-login-page 指定的页面。如果验证失败的话,servlet 容器发出 form-error-page 指定的元素。可以将 form-login-page 与 form-error-page 同时都指定为登录页面,这样当验证错误时用户可以继续输入用户名和密码。
下面是一个简单的登录页面 login.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login Form</h1>
<form action="j_security_check" method="post">
<div>
User Name: <input name="j_username"/>
</div>
<div>
Password: <input name="j_password" type="password" />
</div>
<div>
<input type="submit" value="Login" />
</div>
<form>
</body>
</html>
当在浏览器键入受保护的资源时,servlet 容器发送指定的登录页面:
如果输入正确的用户名和密码时,则可以访问到受保护的资源。可以使用 HttpServletRequest.getUserPrincipal 获取包含用户登录信息的 java.security.Principal 对象。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html>
<head>
<title>Photo</title>
</head>
<body>
Hello, ${ pageContext.request.userPrincipal.name }!
</body>
</html>
成功登录后,访问受保护的资源:
Servlet & JSP - Form-based Authentication的更多相关文章
- Developing a Custom Membership Provider from the scratch, and using it in the FBA (Form Based Authentication) in SharePoint 2010
//http://blog.sharedove.com/adisjugo/index.php/2011/01/05/writing-a-custom-membership-provider-and-u ...
- configure JDBCRealm JAAS for mysql and tomcat 7 with form based authentication--reference
Hello all, In this tutorial we are going to configure JDBCRealm JAAS for tomcat 7 and mysql database ...
- 报错:严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [/index.jsp (line: 13, column: 20) No tag "textfiled" defined in tag library imported with prefix
严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [ ...
- servlet+jsp+java实现Web 应用
servlet+jsp+java实现Web 应用 用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中.程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环 ...
- javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- [springMVC]javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean
问题描述: 页面使用标签<form:form>进行提交时,出现[springMVC]javax.servlet.jsp.JspTagException: Neither BindingRe ...
- 基于Servlet+jsp的web计算器
基于Servlet+jsp的web计算器 这次老大为了让我们自己复习web中页面的跳转给布置得任务 天下代码一大抄,关键看你怎么抄 首先我想到的计算算法不是什么堆栈,是简单的(其实很复杂,但是我就只需 ...
- Servlet+JSP例子
前面两节已经学习了什么是Servlet,Servlet接口函数是哪些.怎么运行.Servlet生命周期是什么? 以及Servlet中的模式匹配URL,web.xml配置和HttpServlet.怎么 ...
- JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- Java——分页 Servlet + Jsp+Jdbc 有点瑕疵
1.创建数据库,插入多条数据 2.java连接DB 3.Person类: package com.phome.po; public class Person { private int id; pri ...
随机推荐
- memcached全面剖析–4. memcached的分布式算法
memcached的分布式 正如第1次中介绍的那样, memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能. 服务器端仅包括 第2次. 第3次 前坂介绍的内存存储功能,其实现 ...
- UIPanGestureRecognizer
http://blog.csdn.net/huifeidexin_1/article/details/8282035 UIGestureRecognizer是一个定义基本手势的抽象类,具体什么手势,在 ...
- Unity3D中关于场景销毁时事件调用顺序的一点记录
先说一下我遇到的问题,我弄了一个对象池管理多个对象,对象池绑定在一个GameObject上,每个对象在OnBecameInvisible时会进行回收(即移出屏幕就回收),但是当场景切换或停止运行程序时 ...
- main函数是个什么东西
习惯的main函数有无参和两个参数的版本,那么main函数只能这么写吗? 好奇写了一个bug版本的main,结果是呵呵 #include <iostream> ...
- 【转】Android 4.4前后版本读取图库图片和拍照完美解决方案
http://blog.csdn.net/zbjdsbj/article/details/42387551 4.3或以下,选了图片之后,根据Uri来做处理,很多帖子都有了,我就不详细说了.主要是4.4 ...
- TC SRM 664 div2 A BearCheats 暴力
BearCheats Problem Statement Limak is an old brown bear. Because of his bad eyesight he sometime ...
- iOS 2D绘图详解(Quartz 2D)之路径(stroke,fill,clip,subpath,blend)
Stroke-描边 影响描边的因素 线的宽度-CGContextSetLineWidth 交叉线的处理方式-CGContextSetLineJoin 线顶端的处理方式-CGContextSetLine ...
- MS509Team----------------Cknife
http://www.ms509.com/ http://www.freebuf.com/sectool/98681.html 中国蚁剑
- Why Python is Slow
Why Python is Slow: Looking Under the Hood https://jakevdp.github.io/blog/2014/05/09/why-python-is-s ...
- [带你飞]一小时带你学会Python
1.面向的读者: 具有Javascript经验的程序猿. 2 快速入门2.1 Hello world 安装完Python之后,打开IDLE(Python GUI) , 该程序是Python语言解释器, ...