Spring OAuth2 вдоль локальной аутентификации - PullRequest
1 голос
/ 19 марта 2019

Я хотел бы добавить второй метод аутентификации (OAuth2) в мое загрузочное приложение Spring.

До сих пор я реализовал локальную аутентификацию (имя пользователя + пароль, хранящиеся в моей собственной базе данных), и она отлично работает.

@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

в моём приложении.

Это работает, но это переопределяет мою локальную аутентификацию (/ login перенаправляет на fb вместо страницы login.html).

Есть ли (простой) способ сохранить обе стратегии аутентификации?

Заранее спасибо!

...