Spring登录showuser信息

时间:2014-03-17 06:49:46

标签: java spring hibernate spring-mvc spring-security

您好我创建了一个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>

但我不知道如何显示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());  
    }

我的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);
}

我可以登录,并向用户显示#{request.userPrincipal.name},但我想使用@Controller和(UserDetails)SecurityContextHolder.getContext()。getAuthentication()。 getPrincipal();通过controller / jsp显示我的用户登录的其他值,互联网中的示例使用System.out.println但是有一种方法可以在表格中显示,但是。

1 个答案:

答案 0 :(得分:1)

我猜你有一个标准的Spring MVC设置。如果您需要配置支持,请查看:http://fruzenshtein.com/spring-mvc-creation-of-simple-controller-with-java-based-config/

创建/扩展控制器

@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;
    }

}

创建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>

玩得开心!

编辑:如果您想从数据库加载用户对象的详细版本 - 可能包含有关用户的其他信息,例如性别,... - 扩展 UserDetailService ,添加一个返回 Users 对象的方法(例如:

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

并将此对象添加到 ModelAndView ExampleController ):

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