0

我正在使用 Spring SimpleFormController 来处理表单提交。当客户端在 httpclient 中通过 httpclient.getState().setCredentials 设置 UsernamePasswordCredentials 时,如何在我的“onSubmit”方法中从 HttpServletRequest 在服务器端获取此 UsernamePasswordCredentials。

4

1 回答 1

1

我们可以从请求中获取“授权”标头值,然后对该值进行 Base64 解码,以表明如果有人拦截网络通信,用户名和密码实际上非常容易读取。(这就是为什么使用 HTTPS 进行身份验证是一个非常好的主意)。

     try {
        String auth = request.getHeader("Authorization");
        if(auth!=null&&auth.length()>6){
            String userpassEncoded = auth.substring(6);  
            // Decode it, using any base 64 decoder  
            sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();  
            String userpassDecoded= new String(dec.decodeBuffer(userpassEncoded));
            System.out.println(" userpassDecoded = "+userpassDecoded);
         }
       } catch (Exception e) {
        e.printStackTrace();
       }
于 2013-10-03T05:34:47.303 回答