You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shiro.apache.org by Helge Wiemann <hw...@feedbager.de> on 2024/04/13 14:29:15 UTC
get the authenticated user in spring controller method
Hi all,
I am quite new to Shiro and currently moving from Spring Security to Shiro.
One question: After a user is successfully authenticated and authorized,
what is the best way to get the user in a Spring controller method?
With used Spring Security it was something like that:
publicModelAndView
index(*@AuthenticationPrincipalUsernamePasswordAuthenticationTokenauthenticatedUser*,
HttpServletRequest request, HttpServletResponse response)
throwsIOException {
Best regards,
Helge
Re: get the authenticated user in spring controller method
Posted by le...@flowlogix.com.
He Principal type depends on the Realm that you are using.
The Principal type of Map is for example purposes only.
In your code, you would substitute the type passed into byType() method with the appropriate type.
Or you can use Subject.getPrincipal() that returns Object type and cast to appropriate type manually.
> On Apr 13, 2024, at 12:50 PM, Helge Wiemann <hw...@feedbager.de> wrote:
>
> Hi,
>
> thanks for the sample code.
>
> But I questions because I got some problems to understand the code:
>
>
> Collection<Map> principalMaps = subject.getPrincipals().byType(Map.class);
> if (CollectionUtils.isEmpty(principalMaps)) {
> name = subject.getPrincipal().toString();
> } else {
> name = (String) principalMaps.iterator().next().get("username");
> }
>
>
> When the map of the principals is empty, why and how do I get then the logged user by getPrincipal? I would expect that a call of getPrincipal() is null because the map is empty.
>
> When the map is not empty, I got the first entry of the map as principal. But for instance three users are logged in, how do I got the correct one out of the map?
>
>
> Am 13.04.2024 um 17:53 schrieb lenny@flowlogix.com:
>> Hi,
>>
>> Take a look at Shiro’s SpringBoot 3 example.
>> This should get your started: https://github.com/apache/shiro/blob/main/samples/spring-boot-3-web/src/main/java/org/apache/shiro/samples/HelloController.java <https://github.com/apache/shiro/blob/main/samples/spring-boot-3-web/src/main/java/org/apache/shiro/samples/HelloController.java>
>>
>> Let us know if you have any further questions.
>>
>>> On Apr 13, 2024, at 10:29 AM, Helge Wiemann <hw...@feedbager.de> wrote:
>>>
>>> Hi all,
>>>
>>> I am quite new to Shiro and currently moving from Spring Security to Shiro.
>>>
>>>
>>> One question: After a user is successfully authenticated and authorized, what is the best way to get the user in a Spring controller method?
>>>
>>> With used Spring Security it was something like that:
>>>
>>> publicModelAndView index(*@AuthenticationPrincipalUsernamePasswordAuthenticationTokenauthenticatedUser*,
>>>
>>> HttpServletRequest request, HttpServletResponse response) throwsIOException {
>>>
>>>
>>> Best regards,
>>>
>>>
>>> Helge
>>>
>>
>
Re: get the authenticated user in spring controller method
Posted by Helge Wiemann <hw...@feedbager.de>.
Hi,
thanks for the sample code.
But I questions because I got some problems to understand the code:
Collection<Map> principalMaps =
subject.getPrincipals().byType(Map.class);
if (CollectionUtils.isEmpty(principalMaps)) {
name = subject.getPrincipal().toString();
} else {
name = (String)
principalMaps.iterator().next().get("username");
}
When the map of the principals is empty, why and how do I get then the
logged user by getPrincipal? I would expect that a call of
getPrincipal() is null because the map is empty.
When the map is not empty, I got the first entry of the map as
principal. But for instance three users are logged in, how do I got the
correct one out of the map?
Am 13.04.2024 um 17:53 schrieb lenny@flowlogix.com:
> Hi,
>
> Take a look at Shiro’s SpringBoot 3 example.
> This should get your started: https://github.com/apache/shiro/blob/main/samples/spring-boot-3-web/src/main/java/org/apache/shiro/samples/HelloController.java <https://github.com/apache/shiro/blob/main/samples/spring-boot-3-web/src/main/java/org/apache/shiro/samples/HelloController.java>
>
> Let us know if you have any further questions.
>
>> On Apr 13, 2024, at 10:29 AM, Helge Wiemann <hw...@feedbager.de> wrote:
>>
>> Hi all,
>>
>> I am quite new to Shiro and currently moving from Spring Security to Shiro.
>>
>>
>> One question: After a user is successfully authenticated and authorized, what is the best way to get the user in a Spring controller method?
>>
>> With used Spring Security it was something like that:
>>
>> publicModelAndView index(*@AuthenticationPrincipalUsernamePasswordAuthenticationTokenauthenticatedUser*,
>>
>> HttpServletRequest request, HttpServletResponse response) throwsIOException {
>>
>>
>> Best regards,
>>
>>
>> Helge
>>
>
Re: get the authenticated user in spring controller method
Posted by le...@flowlogix.com.
Hi,
Take a look at Shiro’s SpringBoot 3 example.
This should get your started: https://github.com/apache/shiro/blob/main/samples/spring-boot-3-web/src/main/java/org/apache/shiro/samples/HelloController.java <https://github.com/apache/shiro/blob/main/samples/spring-boot-3-web/src/main/java/org/apache/shiro/samples/HelloController.java>
Let us know if you have any further questions.
> On Apr 13, 2024, at 10:29 AM, Helge Wiemann <hw...@feedbager.de> wrote:
>
> Hi all,
>
> I am quite new to Shiro and currently moving from Spring Security to Shiro.
>
>
> One question: After a user is successfully authenticated and authorized, what is the best way to get the user in a Spring controller method?
>
> With used Spring Security it was something like that:
>
> publicModelAndView index(*@AuthenticationPrincipalUsernamePasswordAuthenticationTokenauthenticatedUser*,
>
> HttpServletRequest request, HttpServletResponse response) throwsIOException {
>
>
> Best regards,
>
>
> Helge
>