Advertisement
Guest User

Untitled

a guest
Oct 17th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package org.tc.services.security;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.security.authentication.AuthenticationServiceException;
  6. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  7. import org.springframework.security.core.Authentication;
  8. import org.springframework.security.core.AuthenticationException;
  9. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.web.servlet.FlashMap;
  12. import org.springframework.web.servlet.FlashMapManager;
  13. import org.springframework.web.servlet.support.RequestContextUtils;
  14.  
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19.  
  20.  
  21. public class LoginUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
  22. private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
  23. private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
  24. private boolean postOnly = true;
  25. @Override
  26. public Authentication attemptAuthentication(HttpServletRequest request,
  27. HttpServletResponse response) throws AuthenticationException {
  28. if (postOnly && !request.getMethod().equals("POST")) {
  29. throw new AuthenticationServiceException(
  30. "Authentication method not supported: " + request.getMethod());
  31. }
  32. String username = obtainUsername(request);
  33. String password = obtainPassword(request);
  34. FlashMap flashMap = new FlashMap();
  35. if (username == null) {
  36. username = "";
  37. flashMap.addTargetRequestParam
  38. ("loginError","Login must not be blank.");
  39. }
  40. if (password == "") {
  41. flashMap.addTargetRequestParam
  42. ("passwordError","Password must not be blank.");
  43. }
  44. FlashMapManager manager = RequestContextUtils
  45. .getFlashMapManager(request);
  46. manager.saveOutputFlashMap(flashMap,request,response);
  47. username = username.trim();
  48.  
  49. UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
  50. username, password);
  51.  
  52. // Allow subclasses to set the "details" property
  53. setDetails(request, authRequest);
  54.  
  55. return this.getAuthenticationManager().authenticate(authRequest);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement