/** * @author: Vladislav Kamensky */ import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class AuthorizeServlet extends HttpServlet { private String authUserName; private String authUserPsswd; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try{ if(getAuthorizedParameters(req) && authUserName.equals("modano") && authUserPsswd.equals("modano")){ res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println(""); out.println("AbntPasswordServlet"); out.println("
"); out.println("

You have been authorized

"); out.println("
"); out.println(""); }else{ res.setStatus(res.SC_UNAUTHORIZED); res.setHeader("WWW-Authenticate","Basic realm=\"testarea\""); } }catch(Throwable theException){ // uncomment the following line when unexpected exceptions // are occuring to aid in debugging the problem. //theException.printStackTrace(); } doPost(req,res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try{ }catch(Exception ex){ } } /** * @author: Igor Maznitsa */ public boolean getAuthorizedParameters(HttpServletRequest req) { sun.misc.BASE64Decoder bs64dec = new sun.misc.BASE64Decoder(); boolean result = true; String NamePassword = null; String auth_string = req.getHeader("Authorization"); if (auth_string==null) return false; StringTokenizer st = new StringTokenizer(auth_string," "); String scheme = null; try{ scheme = st.nextToken(); }catch (NoSuchElementException e){ scheme = ""; result = false; } try{ if (scheme.equalsIgnoreCase("BASIC")){ NamePassword = st.nextToken(); NamePassword = new String(bs64dec.decodeBuffer(NamePassword)); }else NamePassword = st.nextToken(); }catch (NoSuchElementException e){ NamePassword = ""; result = false; }catch (IOException e){ NamePassword = ""; result = false; } st = null; st = new StringTokenizer(NamePassword,":"); try{ authUserName = st.nextToken(); }catch (NoSuchElementException e){ authUserName = null; result = false; } try{ this.authUserPsswd = st.nextToken(); }catch (NoSuchElementException e){ this.authUserPsswd = null; result = false; } return result; } public void init() { System.out.println("In AbntPasswordServlet init"); } }