Skip to content

Commit

Permalink
Update SecurityConfig.java
Browse files Browse the repository at this point in the history
Adding SecurityFilterChain implementation to make code compatible with springboot 3.3.5 and java 21.
  • Loading branch information
nehbehl authored Nov 7, 2024
1 parent 153523e commit 7ecf186
Showing 1 changed file with 25 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
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.web.SecurityFilterChain;

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

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
public class SecurityConfig {

private AuthenticationService authenticationService;
final private AuthenticationService authenticationService;

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

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {


http
.authorizeHttpRequests(requests -> requests
.requestMatchers("/signup", "/css/**", "/js/**").permitAll()
.anyRequest().authenticated()
)


.formLogin(formLogin ->
formLogin.loginPage("/login")
.permitAll()
.defaultSuccessUrl("/home", true)
)
.authenticationProvider(authenticationService);

return http.build();

@Override
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(this.authenticationService);
}

@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);
}


Expand Down

0 comments on commit 7ecf186

Please sign in to comment.