Skip to content

Commit

Permalink
Merge pull request #62 from nehbehl/patch-50
Browse files Browse the repository at this point in the history
Update SecurityConfig.java
  • Loading branch information
SudKul authored Nov 21, 2024
2 parents 4abacf8 + 0636330 commit 6b915b4
Showing 1 changed file with 22 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,38 @@
package com.udacity.jwdnd.spring_security_basics.config;

import com.udacity.jwdnd.spring_security_basics.mapper.UserMapper;
import com.udacity.jwdnd.spring_security_basics.service.AuthenticationService;
import com.udacity.jwdnd.spring_security_basics.service.HashService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;

import java.util.ArrayList;
import com.udacity.jwdnd.spring_security_basics.service.AuthenticationService;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private AuthenticationService authenticationService;
public class SecurityConfig {
private final AuthenticationService authenticationService;

public SecurityConfig(AuthenticationService authenticationService) {
this.authenticationService = authenticationService;
}

@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(this.authenticationService);
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.formLogin(httpForm ->{
httpForm.loginPage("/login").permitAll();
httpForm.defaultSuccessUrl("/home");

})
.authorizeHttpRequests(registry ->{
registry.requestMatchers("/signup","/css/**","/js/**").permitAll();
registry.anyRequest().authenticated();
})
.authenticationProvider(authenticationService)
.build();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/signup", "/css/**", "/js/**").permitAll()
.anyRequest().authenticated();

http.formLogin()
.loginPage("/login")
.permitAll();

http.formLogin()
.defaultSuccessUrl("/home", true);
}


}

0 comments on commit 6b915b4

Please sign in to comment.