Skip to content

Commit

Permalink
spring boot 3.1 migration - taking care of deprecated methods + using…
Browse files Browse the repository at this point in the history
… rewrite-maven-plugin
  • Loading branch information
gtiwari333 committed Sep 23, 2023
1 parent 268057b commit b8e8212
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
<version>3.1.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

Expand Down
32 changes: 0 additions & 32 deletions src/main/java/gt/app/config/security/MethodSecurityConfig.java

This file was deleted.

24 changes: 18 additions & 6 deletions src/main/java/gt/app/config/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
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.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
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;

import java.util.stream.Stream;

import static org.springframework.security.web.util.matcher.AntPathRequestMatcher.antMatcher;

@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
@EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true)
public class SecurityConfig {

private static final String[] AUTH_WHITELIST = {
"/swagger-resources/**",
"/v3/api-docs/**",
"/h2-console/**",
"/webjars/**",
"/static/**",
"/error/**",
Expand All @@ -30,13 +38,17 @@ public class SecurityConfig {
"/" //landing page is allowed for all
};


@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
protected SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector introspector) throws Exception {
var mvcH2Console = new MvcRequestMatcher.Builder(introspector).servletPath("/h2-console");
http.headers(h -> h.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))
.authorizeHttpRequests(ah -> ah.requestMatchers(AUTH_WHITELIST).permitAll()
.requestMatchers("/admin/**").hasAuthority(Constants.ROLE_ADMIN)
.requestMatchers("/user/**").hasAuthority(Constants.ROLE_USER)
.requestMatchers("/api/**").authenticated()//individual api will be secured differently
.authorizeHttpRequests(ah -> ah
.requestMatchers(Stream.of(AUTH_WHITELIST).map(AntPathRequestMatcher::antMatcher).toList().toArray(new AntPathRequestMatcher[0])).permitAll()
.requestMatchers(mvcH2Console.pattern("/**")).permitAll()
.requestMatchers(antMatcher("/admin/**")).hasAuthority(Constants.ROLE_ADMIN)
.requestMatchers(antMatcher("/user/**")).hasAuthority(Constants.ROLE_USER)
.requestMatchers(antMatcher("/api/**")).authenticated()//individual api will be secured differently
.anyRequest().authenticated())
.csrf(AbstractHttpConfigurer::disable)
.formLogin(f -> f.loginProcessingUrl("/auth/login")
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ spring:
mvc:
static-path-pattern: /static/**
jpa:
show-sql: true
show-sql: false
datasource:
url: jdbc:h2:mem:testdb
h2:
console:
enabled: true #Access from http://localhost:8080/h2-console/
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ logging.level:
org.springframework.security: INFO
org.springframework.security.web: INFO
org.hibernate: INFO
ROOT: WARN
ROOT: INFO
gt: DEBUG


Expand Down

0 comments on commit b8e8212

Please sign in to comment.