Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RestUsernamePasswordAuthenticationFilter extends
- UsernamePasswordAuthenticationFilter {
- private final Logger log = LoggerFactory.getLogger(this.getClass()
- .getName());
- static final String ORIGIN = "Origin";
- @Override
- public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response){
- String originHeader = request.getHeader(ORIGIN);
- log.debug(">> request.getHeader()::"+originHeader);
- log.debug(">> request.getMethod()::"+request.getMethod());
- String jsonReQuestBody = extractPostRequestBody(request);
- log.debug(">> request.body " + jsonReQuestBody);
- JsonObject requestJsonBody = new Gson().fromJson(jsonReQuestBody, JsonObject.class);
- Iterator<Entry<String, JsonElement>> jsonIterator = requestJsonBody.entrySet().iterator();
- String userName = null;
- String password = null;
- String serviceLocationId = null;
- while (jsonIterator.hasNext()){
- Entry<String,JsonElement> entry= jsonIterator.next();
- String key = entry.getKey();
- JsonElement value = entry.getValue();
- log.debug(">> key " + key + "value" + value.getAsString());
- if (key != null && key.trim().equalsIgnoreCase("j_username")){
- userName = value.getAsString();
- }else if (key != null && key.trim().equalsIgnoreCase("j_password")){
- password = value.getAsString();
- }else if (key != null && key.trim().equalsIgnoreCase("j_service_location_uid")){
- serviceLocationId = value.getAsString();
- }
- }
- if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
- try {
- response.getWriter().print("OK");
- response.getWriter().flush();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(userName, password);
- request.setAttribute("j_service_location_uid", serviceLocationId);
- // Allow subclasses to set the "details" property
- setDetails(request, authRequest);
- return this.getAuthenticationManager().authenticate(authRequest);
- }
- @SuppressWarnings("resource")
- private String extractPostRequestBody(HttpServletRequest request) {
- if ("POST".equalsIgnoreCase(request.getMethod())) {
- Scanner s = null;
- try {
- s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\A");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return s.hasNext() ? s.next() : "";
- }
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement