0
votes

Hi i create a implementation of Spring Security 3.1, hibernate 4.1.8,Spring MVC, JSP

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="customUserDetailsService">
        <password-encoder ref="passwordEncoder" />
    </authentication-provider>
</authentication-manager>

but i dont know how to show the other values of login of spring security, ejm lastname_user.

@Service
@Transactional(readOnly=true)
public class CustomUserDetailsService implements UserDetailsService{

@Autowired
UsersDao usersDao;
@Override
public UserDetails loadUserByUsername(String login)
        throws UsernameNotFoundException {
    Users domainUser = usersDao.getUser(login);

    boolean accountNonExpired = true;  
    boolean credentialsNonExpired = true;  
    boolean accountNonLocked = true;  
    return new SecurityUser(  
            domainUser.getLogin(),   
            domainUser.getPassword(),  
            domainUser.getEnabled(),   
            accountNonExpired,   
            credentialsNonExpired,   
            accountNonLocked,  
            getAuthorities(domainUser.getauthorities().getId()),
            domainUser.getLastName_user());  
    }

My SecurityUser

public class SecurityUser extends org.springframework.security.core.userdetails.User{
private static final long serialVersionUID = 1L;
private Users user;
public Users getUser() {
    return user;
}
public void setUser(Users user) {
    this.user = user;
}

public SecurityUser(String username, String password, boolean enabled,
        boolean accountNonExpired, boolean credentialsNonExpired,
        boolean accountNonLocked,
        Collection<? extends GrantedAuthority> authorities,String lastname_user) {
    super(username, password, enabled, accountNonExpired, credentialsNonExpired,
            accountNonLocked, authorities);
}

I can login, and show the user with #{request.userPrincipal.name}, but I want to use the @Controller and (UserDetails)SecurityContextHolder.getContext().getAuthentication(). getPrincipal(); to show other values of my user login via controller/jsp, the examples in internet use System.out.println but there is a way to show in a table, thnks.

1
Do you want to show additional user information via Controller/JSP? - Christian Müller
yes, the domainUser.getLastName_user() for example. - Vodo-Siosk Baas

1 Answers

1
votes

I guess you have a standard Spring MVC setup. In case you need configuration support have a look at this: http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/

Create/extend a controller

@Controller
public final class ExampleController {

    @Autowired
    protected UserDetailsService userService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index(ModelAndView mav) {

           UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication(). getPrincipal();

           mav.add("user", user);
           return mav;
    }

}

Create the index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<h1>This is my famous index page!</h1>

<c:when test="${not empty user}">
<table>
<th>
    <td>Username</td>
    <td>Enabled</td>
    ...
</th>
<tr>
    <td>${user.username}</td>
    <td>${user.enabled}</td>
    ...
</tr>
</table>
</c:when>

Have fun!

Edit: In case you want to load the detailed version of the user object from the database - may containing additional information about the user e.g. gender, ... - extend the UserDetailService, add a method which returns the Users object (e.g.:

public Users loadUserByUsernameDetail(String login) {
    return usersDao.getUser(login);  
}

and add this object to the ModelAndView (ExampleController):

UserDetails user = (UserDetails)SecurityContextHolder.getContext().getAuthentication(). getPrincipal();
Users users = userService.loadUserByUsernameDetail(user.getUsername);
mav.add("user", users);