【问题标题】:Display user details in jsp using spring security使用spring security在jsp中显示用户详细信息
【发布时间】:2019-01-01 22:11:48
【问题描述】:

我已经在 Spring Boot 中实现了一个带有 Spring Security 的应用程序。我需要在成功登录后在jsp页面中显示用户的名字,姓氏和图像路径(用于标题,即使我们导航到另一个URL也意味着全局可用)强>。我正在使用remember-me 使用PersistentLogin。所以我不能使用会话来存储详细信息。因为如果我关闭浏览器,会话将被销毁。

我已经实现了CustomUserDetailsService,它返回org.springframework.security.core.userdetails.User

@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService{
    //codes
    return new org.springframework.security.core.userdetails.User(
        username,
        password,
        enabled,
        accountNonExpired,
        credentialsNonExpired,
        accountNonLocked,
        authorities);
}

我知道有两个限制

  • 如果我不使用记住我,我可以轻松地在会话中存储。
  • 如果我在CustomUserDetailsService ...中返回User模型类,我可以很容易地在jsp中获取用户详细信息 页面使用<security:authentication property="principal.firstName"> jsp中的标签。但我需要返回org.springframework.security.core.userdetails.User

不幸的是,我需要这两个限制。我的User 模型类有firstName, lastName, imagePath,.. etc.

如何在 jsp 页面中显示用户详细信息?有什么可用的方法吗?提前致谢。

【问题讨论】:

    标签: spring spring-mvc jsp spring-boot spring-security


    【解决方案1】:

    Spring 内置提供了同样的解决方案。

    Java 代码:

    public User getCurrentUser() {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth != null) {
                Object principal = auth.getPrincipal();
                if (principal instanceof User) {
                    return ((User) principal);
                }
            }
    
        }
    

    JSP 代码:

    ${pageContext["request"].userPrincipal.principal}
    

    【讨论】:

    • 您可以创建一个实用程序类并在任何您想获取登录用户详细信息的地方使用它。
    • 对不起,我用那个jsp页面做标题。所以一旦我成功通过身份验证,我需要显示它,即使我也导航到另一个页面,标题也会在那里。
    • 是的,在标头 jsp 页面中,您使用 ${pageContext["request"].userPrincipal.principal} 获取详细信息。
    【解决方案2】:

    我所做的是,我创建了一个User 的原型,名为UserAuth

    public class UserAuth extends org.springframework.security.core.userdetails.User{
        private String firstName;
        private String lastName;
        private String imagePath;
    
        public UserAuth(String username, String password, boolean enabled,
                boolean accountNonExpired, boolean credentialsNonExpired,
                boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities,
                 String firstName, String lastName, String imagePath) {
    
                     super(username, password, enabled, accountNonExpired,
                        credentialsNonExpired, accountNonLocked, authorities);
    
                     this.firstName = firstName;
                     this.lastName = lastName;
                     this.imagePath = imagePath;
             }
    
        //getters and setters   
    
    }
    

    在CustomeUserDetailsS​​ervice中

    @Service("customUserDetailsService")
    public class CustomUserDetailsService implements UserDetailsService{
        //codes
        return UserAuth
            username,
            password,
            enabled,
            accountNonExpired,
            credentialsNonExpired,
            accountNonLocked,
            authorities,
            user.getFirstName(),
            user.getLastName(),
            user.getImagePath());
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 2013-07-23
      • 2014-11-30
      • 2017-06-05
      • 2015-12-03
      • 1970-01-01
      相关资源
      最近更新 更多