Last active
May 1, 2020 06:11
-
-
Save rajeevshukla/964a2cd7a981731d1997427cfd24b849 to your computer and use it in GitHub Desktop.
Spring Security Config for Form based as well as OAuth2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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