webservice 安全认证请求头信息

java:
import java.io.IOException;
import java.util.Enumeration;

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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;


@WebFilter("/webservice/*")
public class AuthFilter implements Filter {

   
    public AuthFilter() {
       
    }


public void destroy() {

}


public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;
Enumeration<String> enums = req.getHeaderNames();

System.out.println("header start ******");

while(enums.hasMoreElements()){

String header = enums.nextElement();
System.out.println(header+" = "+req.getHeader(header));

if("authorization".equals(header)){
String auth = req.getHeader(header);
auth = auth.substring(6);

String pwd = "songrenfei:06123";
pwd = new String(Base64.encodeBase64(pwd.getBytes()));
System.out.println("auth = "+auth);
System.out.println("pwd = "+pwd);
if(pwd.equals(auth)){
System.out.println("SOAP权限验证成功!");
}
}

}

System.out.println("header end ******");

chain.doFilter(request, response);
}


public void init(FilterConfig fConfig) throws ServletException {

}

}



.net
#region 验证手机审批
        public static bool WindowValate()
        {
            return true;
            bool result = false;
            string name = "";
            try
            {
                name = HttpContext.Current.Request.Headers.GetValues("Authorization")[0];
                name = name.Trim().Replace("Basic", "");
                name = Base64StringDecode(name);
                string[] parm = name.Split(':');
                result = ValideUser(parm[0], parm[1]);
            }
            catch (Exception e)
            {
                return result;
            }
            return result;
        }
        private static bool ValideUser(string in_UserName, string in_PassWord)
        {
            if ((in_UserName == "zbcgmb") && (in_PassWord == "1234567890"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private static string Base64StringDecode(string input)
        {
            byte[] decbuff = Convert.FromBase64String(input);
            return System.Text.Encoding.UTF8.GetString(decbuff);
        }
        #endregion


---------webservice调用部分
    [WebMethod(EnableSession = true)]
    public string GetJsonTodoTableData(string loginname, int nowpage, int pages)
    {
        if (!SourceVerification.WindowValate())
            //验证失败
            strRetrn = "";
        else
          //验证成功后的业务逻辑
     
    }

你可能感兴趣的:(webservice)