spring-boot - Spring + LDAP 身份验证 + Google 登录
问题描述
我正在尝试实现LDAP 身份验证和Google 登录,但我不知道如何将有关我的用户的信息从前端发送到后端。
我在前端使用 React.JS,当我使用 npm 的react-google-login包登录时,我会从 Google 获得一些信息(例如电子邮件、全名等)。当我使用 LDAP 登录时,我什么也没有得到(chrome 中显示了一个 JSESSIONID cookie,但 Spring 的响应数据中没有 cookie。)我可以成功登录两者但我怎么知道哪个人是从前端发送请求到后端吗?
在不使用 React 的情况下,如果我从 Chrome 或 Postman 向 spring 发送请求(如果我将 JSESSIONID cookie 添加到 Postman),如果用户使用 LDAP 登录,我可以使用SecurityContextHolder.getContext().getAuthentication()对用户进行身份验证,并且从如果用户使用 Google Sign in 登录,则为 OAuth2USer对象。但问题是如何从 React 发送相同的参数?
我正在寻找从 Spring 获取 JWT 作为响应,因此我只能将 JWT 令牌发送到后端进行授权,但我无法弄清楚如何在 LDAP 身份验证和 Google 身份验证中发送它,并且不知道是否那将是矫枉过正。
到目前为止,我在 Spring 中的代码:
网络安全配置类
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final PasswordEncoder passwordEncoder;
private final UserDetailsServiceImpl userDetailsServiceImpl;
@Autowired
public WebSecurityConfig(PasswordEncoder passwordEncoder, UserDetailsServiceImpl userDetailsService) {
this.passwordEncoder = passwordEncoder;
this.userDetailsServiceImpl = userDetailsService;
}
@Bean
public AuthenticationFailureHandler authenticationFailureHandler() {
return new AuthenticationFailureHandlerImpl();
}
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(passwordEncoder);
provider.setUserDetailsService(userDetailsServiceImpl);
return provider;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers("/user/authenticate", "/login", "/user/me").permitAll()
.antMatchers("/user/add").hasRole(ADMIN.name())
.anyRequest().authenticated()
.and()
.formLogin()
.defaultSuccessUrl("/", true)
.usernameParameter("username")
.passwordParameter("password")
.failureHandler(authenticationFailureHandler())
.and()
.logout()
.logoutUrl("/logout")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
.clearAuthentication(true)
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("/login")
.and()
.oauth2Login();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.authenticationProvider(daoAuthenticationProvider())
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource()
.url("ldap://localhost:8389/dc=springframework,dc=org")
.and()
.passwordCompare()
.passwordEncoder(passwordEncoder)
.passwordAttribute("userPassword");
}
}
在控制器中查找用户的方法
public User findUser(@AuthenticationPrincipal OAuth2User user) {
Optional<User> foundUser;
if (user == null) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
foundUser = userRepository.findUserByUserID(((UserDetailsImpl) auth.getPrincipal()).getUserID());
}
else {
foundUser = userRepository.findUserByEmail(user.getAttributes().get("email").toString());
}
return foundUser.get();
}
解决方案
推荐阅读
- python - 如何从数据框中的列中删除“+1”,如果它包含它?
- python - 如何在kivy中创建一个从文本文件中获取数据的更新列表
- docker - Docker+Nginx 允许来自外部 docker 的上游 gunicorn
- azure-data-factory - 收集经验。映射数据流 ADF 聚合转换中具有不同值的函数
- regex - 需要使用 Notepad++ RegExp 查找和删除大括号之间的字符
- docker - 从 docker hub 拉取 Docker 镜像大小会增加
- loops - Google Script-如果连续 7 个单元格为空白,如何停止循环
- tensorflow - 恢复没有最后一层的tensorflow Resnet50V2模型
- python - 如何加载 JSON 文件并将此 JSON 中的值分配给 Python 代码中的局部变量?
- docker - Docker-compose 错误服务器是否在主机“postgres”(172.18.0.2)上运行并接受