Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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_20230617
Commits
0a505d9b
Commit
0a505d9b
authored
Jul 30, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SpringSecurity - hasła w bazie
parent
cab20856
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
12 deletions
+75
-12
security.sql
PC27-SklepSpring/sql/security.sql
+55
-0
SecurityConfig.java
...epSpring/src/main/java/sklep/security/SecurityConfig.java
+20
-12
No files found.
PC27-SklepSpring/sql/security.sql
0 → 100644
View file @
0a505d9b
/* Dodatkowe tabele i widoki na potrzeby konfiguracji jdbcAuthentication,
* czyli przechowywanie użytkowników i haseł w bazie danych.
*/
DROP
VIEW
IF
EXISTS
spring_account_roles
;
DROP
VIEW
IF
EXISTS
spring_accounts
;
DROP
TABLE
IF
EXISTS
user_roles
;
DROP
TABLE
IF
EXISTS
users
;
CREATE
TABLE
users
(
user_id
INTEGER
NOT
NULL
,
username
VARCHAR
(
30
)
NOT
NULL
,
password
VARCHAR
(
100
)
NOT
NULL
,
first_name
VARCHAR
(
50
),
last_name
VARCHAR
(
50
),
-- enabled BOOLEAN NOT NULL,
PRIMARY
KEY
(
user_id
),
UNIQUE
(
username
)
);
CREATE
TABLE
user_roles
(
user_id
INTEGER
NOT
NULL
,
role
VARCHAR
(
20
)
NOT
NULL
,
PRIMARY
KEY
(
user_id
,
role
),
FOREIGN
KEY
(
user_id
)
REFERENCES
users
(
user_id
)
);
CREATE
VIEW
spring_accounts
AS
SELECT
username
,
'{noop}'
||
password
AS
password
,
1
AS
enabled
FROM
users
;
CREATE
VIEW
spring_account_roles
AS
SELECT
username
,
'ROLE_'
||
role
AS
role
FROM
user_roles
JOIN
users
USING
(
user_id
);
INSERT
INTO
users
(
user_id
,
username
,
password
,
first_name
,
last_name
)
VALUES
(
1
,
'adam'
,
'abc123'
,
'Adam'
,
'Abacki'
);
INSERT
INTO
users
(
user_id
,
username
,
password
,
first_name
,
last_name
)
VALUES
(
2
,
'bartek'
,
'abc123'
,
'Bartosz'
,
'Borecki'
);
INSERT
INTO
users
(
user_id
,
username
,
password
,
first_name
,
last_name
)
VALUES
(
3
,
'damian'
,
'abc123'
,
'Damian'
,
'Domyślny'
);
INSERT
INTO
user_roles
(
user_id
,
role
)
VALUES
(
1
,
'manager'
);
INSERT
INTO
user_roles
(
user_id
,
role
)
VALUES
(
1
,
'inna_rola'
);
INSERT
INTO
user_roles
(
user_id
,
role
)
VALUES
(
2
,
'pomocnik'
);
-- SELECT * FROM users;
-- SELECT * FROM users LEFT JOIN user_roles USING(user_id);
--
-- SELECT username, password, enabled FROM spring_accounts;
-- SELECT username, role FROM spring_account_roles;
PC27-SklepSpring/src/main/java/sklep/security/SecurityConfig.java
View file @
0a505d9b
package
sklep
.
security
;
package
sklep
.
security
;
import
javax.sql.DataSource
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.security.config.Customizer
;
import
org.springframework.security.config.Customizer
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
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.EnableWebSecurity
;
import
org.springframework.security.core.userdetails.User
;
import
org.springframework.security.provisioning.JdbcUserDetailsManager
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.security.provisioning.InMemoryUserDetailsManager
;
import
org.springframework.security.web.SecurityFilterChain
;
import
org.springframework.security.web.SecurityFilterChain
;
import
jakarta.servlet.DispatcherType
;
import
jakarta.servlet.DispatcherType
;
...
@@ -15,7 +16,9 @@ import jakarta.servlet.DispatcherType;
...
@@ -15,7 +16,9 @@ import jakarta.servlet.DispatcherType;
@Configuration
@Configuration
@EnableWebSecurity
@EnableWebSecurity
public
class
SecurityConfig
{
public
class
SecurityConfig
{
@Autowired
private
DataSource
dataSource
;
// Metoda odpowiedzialna za ogólną konfigurację security aplikacj webowej, w tym:
// Metoda odpowiedzialna za ogólną konfigurację security aplikacj webowej, w tym:
// - reguły autoryzacji, czyli kot może wykonywać jakie zapytania
// - reguły autoryzacji, czyli kot może wykonywać jakie zapytania
// - sposób uwierzytelniania
// - sposób uwierzytelniania
...
@@ -47,14 +50,19 @@ public class SecurityConfig {
...
@@ -47,14 +50,19 @@ public class SecurityConfig {
}
}
// Aspektem konfiguracji, który jest podawany w innej metodzie, jest zdefiniowany zbiór użytkowników.
// Aspektem konfiguracji, który jest podawany w innej metodzie, jest zdefiniowany zbiór użytkowników.
// W tej wersji definiujemy użytkowników w kodzie aplikacji ("in memory").
@Bean
@Bean
public
InMemoryUserDetailsManager
userDetailsService
()
{
JdbcUserDetailsManager
userDetailsService2
()
{
UserDetails
[]
users
=
{
// Użytkownicy zdefiniowany w tabelach bazodanowych.
User
.
withUsername
(
"ala"
).
password
(
"{noop}ala123"
).
roles
(
"manager"
,
"worker"
).
build
(),
JdbcUserDetailsManager
jdbcUserDetailsManager
=
new
JdbcUserDetailsManager
(
dataSource
);
User
.
withUsername
(
"ola"
).
password
(
"{noop}ola123"
).
roles
(
"worker"
).
build
(),
};
return
new
InMemoryUserDetailsManager
(
users
);
}
// Mamy podać zapytanie SQL, które pozwoli Springowi odczytać informacje o userze na podstawie nazwy usera
// w wyniku ma zwrócić rekord z trzeba kolumnami: nazwa, hasło, czy aktywny (0/1)
jdbcUserDetailsManager
.
setUsersByUsernameQuery
(
"SELECT username, password, enabled FROM spring_accounts WHERE username = ?"
);
// dla użytkownika zwraca info o uprawnieniach (rolach) danego użytkownika; wynik może składać się z wielu rekordów
jdbcUserDetailsManager
.
setAuthoritiesByUsernameQuery
(
"SELECT username, role FROM spring_account_roles WHERE username = ?"
);
return
jdbcUserDetailsManager
;
}
}
}
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