Android --- Http協議,解析服務器中給定的XML文件時的權限問題。
如題:要求使用Http協議中的POST方法,解析存在服務器中的XML文件。
服務器使用的是Tomcat搭建,在webapp目錄下自定義一個xml文件。
- <?xml version="1.0" encoding="utf-8"?>
- <error_logs>
-
- <log>
- <id name="id">1</id>
- <date name="date">2012-04-04 15:45</date>
- <from name = "from">the error from 1</from>
- <subject name="subject">subject1</subject>
- <content name="content">content aaaaaaaaaaaaaaaa</content>
- </log>
-
- <log>
- <id name="id">2</id>
- <date name="date">2012-03-12 11:33</date>
- <from name = "from">the_error_from_2</from>
- <subject name="subject">subject2</subject>
- <content name="content">content bbbbbbbbbbbbbbbbbbb</content>
- </log>
-
- <log>
- <id name="id">3</id>
- <date name="date">2012-03-02 01:00</date>
- <from name = "from">the_error_from_3</from>
- <subject name="subject">subject3</subject>
- <content name="content">content cccccccccccccccccccc</content>
- </log>
-
- </error_logs>
疑點:這個xml文件,只要是開著服務器,在浏覽器中直接輸入地址就訪問,那麼如何屏蔽這種直接訪問?
辦法:
1、寫一個過濾類
- package me.lechao;
-
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
-
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class IsLogin implements Filter {
- FilterConfig filterConfig;
-
- @Override
- public void destroy() {
- this.filterConfig = null;
- }
-
- @Override
- public void doFilter(ServletRequest arg0, ServletResponse arg1,
- FilterChain arg2) throws IOException, ServletException {
- HttpServletRequest req = (HttpServletRequest) arg0;
- HttpServletResponse res = (HttpServletResponse) arg1;
-
- String username = req.getParameter("username");
- String password = req.getParameter("password");
-
- PrintWriter out = res.getWriter();
-
- if ("1591408983".equals(username) && "123456".equals(password)) {
- //驗證成功可以訪問,屏蔽23456萬字
- } else {
- out.print("the username or password is error!");
- }
- out.flush();
- out.close();
- }
-
- @Override
- public void init(FilterConfig arg0) throws ServletException {
- this.filterConfig = arg0;
- }
-
- }