You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by tarka <in...@tarka.tv> on 2012/07/20 15:03:41 UTC
Re: How to set Principals?
Sorry, I have been away on another project for little while. I am now back to
try and finish off this Shiro issue!
I tried to implement the code that you suggested but as always the second I
do that subjects are no longer able to authenticate. Lets just take a step
back for a moment so I can try to understand why this isn't working.
Here is my current code (works perfectly).
My doGetAuthenticationInfo method in my custom realm:
###################################
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken
token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
String username = upToken.getUsername();
// Null username is invalid
if (username == null) {
throw new AccountException("Null usernames are not allowed by
this realm.");
}
SimpleAuthenticationInfo info = null;
try {
String password = null;
try {
password = getPasswordForUser(username)[0];
} catch (Exception ex) {
java.util.logging.Logger.getLogger(DynamoRealm.class.getName()).log(Level.SEVERE,
null, ex);
}
if (password == null) {
throw new UnknownAccountException("No account found for user
[" + username + "]");
}
info = new SimpleAuthenticationInfo(username,
password.toCharArray(), getName());
} catch (Exception e) {
final String message = "There was an error while authenticating
user [" + username + "]";
if (log.isErrorEnabled()) {
log.error(message, e);
}
// Rethrow any SQL errors as an authentication exception
throw new AuthenticationException(message, e);
}
return info;
}
###################################
And a stripped down version of my authentication class used with a form:
###################################
public void authenticate(
String username,
String password,
boolean rememberMe) {
// some logic checks
try {
// Submit credentials to shiro for authentication
UsernamePasswordToken subjectToken = new
UsernamePasswordToken(username, password);
subjectToken.setRememberMe(rememberMe);
SecurityUtils.getSubject().login(subjectToken);
} catch (Exception e) {
// Catch all the exceptions ;
}
}
###################################
At the moment I can use
SecurityUtils.getSubject().getPrincipal().toString(); to retrieve the
current subjects principal, which is their username (only).
###################################
If I now want to add a uuid to that PrincipalCollection (the
getUuidForUser(username) returns a string from the db). I have modified my
doGetAuthenticationInfo method as follows:
###################################
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken
token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
String username = upToken.getUsername();
// Null username is invalid
if (username == null) {
throw new AccountException("Null usernames are not allowed by
this realm.");
}
SimpleAuthenticationInfo info = null;
try {
String password = null;
String uuid = null;
try {
password = getPasswordForUser(username)[0];
uuid = getUuidForUser(username);
} catch (Exception ex) {
java.util.logging.Logger.getLogger(DynamoRealm.class.getName()).log(Level.SEVERE,
null, ex);
}
if (password == null) {
throw new UnknownAccountException("No account found for user
[" + username + "]");
}
SimplePrincipalCollection principals = new
SimplePrincipalCollection();
principals.add(username, getName());
principals.add(uuid, getName());
info = new SimpleAuthenticationInfo(principals,
password.toCharArray(), getName());
} catch (Exception e) {
final String message = "There was an error while authenticating
user [" + username + "]";
if (log.isErrorEnabled()) {
log.error(message, e);
}
// Rethrow any SQL errors as an authentication exception
throw new AuthenticationException(message, e);
}
return info;
}
###################################
First off I get a org.apache.shiro.subject.SimplePrincipalCollection cannot
be cast to java.lang.String exception. Even if I comment out the
principals.add(uuid, getName()); line I still get the error so Im obviously
not implementing this correctly.
Thanks in advance for any help
--
View this message in context: http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577614.html
Sent from the Shiro User mailing list archive at Nabble.com.
Re: How to set Principals?
Posted by tarka <in...@tarka.tv>.
Amazing that's done it.
Thanks very much
On 20/07/2012 21:36, Jared Bunting-2 [via Shiro User] wrote:
> That is the correct way to get it, however it appears that you are
> creating it slightly incorrectly. This line:
>
> info = new SimpleAuthenticationInfo(principals,
> password.toCharArray(), getName());
>
> is not calling the constructor that you expect. It is calling (Object,
> Object, String) - where the first object is "the primary principal".
> Instead, change it to:
>
> info = new SimpleAuthenticationInfo(principals,
> password.toCharArray());
>
> which should call (PrincipalCollection, Object). You don't need to
> specify the realm name again, since you've already specified it when
> adding principals to the principal collection.
>
> Hope that helps,
> Jared
>
> On Fri 20 Jul 2012 02:30:08 PM CDT, tarka wrote:
>
> > Thanks Kalle,
> >
> > Could you just clarify for me. My understanding is that a
> 'principal' is
> > the name used to specify a subjects unique identifying attribute
> such as
> > a uuid or a username etc. However when I place a call to
> > getPrimaryPrincipal() I get a collection of type
> > <SimplePrincipalCollection> containing a linkedHashMap of both the
> > username keypair and the uuid keypair. I would have thought that this
> > should return only the 'Primary Principal' ie. a single keypair value!
> >
> > If the above is the correct behaviour and I have misunderstood then
> > what's the best way to access the individual principals? When I do this
> > for example:
> >
> > doGetAuthorizationInfo(PrincipalCollection principals) {
> > ...
> > Object c = principals.getPrimaryPrincipal();
> > ...
> > }
> >
> > I can't then access the LinkedHashMap.
> >
> > Tarka
> >
> > On 20/07/2012 18:00, kaosko [via Shiro User] wrote:
> >> On Fri, Jul 20, 2012 at 8:27 AM, tarka <[hidden email]
> >> </user/SendEmail.jtp?type=node&node=7577616&i=0>> wrote:
> >>> The above code actually works! The problem is coming from trying to
> >> retrieve
> >>> the principals! At the moment I am getting a LinkedHashMap inside a
> >>> SimplePrincipalCollection object returned.
> >>
> >> You are getting or "you are trying to get"?
> >>
> >>> How can I simply access the LinkedHashMap keys directly?
> >>
> >> The simple answer is you don't. Use the operations of
> >> PrincipalCollection to return a principal specific to your needs, for
> >> example getPrimaryPrincipal(), oneByType() or fromRealm(...).
> >>
> >> Kalle
> >>
> >>
> >>
> ------------------------------------------------------------------------
> >> If you reply to this email, your message will be added to the
> >> discussion below:
> >>
> http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577616.html
> >>
> >> To unsubscribe from How to set Principals?, click here
> >> <
> >> NAML
> >>
> <http://shiro-user.582556.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
> >>
> >
> >
> >
> >
> >
> >
> > --
> > View this message in context:
> http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577617.html
> > Sent from the Shiro User mailing list archive at Nabble.com.
>
>
>
>
> ------------------------------------------------------------------------
> If you reply to this email, your message will be added to the
> discussion below:
> http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577618.html
>
> To unsubscribe from How to set Principals?, click here
> <http://shiro-user.582556.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=7490972&code=aW5mb0B0YXJrYS50dnw3NDkwOTcyfC0xNjQ5ODA1ODk5>.
> NAML
> <http://shiro-user.582556.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--
View this message in context: http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577619.html
Sent from the Shiro User mailing list archive at Nabble.com.
Re: How to set Principals?
Posted by Jared Bunting <ja...@peachjean.com>.
That is the correct way to get it, however it appears that you are
creating it slightly incorrectly. This line:
info = new SimpleAuthenticationInfo(principals,
password.toCharArray(), getName());
is not calling the constructor that you expect. It is calling (Object,
Object, String) - where the first object is "the primary principal".
Instead, change it to:
info = new SimpleAuthenticationInfo(principals,
password.toCharArray());
which should call (PrincipalCollection, Object). You don't need to
specify the realm name again, since you've already specified it when
adding principals to the principal collection.
Hope that helps,
Jared
On Fri 20 Jul 2012 02:30:08 PM CDT, tarka wrote:
> Thanks Kalle,
>
> Could you just clarify for me. My understanding is that a 'principal' is
> the name used to specify a subjects unique identifying attribute such as
> a uuid or a username etc. However when I place a call to
> getPrimaryPrincipal() I get a collection of type
> <SimplePrincipalCollection> containing a linkedHashMap of both the
> username keypair and the uuid keypair. I would have thought that this
> should return only the 'Primary Principal' ie. a single keypair value!
>
> If the above is the correct behaviour and I have misunderstood then
> what's the best way to access the individual principals? When I do this
> for example:
>
> doGetAuthorizationInfo(PrincipalCollection principals) {
> ...
> Object c = principals.getPrimaryPrincipal();
> ...
> }
>
> I can't then access the LinkedHashMap.
>
> Tarka
>
> On 20/07/2012 18:00, kaosko [via Shiro User] wrote:
>> On Fri, Jul 20, 2012 at 8:27 AM, tarka <[hidden email]
>> </user/SendEmail.jtp?type=node&node=7577616&i=0>> wrote:
>>> The above code actually works! The problem is coming from trying to
>> retrieve
>>> the principals! At the moment I am getting a LinkedHashMap inside a
>>> SimplePrincipalCollection object returned.
>>
>> You are getting or "you are trying to get"?
>>
>>> How can I simply access the LinkedHashMap keys directly?
>>
>> The simple answer is you don't. Use the operations of
>> PrincipalCollection to return a principal specific to your needs, for
>> example getPrimaryPrincipal(), oneByType() or fromRealm(...).
>>
>> Kalle
>>
>>
>> ------------------------------------------------------------------------
>> If you reply to this email, your message will be added to the
>> discussion below:
>> http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577616.html
>>
>> To unsubscribe from How to set Principals?, click here
>> <http://shiro-user.582556.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=7490972&code=aW5mb0B0YXJrYS50dnw3NDkwOTcyfC0xNjQ5ODA1ODk5>.
>> NAML
>> <http://shiro-user.582556.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
>
>
>
>
> --
> View this message in context: http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577617.html
> Sent from the Shiro User mailing list archive at Nabble.com.
Re: How to set Principals?
Posted by tarka <in...@tarka.tv>.
Thanks Kalle,
Could you just clarify for me. My understanding is that a 'principal' is
the name used to specify a subjects unique identifying attribute such as
a uuid or a username etc. However when I place a call to
getPrimaryPrincipal() I get a collection of type
<SimplePrincipalCollection> containing a linkedHashMap of both the
username keypair and the uuid keypair. I would have thought that this
should return only the 'Primary Principal' ie. a single keypair value!
If the above is the correct behaviour and I have misunderstood then
what's the best way to access the individual principals? When I do this
for example:
doGetAuthorizationInfo(PrincipalCollection principals) {
...
Object c = principals.getPrimaryPrincipal();
...
}
I can't then access the LinkedHashMap.
Tarka
On 20/07/2012 18:00, kaosko [via Shiro User] wrote:
> On Fri, Jul 20, 2012 at 8:27 AM, tarka <[hidden email]
> </user/SendEmail.jtp?type=node&node=7577616&i=0>> wrote:
> > The above code actually works! The problem is coming from trying to
> retrieve
> > the principals! At the moment I am getting a LinkedHashMap inside a
> > SimplePrincipalCollection object returned.
>
> You are getting or "you are trying to get"?
>
> > How can I simply access the LinkedHashMap keys directly?
>
> The simple answer is you don't. Use the operations of
> PrincipalCollection to return a principal specific to your needs, for
> example getPrimaryPrincipal(), oneByType() or fromRealm(...).
>
> Kalle
>
>
> ------------------------------------------------------------------------
> If you reply to this email, your message will be added to the
> discussion below:
> http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577616.html
>
> To unsubscribe from How to set Principals?, click here
> <http://shiro-user.582556.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=7490972&code=aW5mb0B0YXJrYS50dnw3NDkwOTcyfC0xNjQ5ODA1ODk5>.
> NAML
> <http://shiro-user.582556.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--
View this message in context: http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577617.html
Sent from the Shiro User mailing list archive at Nabble.com.
Re: How to set Principals?
Posted by Kalle Korhonen <ka...@gmail.com>.
On Fri, Jul 20, 2012 at 8:27 AM, tarka <in...@tarka.tv> wrote:
> The above code actually works! The problem is coming from trying to retrieve
> the principals! At the moment I am getting a LinkedHashMap inside a
> SimplePrincipalCollection object returned.
You are getting or "you are trying to get"?
> How can I simply access the LinkedHashMap keys directly?
The simple answer is you don't. Use the operations of
PrincipalCollection to return a principal specific to your needs, for
example getPrimaryPrincipal(), oneByType() or fromRealm(...).
Kalle
Re: How to set Principals?
Posted by tarka <in...@tarka.tv>.
The above code actually works! The problem is coming from trying to retrieve
the principals! At the moment I am getting a LinkedHashMap inside a
SimplePrincipalCollection object returned.
How can I simply access the LinkedHashMap keys directly?
--
View this message in context: http://shiro-user.582556.n2.nabble.com/How-to-set-Principals-tp7490972p7577615.html
Sent from the Shiro User mailing list archive at Nabble.com.