过去几天我一直在玩 Spring,事情变得很有趣。我现在正在与安全部门合作,但遇到了一些小问题。基本上,我希望通过 API 调用而不是表单进行身份验证。有没有一种巧妙的方法来做到这一点?
我已经像这样扩展了 WebSecurityConfigurerAdapter -
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.antMatchers("/openapi/**").hasRole("USER")
.anyRequest().authenticated();
http
.formLogin()
.defaultSuccessUrl("/hello")
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
authManagerBuilder.inMemoryAuthentication()
.withUser("manager").password("password").roles("MANAGER");
}
}
有没有办法从数据库中获取用户名和密码,并且我可以通过 API 调用执行身份验证吗?
最佳答案
Spring Security 3 database authentication with Hibernate
这看起来很有希望。它需要创建一个自定义身份验证管理器。
关于java - Spring WebSecurityConfigurerAdapter API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20758479/