-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from nehbehl/patch-50
Update SecurityConfig.java
- Loading branch information
Showing
1 changed file
with
22 additions
and
32 deletions.
There are no files selected for viewing
54 changes: 22 additions & 32 deletions
54
...-master/src/main/java/com/udacity/jwdnd/spring_security_basics/config/SecurityConfig.java
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
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); | ||
} | ||
|
||
|
||
} |