六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 112|回复: 0

java http 认证方式

[复制链接]

升级  1.67%

53

主题

53

主题

53

主题

举人

Rank: 3Rank: 3

积分
205
 楼主| 发表于 2013-2-7 19:29:35 | 显示全部楼层 |阅读模式
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <title>My JSP 'index.jsp' starting page</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 rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body>  <%   sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();   boolean authenticated = false;   String authorization = request.getHeader("authorization");   System.out.println("authorization:" + authorization);   if (authorization != null) {    if (authorization.startsWith("Basic")) {     authorization = authorization.substring(authorization     .indexOf(' ') + 1);     byte[] bytes = decoder.decodeBuffer(authorization);     authorization = new String(bytes);     String userName = authorization.substring(0, authorization     .indexOf(':'));     String password = authorization.substring(authorization     .indexOf(':') + 1);     System.out.println("userName:" + userName);     System.out.println("password:" + password);     authenticated = userName.equals("abc")     && password.equals("abc");    } else if (authorization.startsWith("Digest")) {     String userName = authorization.substring(authorization     .indexOf("username="));     userName = userName.substring("username=\"".length());     userName = userName.substring(0, userName.indexOf('"'));     String password = authorization.substring(authorization     .indexOf("response="));     password = password.substring("response=\"".length());     password = password.substring(0, password.indexOf('"'));     authenticated = userName.equals("abc")     && password     .equals("3cf1135d3b8e20dd9272d06288569a56");    }   }   if (!authenticated) {    // response.addHeader("WWW-Authenticate","Digest realm=\"Tomcat Manager Application\"");      response.addHeader("WWW-Authenticate",    "Basic realm=\"Tomcat Manager Application\"");    response.sendError(401, "Unauthorized");   } else {    out.println("hello abc");   }  %> </body></html>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表