A robust starter web application to ease Java webapp development.

Home | Tutorials | Demos | Issues

How to get user from session

View: New views
4 Messages — Rating Filter:   Alert me  

How to get user from session

by krishgy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All,

I am just beginning the java for a web based application. After some struggle, I found appfuse and moven.

I have basic appfuse-struts-spring-hibernate project.

I wanted to get the user loggined in my action classes and JSP pages. How to do this?

Regards,

Krish

Re: How to get user from session

by Allan Ang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

use

String username = this.getRequest().getRemoteUser();

in your action class.

allan


krishgy wrote:
Hi All,

I am just beginning the java for a web based application. After some struggle, I found appfuse and moven.

I have basic appfuse-struts-spring-hibernate project.

I wanted to get the user loggined in my action classes and JSP pages. How to do this?

Regards,

Krish



Re: How to get user from session

by alibehzadian :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If you want to get current logged in user, use the code snippet below:

        User currentUser = null;
        SecurityContext ctx = SecurityContextHolder.getContext();
        if (ctx.getAuthentication() != null) {
            Authentication auth = ctx.getAuthentication();
            if (auth.getPrincipal() instanceof UserDetails) {
                currentUser = (User) auth.getPrincipal();
            } else if (auth.getDetails() instanceof UserDetails) {
                currentUser = (User) auth.getDetails();
            } else {
                throw new AccessDeniedException("User not properly authenticated.");
            }
        }

Ali Behzadian Nejad.

Re: How to get user from session

by jesperForum :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


alibehzadian wrote:
If you want to get current logged in user, use the code snippet below:
        User currentUser = null;
        SecurityContext ctx = SecurityContextHolder.getContext();
        if (ctx.getAuthentication() != null) {
            Authentication auth = ctx.getAuthentication();
            if (auth.getPrincipal() instanceof UserDetails) {
                currentUser = (User) auth.getPrincipal();
            } else if (auth.getDetails() instanceof UserDetails) {
                currentUser = (User) auth.getDetails();
            } else {
                throw new AccessDeniedException("User not properly authenticated.");
            }
        }
Ali Behzadian Nejad.
as you told that you're using spring then perhaps you can do as same that I do, that I create a class, I put it in src/service/.../util/, I call it UserUtil because I call it often in my application, it return the logged in user, with parameter UserManager that already appfuse had. and this is my full code of that class:

public final class UserUtil {

        public synchronized static User getCurrentUser(UserManager mgr) {
                Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                String username;
                if (obj instanceof UserDetails) {
                        username = ((UserDetails) obj).getUsername();
                } else {
                        username = obj.toString();
                }

                return username != null ? mgr.getUserByUsername(username) : null;
        }
}

HTH
LightInTheBox - Buy quality products at wholesale price