Skip to content

Commit

Permalink
fix spring security
Browse files Browse the repository at this point in the history
  • Loading branch information
persapiens authored Jul 29, 2023
1 parent 0f1390e commit 49ae252
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/main/java/org/joinfaces/example/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;

/**
* Spring Security Configuration.
Expand All @@ -45,31 +49,36 @@ public class SecurityConfig {
**/
@SuppressFBWarnings("SPRING_CSRF_PROTECTION_DISABLED")
@Bean
public SecurityFilterChain configure(HttpSecurity http) {
public SecurityFilterChain configure(HttpSecurity http, MvcRequestMatcher.Builder mvc) {
try {
http.csrf().disable();
http.csrf((csrf) -> csrf.disable());
http
.authorizeHttpRequests((authorize) -> authorize
.requestMatchers("/").permitAll()
.requestMatchers("/**.jsf").permitAll()
.requestMatchers("/jakarta.faces.resource/**").permitAll()
.requestMatchers(mvc.pattern("/")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/**.faces")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/jakarta.faces.resource/**")).permitAll()
.anyRequest().authenticated())
.formLogin()
.loginPage("/login.jsf")
.permitAll()
.failureUrl("/login.jsf?error=true")
.defaultSuccessUrl("/starter.jsf")
.and()
.logout()
.logoutSuccessUrl("/login.jsf")
.deleteCookies("JSESSIONID");
.formLogin((formLogin) ->
formLogin.loginPage("/login.faces")
.permitAll()
.failureUrl("/login.faces?error=true")
.defaultSuccessUrl("/starter.faces"))
.logout((logout) ->
.logoutSuccessUrl("/login.faces")
.deleteCookies("JSESSIONID"));
return http.build();
}
catch (Exception ex) {
throw new BeanCreationException("Wrong spring security configuration", ex);
}
}

@Scope("prototype")
@Bean
MvcRequestMatcher.Builder mvc(HandlerMappingIntrospector introspector) {
return new MvcRequestMatcher.Builder(introspector);
}

/**
* UserDetailsService that configures an in-memory users store.
* @param applicationUsers - autowired users from the application.yml file
Expand Down

0 comments on commit 49ae252

Please sign in to comment.