Spring OAuth2和本地身份验证

时间:2019-03-19 12:01:03

标签: spring authentication oauth

我想在我的Spring启动应用程序中添加第二种身份验证方法(OAuth2)。

到目前为止,我已经实现了本地身份验证(用户名+密码存储在我自己的数据库中),并且运行良好。

@Configuration
@EnableWebSecurity
@Order(1000)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Bean
    public static BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    // Enable jdbc authentication
    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/")
                .permitAll()
            .antMatchers("/welcome")
                .hasAnyRole("USER", "ADMIN")
            .antMatchers("/getEmployees")
                .hasAnyRole("USER", "ADMIN")
            .antMatchers("/addNewEmployee")
                .hasAnyRole("ADMIN")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll()
                .and()
            .httpBasic();

        http.csrf().disable();
    }
}

对于OAuth2,我发现只需要投放 @ EnableOAuth2Sso

 facebook.app.id=xxx
 facebook.app.secret=xxx

以我的申请专有权。

它可以工作,但这会覆盖我的本地身份验证(/登录重定向到fb而不是login.html页面)。

是否有一种(简单的)方法来保留两种身份验证策略?

谢谢!

0 个答案:

没有答案
相关问题