SmsCodeAuthenticationToken.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. *
  3. */
  4. package com.yihu.base.security.sms;
  5. import org.springframework.security.authentication.AbstractAuthenticationToken;
  6. import org.springframework.security.core.GrantedAuthority;
  7. import org.springframework.security.core.SpringSecurityCoreVersion;
  8. import java.util.Collection;
  9. /**
  10. * @author chenweida
  11. * 短信授权的Token对象
  12. */
  13. public class SmsCodeAuthenticationToken extends AbstractAuthenticationToken {
  14. private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
  15. // ~ Instance fields
  16. // ================================================================================================
  17. private final Object principal;
  18. // ~ Constructors
  19. // ===================================================================================================
  20. /**
  21. * This constructor can be safely used by any code that wishes to create a
  22. * <code>UsernamePasswordAuthenticationToken</code>, as the {@link #isAuthenticated()}
  23. * will return <code>false</code>.
  24. *
  25. */
  26. public SmsCodeAuthenticationToken(String mobile) {
  27. super(null);
  28. this.principal = mobile;
  29. setAuthenticated(false);
  30. }
  31. /**
  32. * This constructor should only be used by <code>AuthenticationManager</code> or
  33. * <code>AuthenticationProvider</code> implementations that are satisfied with
  34. * producing a trusted (i.e. {@link #isAuthenticated()} = <code>true</code>)
  35. * authentication token.
  36. *
  37. * @param principal
  38. * @param credentials
  39. * @param authorities
  40. */
  41. public SmsCodeAuthenticationToken(Object principal,
  42. Collection<? extends GrantedAuthority> authorities) {
  43. super(authorities);
  44. this.principal = principal;
  45. super.setAuthenticated(true); // must use super, as we override
  46. }
  47. // ~ Methods
  48. // ========================================================================================================
  49. public Object getCredentials() {
  50. return null;
  51. }
  52. public Object getPrincipal() {
  53. return this.principal;
  54. }
  55. public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
  56. if (isAuthenticated) {
  57. throw new IllegalArgumentException(
  58. "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
  59. }
  60. super.setAuthenticated(false);
  61. }
  62. @Override
  63. public void eraseCredentials() {
  64. super.eraseCredentials();
  65. }
  66. }