Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
javab_20230928
Commits
143f6e90
Commit
143f6e90
authored
Nov 07, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Poprawka reguł autoryzacji do najnowszego Springa
parent
23837b01
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
11 deletions
+18
-11
SecurityConfig.java
...epSpring/src/main/java/sklep/security/SecurityConfig.java
+18
-11
No files found.
PC30-SklepSpring/src/main/java/sklep/security/SecurityConfig.java
View file @
143f6e90
...
...
@@ -15,6 +15,8 @@ import org.springframework.security.provisioning.JdbcUserDetailsManager;
import
org.springframework.security.web.SecurityFilterChain
;
import
jakarta.servlet.DispatcherType
;
import
org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher
;
import
org.springframework.web.servlet.handler.HandlerMappingIntrospector
;
// https://docs.spring.io/spring-security/reference/servlet/getting-started.html
...
...
@@ -29,23 +31,28 @@ import jakarta.servlet.DispatcherType;
@EnableWebSecurity
public
class
SecurityConfig
{
// Konfiguracja właściwa dla SprSec >= 6.1.2 na podstawie
// https://github.com/spring-projects/spring-security/issues/13568
// błąd opisany https://spring.io/security/cve-2023-34035
@Bean
SecurityFilterChain
configHttpSecurity
(
HttpSecurity
httpSecurity
)
throws
Exception
{
SecurityFilterChain
configHttpSecurity
(
HttpSecurity
httpSecurity
,
HandlerMappingIntrospector
hmi
)
throws
Exception
{
MvcRequestMatcher
.
Builder
mvc
=
new
MvcRequestMatcher
.
Builder
(
hmi
);
httpSecurity
.
authorizeHttpRequests
(
authz
->
authz
.
anyRequest
().
authenticated
()
// zezwalamy na działanie przekierowań wewnętrznych (szablony) i błędów
// .dispatcherTypeMatchers(DispatcherType.FORWARD, DispatcherType.ERROR).permitAll()
// .requestMatchers("/", "/whoami", "/*.css").permitAll()
// .requestMatchers("/hello", "/time").permitAll()
// .requestMatchers("/alt?/**").authenticated() // zalogowany jako ktokolwiek
.
dispatcherTypeMatchers
(
DispatcherType
.
FORWARD
,
DispatcherType
.
ERROR
).
permitAll
()
.
requestMatchers
(
mvc
.
pattern
(
"/"
)).
permitAll
()
.
requestMatchers
(
mvc
.
pattern
(
"/whoami"
)).
permitAll
()
.
requestMatchers
(
mvc
.
pattern
(
"/*.css"
)).
permitAll
()
.
requestMatchers
(
mvc
.
pattern
(
"/hello"
),
mvc
.
pattern
(
"/time"
)).
permitAll
()
.
requestMatchers
(
mvc
.
pattern
(
"/alt?/**"
)).
authenticated
()
// zalogowany jako ktokolwiek
// kolejność reguł ma znaczenie - pierwsza reguła, do której pasuje zapytanie, jest decydująca
// np. /products/new wymaga uprawnień managera, chociaż to zapytanie pasowałoby te z do /products/**
// .requestMatchers("/products/new", "/products/*/edit"
).hasAuthority("ROLE_manager")
// .requestMatchers("/products/**"
).permitAll() // kolejność reguł ma znaczenie
// .requestMatchers("/customers/new", "/customers/*/edit"
).hasRole("manager") // skrót na hasAuthority("ROLE_...")
// .requestMatchers("/customers/**"
).authenticated()
//
.anyRequest().denyAll() // dobra praktyka - odrzucanie pozostałych zapytań; Spring domyślnie wymagałby "authenticated"
.
requestMatchers
(
mvc
.
pattern
(
"/products/new"
),
mvc
.
pattern
(
"/products/*/edit"
)
).
hasAuthority
(
"ROLE_manager"
)
.
requestMatchers
(
mvc
.
pattern
(
"/products/**"
)
).
permitAll
()
// kolejność reguł ma znaczenie
.
requestMatchers
(
mvc
.
pattern
(
"/customers/new"
),
mvc
.
pattern
(
"/customers/*/edit"
)
).
hasRole
(
"manager"
)
// skrót na hasAuthority("ROLE_...")
.
requestMatchers
(
mvc
.
pattern
(
"/customers/**"
)
).
authenticated
()
.
anyRequest
().
denyAll
()
// dobra praktyka - odrzucanie pozostałych zapytań; Spring domyślnie wymagałby "authenticated"
)
.
formLogin
(
Customizer
.
withDefaults
())
// albo .httpBasic(Customizer.withDefaults())
// .csrf(authz -> authz.disable())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment