基本认证摘要认证都只能使用浏览器自带的登录框而不能使用自定义的登录页面。如果必须使用自定义的登录页面,则可以选择基于表框的认证方式。

基于表框的认证的配置与基本认证和摘要认证的差别在于部署描述符中 <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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 报错:严重: 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 [ ...

  4. servlet+jsp+java实现Web 应用

    servlet+jsp+java实现Web 应用 用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中.程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环 ...

  5. javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  6. [springMVC]javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean

    问题描述: 页面使用标签<form:form>进行提交时,出现[springMVC]javax.servlet.jsp.JspTagException: Neither BindingRe ...

  7. 基于Servlet+jsp的web计算器

    基于Servlet+jsp的web计算器 这次老大为了让我们自己复习web中页面的跳转给布置得任务 天下代码一大抄,关键看你怎么抄 首先我想到的计算算法不是什么堆栈,是简单的(其实很复杂,但是我就只需 ...

  8. Servlet+JSP例子

    前面两节已经学习了什么是Servlet,Servlet接口函数是哪些.怎么运行.Servlet生命周期是什么?  以及Servlet中的模式匹配URL,web.xml配置和HttpServlet.怎么 ...

  9. JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册

    一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...

  10. Java——分页 Servlet + Jsp+Jdbc 有点瑕疵

    1.创建数据库,插入多条数据 2.java连接DB 3.Person类: package com.phome.po; public class Person { private int id; pri ...

随机推荐

  1. ASP.NET面试题总结

    1.ASP.NET中的身份验证有那些?你当前项目采用什么方式验证请解释ASP.NET身份验证模式包括Windows.Forms(窗体).Passport(护照)和None(无). 1.Windows身 ...

  2. POJ 3668 Game of Lines (暴力,判重)

    题意:给定 n 个点,每个点都可以和另一个点相连,问你共有多少种不同斜率的直线. 析:那就直接暴力好了,反正数也不大,用set判重就好,注意斜率不存在的情况. 代码如下: #include <c ...

  3. IIS6的SSL配置,如何配置SSL到登陆页,如何将SSL证书设置成受信任的证书

    一. 申请证书1. 到受信任的机构申请 略 2. 到自建的证书服务器申请 a. 安装证书服务 通过控制面板中的“添加/删除程序”,选择“添加/删除Windows组件”.在Windows组件向导中找到“ ...

  4. MVC神韵---你想在哪解脱!(十三)

    维护模型与数据库结构之间的差别 现在我们已经将应用程序修改完毕,在Movie数据模型中添加了一个Rating属性.现在让我们重新运行应用程序,打开“http://localhost:xx/Movies ...

  5. IOS Note - Core NS Data Types

    NSString (Immutable)NSMutableString (rarely used)NSNumberNSValueNSData (bits)NSDateNSArray (Immutabl ...

  6. 图的深度优先搜索算法DFS

    1.问题描写叙述与理解 深度优先搜索(Depth First Search.DFS)所遵循的策略.如同其名称所云.是在图中尽可能"更深"地进行搜索. 在深度优先搜索中,对最新发现的 ...

  7. 【Github教程】史上最全github用法:github入门到精通

    原文 http://www.eoeandroid.com/thread-274556-1-1.html [初识Github] 首先让我们大家一起喊一句"Hello Github". ...

  8. 隐藏gvim中的工具栏和菜单栏

    在vim的配置文件.vimrc中添加如下代码: "Toggle Menu and Toolbar set guioptions-=m set guioptions-=T map <si ...

  9. leetcode -- Largest Rectangle in Histogram TODO O(N)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  10. 《C++ 并发编程》- 第1章 你好,C++的并发世界

    <C++ 并发编程>- 第1章 你好,C++的并发世界 转载自并发编程网 – ifeve.com 本文是<C++ 并发编程>的第一章,感谢人民邮电出版社授权并发编程网发表此文, ...