Announcement Announcement Module
Collapse
No announcement yet.
Change headers at request and get response body Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Change headers at request and get response body

    Hi everyone, I'm trying to make a log in. In order the log in to be successful i need to make some changes to the headers. Initialy i have a form get a username and a password.The i make a 64encoding to (usernameassword) and i add it to the headers. Afterwards i send this using the resttemplate.exhange(url...).The problem is that i want to send only the headers(this is enough for the log in authentication) and then,i expect to take automatically a response which will contain the following data: id,username,role (+HTTP headers).I'm confused and i need your help.I'm trying this:


    HttpStatus=null;
    @RequestMapping(value = "/log", method = RequestMethod.GET)
    public String getAddPageLogin(Model model) {

    model.addAttribute("loginAttribute", new Login());

    return "login";
    }




    @RequestMapping(value = "/log", method = RequestMethod.POST)
    public String addPersonLogin(@ModelAttribute("loginAttribute") Login login, Model model,HttpServletRequest request) {

    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    acceptableMediaTypes.add(MediaType.APPLICATION_XML );

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(acceptableMediaTypes);

    String values = login.getUsername()+":"+login.getPassword();
    byte[] bytevalues = Base64.encodeBase64(values.getBytes());
    String authHeader = "Basic " + new String( bytevalues );
    headers.set( "Authorization", authHeader );
    System.out.println(headers);

    HttpSession session = request.getSession(true);
    session.setAttribute("username/password", authHeader);



    HttpEntity<Login> entity = new HttpEntity<Login>(login, headers);


    try {
    ResponseEntity<Login> result = restTemplate.exchange("http://www.../user/login/",
    HttpMethod.POST, entity, Login.class);
    model.addAttribute("names", result.getBody());
    status = result.getStatusCode();

    } catch (Exception e) {
    System.out.println(e);
    }

    return "resultpage";
    }

    My login class:


    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Login {



    private String username;
    private String password;




    public String getUsername() {
    return username;
    }

    public void setUsername(String username) {
    this.username = username;
    }

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    }
    Last edited by alexios.7; May 9th, 2012, 04:51 PM.
Working...
X