使用spring编码/解码一些特殊字符

时间:2019-04-08 16:35:00

标签: java spring resttemplate

我目前遇到这个问题,即使经过一些研究,我仍未找到解决方案。我正在尝试通过http发送用户名和密码。问题是,当我发送“ç”,“ã”或“á”之类的字符时 我收到Ã[]之类的东西,无法通过身份验证。但是,如果我发送“ *”,“ /”,“ @”,则效果很好。 我将向您展示我的代码:

使用RestTemplate进行请求:

    String enc = sec.encrypt(password);
    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("username", username);
    map.add("password", password);
    RestTemplate rt = new RestTemplate();
    rt.getMessageConverters().add(0,new StringHttpMessageConverter(Charset.forName("UTF-8")));

    // Prepare header
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    headers.set("username", username);
    headers.set("password", password);

    HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
    HttpStatus response = null;

    String url = "http://my url"
    try {
        ResponseEntity<DomainSistemas> rps = rt.exchange(url,
                HttpMethod.POST, entity, DomainSistemas.class);
        response = rps.getStatusCode();
        ds = rps.getBody();

接收请求:

@RequestMapping(method = RequestMethod.POST, path = "/authentication")
public @ResponseBody ResponseEntity<Object> Authentication(
        @RequestHeader(value = "username", required = false) String username,
        @RequestHeader(value = "password", required = false) String password) throws IOException, NamingException {

    Ldap ldap = new Ldap();
    GenericResponse response = new GenericResponse();
    String logPrefix = "[AuthenticationController]";

    try {

        if (!ldap.authentication(username, password)) {
            throw new AuthenticationException();
        }

        response.setContent(transform.listEntityToListBean(getPermission.findPermission(username)));

        return ResponseEntity.status(HttpStatus.OK).body(response);

Thx

0 个答案:

没有答案