如何基于邮件和使用spring Security的uid从LDAP验证用户?(How to authenticate a user from LDAP based on mail and by uid

编程入门 行业动态 更新时间:2024-10-27 01:33:09
如何基于邮件和使用spring Security的uid从LDAP验证用户?(How to authenticate a user from LDAP based on mail and by uid with spring Security?)

我希望用户能够通过他的uid和邮件登录? 如何使用我的Spring安全配置实现这一目标?

@Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().fullyAuthenticated() .and() .formLogin().passwordParameter("password"); } @Configuration protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter { @Autowired LdapContextSource contextSource; @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth .ldapAuthentication() .userDnPatterns("uid={0}") .contextSource(contextSource); } } }

在userDnPatterns中我可以指定另一个属性和uid吗? 或者用uid进行身份验证是标准的?

I want a user to be able to login both by his uid and by mail ? How to achieve this using my spring Security configuration ?

@Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().fullyAuthenticated() .and() .formLogin().passwordParameter("password"); } @Configuration protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter { @Autowired LdapContextSource contextSource; @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth .ldapAuthentication() .userDnPatterns("uid={0}") .contextSource(contextSource); } } }

At userDnPatterns can I specify another attribute along with uid? Or authentication with uid is standard ?

最满意答案

您需要使用自定义用户搜索过滤器。 以下代码使用OR过滤器尝试将uid或邮件与用户在登录屏幕中输入的值进行匹配:

@Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().fullyAuthenticated() .and() .formLogin().passwordParameter("password"); } @Configuration protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter { @Autowired LdapContextSource contextSource; @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth .ldapAuthentication() .userDnPatterns("uid={0}") .userSearchFilter("(|(uid={0})(mail={0}))") .contextSource(contextSource); } } }

You need to use a custom user search filter. The following code uses an OR filter that tries to match the uid or mail to the value entered by the user in the login screen:

@Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().fullyAuthenticated() .and() .formLogin().passwordParameter("password"); } @Configuration protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter { @Autowired LdapContextSource contextSource; @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth .ldapAuthentication() .userDnPatterns("uid={0}") .userSearchFilter("(|(uid={0})(mail={0}))") .contextSource(contextSource); } } }

更多推荐

本文发布于:2023-08-04 15:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417778.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:邮件   用户   uid   Security   LDAP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!