2014-10-02 30 views
1

我需要發送包含特殊字符的密碼。例如:包含abc +的密碼最終成爲服務器端的abc(空格),這是錯誤的,從而打破了前方的大象。服務器是無罪的,因爲我檢查了網絡,角度實際上是用空間而不是'abc +'發送到服務器'abc'。所以問題出在客戶端代碼上。無法發送包含特殊字符的密碼(替換爲空格)

login: function (param) { 
         var data, 
          config = { 
           headers: { 
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' 

          }, 
          ignoreAuthModule: 'ignoreAuthModule' 
         }; 

        data = 'j_username=' + param.username + '&j_password=' + param.password + '&_spring_security_remember_me=' + param.rememberMe + '&submit=Login'; 
        if (!angular.isUndefined(param.service)) { 
         data += '&service=' + param.service; 
        } 

        $http.post('app/authentication', data, config).success(
         function (data, status, headers, config) { 
          $rootScope.registrationSuccess = false; 
          $rootScope.authenticationError = false; 
          $rootScope.userLocked = false; 
          if (param.success) { 
           param.success(data, status, headers, config); 
          } 
         } 
        ) ... 

當我調試代碼時,j_password實際上有'abc +'。我猜想一些配置是錯誤的。幫助可憐的Java開發人員請。

+3

您應該對UrlEncode(或Base64)進行參數編碼。你可以用[encodeURI](http://www.w3schools.com/jsref/jsref_encodeuri.asp)來做到這一點。 – 2014-10-02 18:42:57

+0

'encodeURIComponent(param.username)'etc – Pointy 2014-10-02 18:45:39

+0

不知道我可以幫助大象。 – Pointy 2014-10-02 18:46:33

回答

2

您應該使用encodeURIComponent對參數進行編碼。例如,

data = 'j_username=' + encodeURIComponent(param.username) + '&j_password=' + encodeURIComponentparam.password) + '&_spring_security_remember_me=' + encodeURIComponent(param.rememberMe) + '&submit=Login'; 
+0

如果smbdy通過restTemplate轉發像我這樣的請求,那麼我建議org.yaml.snakeyaml.util.UriEncoder發送多部分表單編碼數據。 (反正你用的是yml吧?:-)) – Aubergine 2014-10-03 10:26:15