Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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
java_dzienna_15_09
Commits
1c6de684
Commit
1c6de684
authored
Oct 05, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
jdbcAuthentication - hasła w bazie
parent
a4aef070
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
3 deletions
+68
-3
security.sql
PC30-SklepSpring/sql/security.sql
+55
-0
SecurityConfig.java
...epSpring/src/main/java/sklep/security/SecurityConfig.java
+13
-3
No files found.
PC30-SklepSpring/sql/security.sql
0 → 100644
View file @
1c6de684
/* 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;
PC30-SklepSpring/src/main/java/sklep/security/SecurityConfig.java
View file @
1c6de684
package
sklep
.
security
;
package
sklep
.
security
;
import
javax.sql.DataSource
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
...
@@ -11,6 +14,9 @@ import org.springframework.security.web.SecurityFilterChain;
...
@@ -11,6 +14,9 @@ import org.springframework.security.web.SecurityFilterChain;
@Configuration
@Configuration
public
class
SecurityConfig
{
public
class
SecurityConfig
{
@Autowired
private
DataSource
dataSource
;
// W klasie typu Configuration umieszczamy metody, które w wyniku zwracają obiekty, które dla Springa coś specjalnego znaczą.
// W klasie typu Configuration umieszczamy metody, które w wyniku zwracają obiekty, które dla Springa coś specjalnego znaczą.
// Te metody oznaczamy adnotacją @Bean.
// Te metody oznaczamy adnotacją @Bean.
// (w ten sposób można też stworzyć "własne" beany, zamiast podejścia z adnotacją @Component)
// (w ten sposób można też stworzyć "własne" beany, zamiast podejścia z adnotacją @Component)
...
@@ -45,9 +51,13 @@ public class SecurityConfig {
...
@@ -45,9 +51,13 @@ public class SecurityConfig {
};
};
return
authenticationConfiguration
.
authenticationManagerBuilder
(
objectPostProcessor
,
applicationContext
)
return
authenticationConfiguration
.
authenticationManagerBuilder
(
objectPostProcessor
,
applicationContext
)
.
inMemoryAuthentication
()
.
jdbcAuthentication
()
.
withUser
(
"ala"
).
password
(
"{noop}kot"
).
roles
(
"manager"
,
"innarola"
).
and
()
.
dataSource
(
dataSource
)
.
withUser
(
"ola"
).
password
(
"{noop}pies"
).
roles
(
"pomocnik"
).
and
()
// 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)
.
usersByUsernameQuery
(
"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
.
authoritiesByUsernameQuery
(
"SELECT username, role FROM spring_account_roles WHERE username = ?"
)
.
and
()
.
and
()
.
build
();
.
build
();
}
}
...
...
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