如何获取登录用户SpringSecurity AngularJS的用户信息

目前,我正在使用以下内容将用户登录到我的应用程序.但是,我想使用角度函数来实际执行登录.为此,我想创建一个REST Web服务进行身份验证,但是我在SO上看到的所有示例都使用了我认为已弃用的User.我还希望该服务返回有关用户的信息.

我要问的不足是如何更改MyUserDetailsS​​ervice以用作登录的静态服务,或者如何创建可用于登录的服务,该服务将在登录后返回用户对象.

<form class="navbar-form" action="/j_spring_security_check" method="post">
   <input class="span2" name="j_username"  type="text" placeholder="Email">
   <input class="span2" name="j_password" type="password" placeholder="Password">
   <input type="submit" class="btn btn-primary" value="Log in"/>
</form>

这是我的authenticationManager

<authentication-manager>
    <authentication-provider user-service-ref="MyUserDetailsService">
        <password-encoder ref="passwordEncoder"/>
    </authentication-provider>
</authentication-manager>

这是我当前用于登录的用户详细信息服务.

@Service("MyUserDetailsService")
public class MyUserDetailsService implements UserDetailsService {
    private static final Logger logger = LoggerFactory.getLogger(MyUserDetailsService.class);

    private UserManager userManager;

    @Autowired
    public MyUserDetailsService(UserManager userManager) {
        this.userManager = userManager;
    }
    @Override
    @Transactional
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException,DataAccessException {
        if ((email == null) || email.trim().isEmpty()) {
            throw new UsernameNotFoundException("Email is null or empty");
        }
        logger.debug("Checking users with email: " + email);

        Users users = userManager.findByEmail(email);

        if (users == null) {
            String errorMsg = "User with email: " + email + " could not be found";
            logger.debug(errorMsg);
            throw new UsernameNotFoundException(errorMsg);
        }

        Collection<GrantedAuthority> grantedAuthorities = toGrantedAuthorities(users.getRoleNames());
        String password = users.getPassword();
        boolean enabled = users.isEnabled();
        boolean userNonExpired = true;
        boolean credentialsNonExpired = true;
        boolean userNonLocked = true;

        return new User(email,password,enabled,userNonExpired,userNonLocked,credentialsNonExpired,grantedAuthorities);
    }

    public static Collection<GrantedAuthority> toGrantedAuthorities(List<String> roles) {
        List<GrantedAuthority> result = newArrayList();

        for (String role : roles) {
            result.add(new SimpleGrantedAuthority(role));
        }

        return result;
    }
}
最佳答案
您可以在View上使用一些JSTL tags for Spring Security来执行某些操作.如果不是JSTL选项,则无论出于何种原因,都可以执行以下操作:

${pageContext.request.userPrincipal.principal.yourCustomProperty}

另外,您可以在控制器中获取主体并在模型上进行设置.

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...