Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. public class RestUsernamePasswordAuthenticationFilter extends
  2. UsernamePasswordAuthenticationFilter {
  3. private final Logger log = LoggerFactory.getLogger(this.getClass()
  4. .getName());
  5.  
  6. static final String ORIGIN = "Origin";
  7.  
  8. @Override
  9. public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response){
  10.  
  11. String originHeader = request.getHeader(ORIGIN);
  12.  
  13. log.debug(">> request.getHeader()::"+originHeader);
  14. log.debug(">> request.getMethod()::"+request.getMethod());
  15. String jsonReQuestBody = extractPostRequestBody(request);
  16. log.debug(">> request.body " + jsonReQuestBody);
  17. JsonObject requestJsonBody = new Gson().fromJson(jsonReQuestBody, JsonObject.class);
  18. Iterator<Entry<String, JsonElement>> jsonIterator = requestJsonBody.entrySet().iterator();
  19. String userName = null;
  20. String password = null;
  21. String serviceLocationId = null;
  22. while (jsonIterator.hasNext()){
  23. Entry<String,JsonElement> entry= jsonIterator.next();
  24. String key = entry.getKey();
  25. JsonElement value = entry.getValue();
  26. log.debug(">> key " + key + "value" + value.getAsString());
  27. if (key != null && key.trim().equalsIgnoreCase("j_username")){
  28. userName = value.getAsString();
  29. }else if (key != null && key.trim().equalsIgnoreCase("j_password")){
  30. password = value.getAsString();
  31. }else if (key != null && key.trim().equalsIgnoreCase("j_service_location_uid")){
  32. serviceLocationId = value.getAsString();
  33. }
  34.  
  35. }
  36. if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
  37. try {
  38. response.getWriter().print("OK");
  39. response.getWriter().flush();
  40. } catch (IOException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44.  
  45. return null;
  46.  
  47. }
  48. UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(userName, password);
  49. request.setAttribute("j_service_location_uid", serviceLocationId);
  50. // Allow subclasses to set the "details" property
  51. setDetails(request, authRequest);
  52. return this.getAuthenticationManager().authenticate(authRequest);
  53. }
  54.  
  55. @SuppressWarnings("resource")
  56. private String extractPostRequestBody(HttpServletRequest request) {
  57. if ("POST".equalsIgnoreCase(request.getMethod())) {
  58. Scanner s = null;
  59. try {
  60. s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\A");
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. return s.hasNext() ? s.next() : "";
  65. }
  66. return "";
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement