java - Spring WebSecurityConfigurerAdapter API

标签 java spring spring-security

过去几天我一直在玩 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/

相关文章:

java - 从正则表达式在 Android 中创建对象

java - 如何在java中调用super之前运行一个函数?

java - 如何在android TextView(answerview)中显示分数而不是小数( double )

grails - bcrypt日志回合与哈希迭代

java - JBOSS网站开发

spring - 如何从 POST 请求中获取响应对象?

javascript - 如果在 spring mvc 表单中触发提交按钮的点击函数会发生什么

java - 在struts和spring mvc中使用前端 Controller 模式

Spring 安全 : custom userdetails

java - Spring Security 将 URL 参数传递给身份验证提供程序