利用JSP在Web页面中实现Auth认证

类别:Java 点击:0 评论:0 推荐:

代码如下:

<%@ page import="java.sql.*,javax.sql.*,javax.naming.*,com.maxcard.util.*"%>
<html>
<head>
<title> New Document </title>

</head>

<body>
<%
if(request.getHeader("Authorization") == null){
response.setStatus(401);
response.setHeader("WWW-authenticate","Basic realm=\"www.maxcard.com\"");
}else{

//取得输入的信息
String encoded=(request.getHeader("Authorization"));

//利用Base64作编码的转化
String up = StringUtil.decodeBase64(encoded);

String user="";
String password="";


if(up!=null){

//取出用户名和密码


user=up.substring(0,up.indexOf(":"));
password=up.substring(up.indexOf(":")+1);
}


if(user.equals("david")&&password.equals("maxcard")){
out.print("认证成功");
}else{
out.print("认证失败");
}
}
%>
</body>
</html>

相关工具包下载: http://www.bcxy.com/java/dbutil.htm

本文地址:http://com.8s8s.com/it/it15069.htm