2013-07-30 62 views

回答

0

接收這些用戶名和密碼,您需要使用BASIC認證。您需要使用相同的JAASThis tutorial提供基於FORM的認證。但只要你想BASIC認證,則需要

<login-config> 
    <auth-method>BASIC</auth-method> 
</login-config> 

您可以檢索用戶主要如下

request.getUserPrincipal().getName().toString(); 
0

你必須使用基本身份驗證這一點,你可以參考下面的鏈接,它提供了基本身份驗證使用servlet。

<login-config> 
     <auth-method>BASIC</auth-method> 
</login-config> 

它配置基本身份驗證在web.xml 此外,您還需要添加

<?xml version='1.0' encoding='utf-8'?> 
<tomcat-users> 
    <role rolename="tomcat"/> 
    <user username="tomcat" password="tomcat" roles="tomcat"/> 
    <user username="yourname" password="yourpassword" roles="tomcat"/> 
    <user username="test" password="test"/> 
</tomcat-users> 

中的tomcat-users.xml中

實例鏈接: http://www.avajava.com/tutorials/lessons/how-do-i-use-basic-authentication-with-tomcat.html?page=1

希望它對你有幫助。

+0

感謝。這非常有幫助 –

0
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    request.setCharacterEncoding("windows-31j"); 
    response.setContentType("text/html; charset=windows-31j"); 
    String auth = request.getHeader("Authorization"); 
    if (auth == null) 
    { 
     response.setStatus(401); 
     response.setHeader("Cache-Control","no-store"); 
     response.setDateHeader("Expires",0); 
     response.setHeader("WWW-authenticate","Basic Realm=\"AuthDemo Server\""); 

    } else { 
     response.getWriter().print("Authorization :" + request.getHeader("Authorization")); 
    } 

      } 
     } 
相關問題