[BAEL-16045] - Moved code to spring-security-rest-basic-auth

This commit is contained in:
amit2103
2019-10-27 20:24:22 +05:30
parent 55954d2300
commit 0eaee4015a
2 changed files with 13 additions and 12 deletions

View File

@@ -1,8 +1,12 @@
package org.baeldung.web.controller;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;
import org.baeldung.web.dto.Bar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -28,4 +32,13 @@ public class BarController {
return new Bar();
}
public HttpHeaders createHeaders(String username, String password){
return new HttpHeaders() {{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("US-ASCII")) );
String authHeader = "Basic " + new String( encodedAuth );
set( "Authorization", authHeader );
}};
}
}