public class UsernamePasswordToken extends Object implements HostAuthenticationToken, RememberMeAuthenticationToken
A simple username/password authentication token to support the most widely-used authentication mechanism. This
class also implements the RememberMeAuthenticationToken
interface to support
"Remember Me" services across user sessions as well as the
HostAuthenticationToken
interface to retain the host name
or IP address location from where the authentication attempt is occurring.
"Remember Me" authentications are disabled by default, but if the application developer wishes to allow
it for a login attempt, all that is necessary is to call setRememberMe(true)
. If the underlying
SecurityManager implementation also supports RememberMe services, the user's identity will be
remembered across sessions.
Note that this class stores a password as a char[] instead of a String (which may seem more logical). This is because Strings are immutable and their internal value cannot be overwritten - meaning even a nulled String instance might be accessible in memory at a later time (e.g. memory dump). This is not good for sensitive information such as passwords. For more information, see the Java Cryptography Extension Reference Guide.
To avoid this possibility of later memory access, the application developer should always call
clear()
after using the token to perform a login attempt.
Constructor and Description |
---|
UsernamePasswordToken()
JavaBeans compatible no-arg constructor.
|
UsernamePasswordToken(String username,
char[] password)
Constructs a new UsernamePasswordToken encapsulating the username and password submitted
during an authentication attempt, with a null
host and a
rememberMe default of false. |
UsernamePasswordToken(String username,
char[] password,
boolean rememberMe)
Constructs a new UsernamePasswordToken encapsulating the username and password submitted, as well as if the user
wishes their identity to be remembered across sessions.
|
UsernamePasswordToken(String username,
char[] password,
boolean rememberMe,
String host)
Constructs a new UsernamePasswordToken encapsulating the username and password submitted, if the user
wishes their identity to be remembered across sessions, and the inetAddress from where the attempt is occurring.
|
UsernamePasswordToken(String username,
char[] password,
String host)
Constructs a new UsernamePasswordToken encapsulating the username and password submitted, the
inetAddress from where the attempt is occurring, and a default rememberMe value of false
|
UsernamePasswordToken(String username,
String password)
Constructs a new UsernamePasswordToken encapsulating the username and password submitted
during an authentication attempt, with a null
host and
a rememberMe default of false
This is a convenience constructor and maintains the password internally via a character
array, i.e. |
UsernamePasswordToken(String username,
String password,
boolean rememberMe)
Constructs a new UsernamePasswordToken encapsulating the username and password submitted, as well as if the user
wishes their identity to be remembered across sessions.
|
UsernamePasswordToken(String username,
String password,
boolean rememberMe,
String host)
Constructs a new UsernamePasswordToken encapsulating the username and password submitted, if the user
wishes their identity to be remembered across sessions, and the inetAddress from where the attempt is occurring.
|
UsernamePasswordToken(String username,
String password,
String host)
Constructs a new UsernamePasswordToken encapsulating the username and password submitted, the
inetAddress from where the attempt is occurring, and a default rememberMe value of false
This is a convenience constructor and maintains the password internally via a character
array, i.e.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears out (nulls) the username, password, rememberMe, and inetAddress.
|
Object |
getCredentials()
Returns the
password char array. |
String |
getHost()
Returns the host name or IP string from where the authentication attempt occurs.
|
char[] |
getPassword()
Returns the password submitted during an authentication attempt as a character array.
|
Object |
getPrincipal()
Simply returns
getUsername() . |
String |
getUsername()
Returns the username submitted during an authentication attempt.
|
boolean |
isRememberMe()
Returns true if the submitting user wishes their identity (principal(s)) to be remembered
across sessions, false otherwise.
|
void |
setHost(String host)
Sets the host name or IP string from where the authentication attempt occurs.
|
void |
setPassword(char[] password)
Sets the password for submission during an authentication attempt.
|
void |
setRememberMe(boolean rememberMe)
Sets if the submitting user wishes their identity (principal(s)) to be remembered across sessions.
|
void |
setUsername(String username)
Sets the username for submission during an authentication attempt.
|
String |
toString()
Returns the String representation.
|
public UsernamePasswordToken()
public UsernamePasswordToken(String username, char[] password)
host
and a
rememberMe default of false.username
- the username submitted for authenticationpassword
- the password character array submitted for authenticationpublic UsernamePasswordToken(String username, String password)
host
and
a rememberMe default of false
This is a convenience constructor and maintains the password internally via a character array, i.e. password.toCharArray();. Note that storing a password as a String in your code could have possible security implications as noted in the class JavaDoc.
username
- the username submitted for authenticationpassword
- the password string submitted for authenticationpublic UsernamePasswordToken(String username, char[] password, String host)
username
- the username submitted for authenticationpassword
- the password string submitted for authenticationhost
- the host name or IP string from where the attempt is occurringpublic UsernamePasswordToken(String username, String password, String host)
This is a convenience constructor and maintains the password internally via a character array, i.e. password.toCharArray();. Note that storing a password as a String in your code could have possible security implications as noted in the class JavaDoc.
username
- the username submitted for authenticationpassword
- the password string submitted for authenticationhost
- the host name or IP string from where the attempt is occurringpublic UsernamePasswordToken(String username, char[] password, boolean rememberMe)
username
- the username submitted for authenticationpassword
- the password string submitted for authenticationrememberMe
- if the user wishes their identity to be remembered across sessionspublic UsernamePasswordToken(String username, String password, boolean rememberMe)
This is a convenience constructor and maintains the password internally via a character array, i.e. password.toCharArray();. Note that storing a password as a String in your code could have possible security implications as noted in the class JavaDoc.
username
- the username submitted for authenticationpassword
- the password string submitted for authenticationrememberMe
- if the user wishes their identity to be remembered across sessionspublic UsernamePasswordToken(String username, char[] password, boolean rememberMe, String host)
username
- the username submitted for authenticationpassword
- the password character array submitted for authenticationrememberMe
- if the user wishes their identity to be remembered across sessionshost
- the host name or IP string from where the attempt is occurringpublic UsernamePasswordToken(String username, String password, boolean rememberMe, String host)
This is a convenience constructor and maintains the password internally via a character array, i.e. password.toCharArray();. Note that storing a password as a String in your code could have possible security implications as noted in the class JavaDoc.
username
- the username submitted for authenticationpassword
- the password string submitted for authenticationrememberMe
- if the user wishes their identity to be remembered across sessionshost
- the host name or IP string from where the attempt is occurringpublic String getUsername()
public void setUsername(String username)
username
- the username to be used for submission during an authentication attempt.public char[] getPassword()
public void setPassword(char[] password)
password
- the password to be used for submission during an authentication attempt.public Object getPrincipal()
getUsername()
.getPrincipal
in interface AuthenticationToken
username
.AuthenticationToken.getPrincipal()
public Object getCredentials()
password
char array.getCredentials
in interface AuthenticationToken
password
char array.AuthenticationToken.getCredentials()
public String getHost()
(Shiro's default Authenticator allows null hosts to support localhost and proxy server environments).
getHost
in interface HostAuthenticationToken
public void setHost(String host)
(Shiro's default Authenticator allows null hosts to allow localhost and proxy server environments).
host
- the host name or IP string from where the attempt is occurringpublic boolean isRememberMe()
isRememberMe
in interface RememberMeAuthenticationToken
public void setRememberMe(boolean rememberMe)
rememberMe
- value indicating if the user wishes their identity (principal(s)) to be remembered across
sessions.public void clear()
Copyright © 2004–2019 The Apache Software Foundation. All rights reserved.