Skip to content

Instantly share code, notes, and snippets.

@rajeevshukla
Last active May 1, 2020 06:11
Show Gist options
  • Select an option

  • Save rajeevshukla/964a2cd7a981731d1997427cfd24b849 to your computer and use it in GitHub Desktop.

Select an option

Save rajeevshukla/964a2cd7a981731d1997427cfd24b849 to your computer and use it in GitHub Desktop.
Spring Security Config for Form based as well as OAuth2
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and().formLogin() // enable form based login
.loginPage("/login").defaultSuccessUrl("/formLoginSuccess")
.and().logout() // enable logout
.and().oauth2Login() // enable OAuth2
.loginPage("/login").defaultSuccessUrl("/oauth2LoginSuccess")
.and().csrf().disable(); // disable CSRF
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// Adding in memory User for form based login
auth.inMemoryAuthentication()
.passwordEncoder(NoOpPasswordEncoder.getInstance())
.withUser("admin")
.password("admin")
.roles("USER");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment