gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:11:09 27 4
gpt4 key购买 nike

目前我正在使用以下方法将用户登录到我的应用程序。但是我想使用 Angular 函数来实际执行登录。为此,我想创建一个休息 Web 服务来进行身份验证,但是我在 SO 上看到的所有示例都使用我认为已折旧的用户。我还希望该服务返回有关用户的信息。

我要问的简短内容是如何更改 MyUserDetailsS​​ervice 以用作登录的 Restful 服务,或者如何创建可用于登录的服务,该服务将在登录后返回用户对象中。

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

这是我的身份验证管理器

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

最佳答案

有一些JSTL tags for Spring Security您可以在 View 上使用它来做一些事情。如果 JSTL 不是一个选项,无论出于何种原因,您都可以这样做:

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

此外,您可以在 Controller 中获取主体并将其设置在模型上。

关于spring - 如何获取登录用户的用户信息 SpringSecurity + AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15296113/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com