티스토리 뷰
> Creating a Custom Spring Security Solutiom
> Components for a Custom Spring Security Solutoion
- Spring Security Dependency (Maven or Gradle)
- Entiry Model
- Data Repositoyr
- Business Service
- User Details Service
- Authentication Provider
- Security Configuration
> Spring Security Dependency
> Entiry Model
@Entity
public class Role {
@Id
private Long id;
@NotNull
private String code;
@NotNull
private String label;
public Role(){
}
CREATE TABLE Account (
id BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,
referenceId VARCHAR(255) NOT NULL,
username VARCHAR(100) NOT NULL,
password VARCHAR(200) NOT NULL,
enabled BOOLEAN DEFAULT true NOT NULL,
credentialsexpired BOOLEAN DEFAULT false NOT NULL,
expired BOOLEAN DEFAULT false NOT NULL,
locked BOOLEAN DEFAULT false NOT NULL,
version INT NOT NULL,
createdBy VARCHAR(100) NOT NULL,
createdAt DATETIME NOT NULL,
updatedBy VARCHAR(100) DEFAULT NULL,
updatedAt DATETIME DEFAULT NULL,
PRIMARY KEY (id),
CONSTRAINT UQ_Account_ReferenceId UNIQUE (referenceId),
CONSTRAINT UQ_Account_Username UNIQUE (username)
);
CREATE TABLE Role (
id BIGINT NOT NULL,
code VARCHAR(50) NOT NULL,
label VARCHAR(100) NOT NULL,
ordinal INT NOT NULL,
effectiveAt DATETIME NOT NULL,
expiresAt DATETIME DEFAULT NULL,
createdAt DATETIME NOT NULL,
PRIMARY KEY (id),
CONSTRAINT UQ_Role_Code UNIQUE (code)
);
CREATE TABLE AccountRole (
accountId BIGINT NOT NULL,
roleId BIGINT NOT NULL,
PRIMARY KEY (accountId, roleId),
CONSTRAINT FK_AccountRole_AccountId FOREIGN KEY (accountId) REFERENCES Account (id),
CONSTRAINT FK_AccountRole_RoleId FOREIGN KEY (roleId) REFERENCES Role (id)
);
-- password is 'password'
INSERT INTO Account (referenceId, username, password, enabled, credentialsexpired, expired, locked, version, createdBy, createdAt, updatedBy, updatedAt) VALUES ('a07bd221-3ecd-4893-a0f0-78d7c0fbf94e', 'user', '$2a$10$9/44Rne7kQqPXa0cY6NfG.3XzScMrCxFYjapoLq/wFmHz7EC9praK', true, false, false, false, 0, 'user', NOW(), NULL, NULL);
-- password is 'operations'
INSERT INTO Account (referenceId, username, password, enabled, credentialsexpired, expired, locked, version, createdBy, createdAt, updatedBy, updatedAt) VALUES ('7bd137c8-ab64-4a45-bf2d-d9bae3574622', 'operations', '$2a$10$CoMVfutnv1qZ.fNlHY1Na.rteiJhsDF0jB1o.76qXcfdWN6As27Zm', true, false, false, false, 0, 'user', NOW(), NULL, NULL);
INSERT INTO Role (id, code, label, ordinal, effectiveAt, expiresAt, createdAt) VALUES (1, 'ROLE_USER', 'User', 0, '2015-01-01 00:00:00', NULL, NOW());
INSERT INTO Role (id, code, label, ordinal, effectiveAt, expiresAt, createdAt) VALUES (2, 'ROLE_ADMIN', 'Admin', 1, '2015-01-01 00:00:00', NULL, NOW());
INSERT INTO Role (id, code, label, ordinal, effectiveAt, expiresAt, createdAt) VALUES (3, 'ROLE_SYSADMIN', 'System Admin', 2, '2015-01-01 00:00:00', NULL, NOW());
INSERT INTO AccountRole (accountId, roleId) SELECT a.id, r.id FROM Account a, Role r WHERE a.username = 'user' and r.id = 1;
INSERT INTO AccountRole (accountId, roleId) SELECT a.id, r.id FROM Account a, Role r WHERE a.username = 'operations' and r.id = 3;
> Data Repository
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
Account findByUsername(String username);
}
> Business Service
@Service
public class AccountService {
@Autowired
AccountRepository accountRepository;
public Account findByUsername(String username){
return accountRepository.findByUsername(username);
}
}
> User Detail Service
> Common Application Layers
- Entity Model
- Data Repositories (DAO)
- Business Services
- Facade (or Orchestration Layer)
- Web Controller
- Security
> Benefits of Technical Planning and Design
- ↑Resue
- ↑Cohension
- ↑Maintainablility
- ↑Flexibility = Agility
- ↓Cost
> Authentication Provider
> Spring Configuration
- Total
- Today
- Yesterday