gpt4 book ai didi

spring-security - Spring Security Java Config - 自定义 AuthenticationProvider 和 UserDetailsS​​ervice

转载 作者:行者123 更新时间:2023-12-04 07:53:27 28 4
gpt4 key购买 nike

我使用java配置来配置Spring Security,并且我自定义了AuthenticationProvider和自定义了UserDetailsS​​ervice,在下面添加了额外的登录字段
http://forum.spring.io/forum/spring-projects/security/95715-extra-login-fields

我很难通过使用 java 配置将这两个自定义类添加到 Spring Security 框架中。
正如 AuthenticationProvider#authenticationProvider 的 java 文档所描述的:

Add authentication based upon the custom AuthenticationProvider that is passed in. Since the AuthenticationProvider implementation is unknown, all customizations must be done externally and the AuthenticationManagerBuilder is returned immediately.

This method does NOT ensure that the UserDetailsService is available for the getDefaultUserDetailsService() method.



所以我的问题是在这种情况下设置 UserDetailsS​​ervice 的方法是什么?

最佳答案

以下是自定义 AuthenticationProvider 和自定义 UserDetailsS​​ervice 的示例:

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider());
}

@Bean
AuthenticationProvider customAuthenticationProvider() {
CustomAuthenticationProvider impl = new CustomAuthenticationProvider();
impl.setUserDetailsService(customUserDetailsService());
/* other properties etc */
return impl ;
}

@Bean
UserDetailsService customUserDetailsService() {
/* custom UserDetailsService code here */
}
}

关于spring-security - Spring Security Java Config - 自定义 AuthenticationProvider 和 UserDetailsS​​ervice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276152/

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