Last active
February 6, 2021 16:28
-
-
Save rajj6/5c38d4713a11a8bb0582e9efdbc96c81 to your computer and use it in GitHub Desktop.
This file contains 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
package com.raj.securingweb; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.security.authentication.AuthenticationProvider; | |
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | |
import org.springframework.security.core.userdetails.UserDetailsService; | |
import org.springframework.security.crypto.password.NoOpPasswordEncoder; | |
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | |
@Configuration | |
@EnableWebSecurity | |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | |
@Autowired | |
private UserDetailsService userDetailsService; | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http | |
.csrf().disable() | |
.authorizeRequests().antMatchers("/", "/home", "/login", "/logout-success").permitAll() | |
.and() | |
.formLogin() | |
.loginPage("/login").permitAll() | |
.and() | |
.logout().invalidateHttpSession(true) | |
.clearAuthentication(true) | |
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")) | |
.logoutSuccessUrl("/home").permitAll(); | |
} | |
@Bean | |
public AuthenticationProvider authenticationProvider(){ | |
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); | |
provider.setUserDetailsService(userDetailsService); | |
provider.setPasswordEncoder(NoOpPasswordEncoder.getInstance()); | |
return provider; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment