Commit 0a505d9b by Patryk Czarnik

SpringSecurity - hasła w bazie

parent cab20856
/* 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;
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;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment