[英]In spring security how to implement both SAML and custom authentication handler
[英]Implement Custom Spring Security Authentication Method
我正在构建一个带有用户登录机制的Spring MVC Web应用程序。 我已经使用spring-boot来设置我的应用程序。 要使用数据库对用户进行身份验证,我遵循以下教程:
http://justinrodenbostel.com/2014/05/30/part-5-integrating-spring-security-with-spring-boot-web/
在这里,使用spring的内置身份验证过程。 通过指定
auth.jdbcAuthentication().dataSource(datasource);
Spring安全性检查用户和授权表并验证用户身份。
我想覆盖此默认行为,因为我没有(不需要)身份验证表。 另外,我的用户表中的列比标准的三列(用户名,密码和已启用)多得多。
如何覆盖默认实现?
另外,在用户登录后,如何获取有关该用户的信息?
谢谢!
您可以创建自定义AuthenticationProvider
或将DaoAuthenticationProvider
与自定义UserDetailsService
实现一起使用。
这是第二种解决方案的Spring Java配置类示例:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
// ...
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setPasswordEncoder(new ShaPasswordEncoder());
authenticationProvider.setUserDetailsService(userService);
return authenticationProvider;
}
}
您的UserDetailsService
接口的实现将包含特定于您的项目域的逻辑,用于按用户名检索用户。
如果您需要更详细的示例,请在下面留下评论,我将更新答案,但这应该可以为您提供总体思路。
我也建议您通读上述Spring类和接口的JavaDocs。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.