`

编程实现basic客户端认证

阅读更多
下面我们来示范一下如何使用basic认证。假设我们在basic.jsp中需要远程调用http://localhost:8080/ch104 /admin.jsp的内容。这时为了能够通过Spring Security的权限检测,我们需要在请求的头部加上basic所需的认证信息。

String username = "admin";
String password = "admin";
byte[] token = (username + ":" + password).getBytes("utf-8");
String authorization = "Basic " + new String(Base64.encodeBase64(token), "utf-8");

URL url = new URL("http://localhost:8080/ch104/admin.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", authorization);


我们先将用户名和密码拼接成一个字符串,两者之间使用“:”分隔。

然后使用commons-codec的Base64将这个字符串加密。在进行basic认证的时候Spring Security会使用commons-codec把这段字符串反转成用户名和密码,再进行认证操作。

下一步为加密后得到的字符串添加一个前缀"Basic ",这样Spring Security就可以通过这个判断客户端是否使用了basic认证。



将上面生成的字符串设置到请求头部,名称为“Authorization”。Spring Security会在认证时,获取头部信息进行判断。

下面是通过鉴权认证访问指定地址的代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

//import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.apache.commons.codec.binary.Base64;
//import org.apache.taglibs.standard.lang.jpath.adapter.Convert;

/**
 * This is a stub class with a main method to run an iDigi web service.
 */
public class PostTest {

	/**
	 * Run the web service request
	 */
	public static void main(String[] args) {

		try {
			// Create url to the iDigi server for a given web service request
			URL url = new URL("http://developer.idigi.com:80/ws/sci");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST"); 
			String username = "admin";
			String password = "admin";
			byte[] token = (username + ":" + password).getBytes("utf-8");
			String encodedAuthorization = new String(Base64.encodeBase64(token), "utf-8");
			conn.setRequestProperty("Authorization", "Basic "+ encodedAuthorization);
			 // Send data to server
		  	conn.setRequestProperty("Content-Type", "text/xml");
			OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
			out.write("<!-- \r\n");
			out.write("See http://www.digi.com/wiki/developer/index.php/Rci for\r\n");
			out.write("an example of a python implementation on a NDS device to\r\n");
			out.write("handle this SCI request\r\n");
			out.write("-->\r\n");
			out.write("<sci_request version=\"1.0\">\r\n");
			out.write("  <send_message>\r\n");
			out.write("    <targets>\r\n");
			out.write("      <device id=\"00000000-00000000-00409DFF-FF4A4244\"/>\r\n");
			out.write("    </targets>\r\n");
			out.write("    <rci_request version=\"1.1\">\r\n");
			out.write("      <do_command target=\"idigi_dia\">\r\n");
			out.write("        <channel_dump/>\r\n");
			out.write("      </do_command>\r\n");
			out.write("    </rci_request>\r\n");
			out.write("  </send_message>\r\n");
			out.write("</sci_request>\r\n");
			out.close();
			// Get input stream from response and convert to String
			conn.disconnect();
			conn.setDoInput(true);
			InputStream is = conn.getInputStream();
			Scanner isScanner = new Scanner(is);
			StringBuffer buf = new StringBuffer();
			while(isScanner.hasNextLine()) {	
				buf.append(isScanner.nextLine() +"\n");
			}
			String responseContent = buf.toString();
			// Output response to standard out
			System.out.println(responseContent);
		} catch (IOException e) { 
			// Print any exceptions that occur
			e.printStackTrace();
		}
	}
}
分享到:
评论

相关推荐

    Tcl_TK编程权威指南pdf

    客户端套接字 服务器端套接字 回送(echo)服务 使用http获取一个url http软件包 基本认证 第18章 tclhttpd web服务器 将 tclhttpd与你的应用程序集成 域处理程序 应用执导的url 文档类型 html+tcl模板 ...

    SQL.Server.2008编程入门经典(第3版).part2.rar

    他从1 980年开始深入学习计算技术,从构建和修复计算机软件包,到通过Z80、Basic以及6502汇编语言进行编程。1 983年,Robert开始攻读计算机信息系统的学位,随后转而研究“PC故障”并开始使用数据库语言(从dBase到...

    SQL.Server.2008编程入门经典(第3版).part1.rar

    他从1 980年开始深入学习计算技术,从构建和修复计算机软件包,到通过Z80、Basic以及6502汇编语言进行编程。1 983年,Robert开始攻读计算机信息系统的学位,随后转而研究“PC故障”并开始使用数据库语言(从dBase到...

    asp.net知识库

    利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的内容是否合法 asp.net报表解决方法 SQLDMO类的使用 SQL过程自动C#封装,支持从表到基本存储过程生成 使用SQLDMO控制 SQL Server 使用SQL...

    JAVA面试题最全集

    1.Web安全性的考虑(表单验证、浏览器Basic方式的验证,应用程序的安全性,SSL,代码考虑) 2.简单介绍您所了解的MVC。 3.简单介绍所了解的XML。 4.文档和编码规范 5.Java中的分页、效率考虑。 6.简单介绍您所...

    Spring中文帮助文档

    2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving...

    Spring API

    2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut element)的支持 2.3.4. 对AspectJ装载时织入(AspectJ load-time weaving...

    PHP基础教程 是一个比较有价值的PHP新手教程!

    传统上网页的交互作用是通过CGI来实现的。CGI程序的伸缩性不很理想,因为它为每一个正在运行的CGI程序开一个独立进程。解决方法就是将经常用来编写CGI程序的语言的解释器编译进你的web服务器(比如mod_perl,JSP)。PHP...

    HttpClient以及获取页面内容应用

    HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta...

    CuteFTP9简易汉化版

    SSL会话Choices-When设置SSL连接,一种上传软件允许您选择三种常见的SSL实现,包括TLS(AUTH TLS)*,SSL隐* *(直接连接在端口990)和SSL显式* *(身份验证SSL)模式。大多数FTP服务器支持至少一种,而一些(比如Globalscape ...

Global site tag (gtag.js) - Google Analytics