Skip to content

Commit

Permalink
Merge pull request #92 from LikeKNU/Feature/Admin
Browse files Browse the repository at this point in the history
์–ด๋“œ๋ฏผ ํŽ˜์ด์ง€ ๊ฐœ๋ฐœ
  • Loading branch information
jcw1031 authored Jan 13, 2024
2 parents 485d525 + 11ab66f commit 9f8d9d0
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 130 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testCompileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/ac/knu/likeknu/config/SecurityConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;

@Configuration
@EnableWebSecurity
Expand All @@ -24,12 +27,18 @@ public class SecurityConfiguration {
private String password;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http.authorizeHttpRequests(registry -> registry.requestMatchers("/api/**").permitAll()
public SecurityFilterChain filterChain(HttpSecurity http, HandlerMappingIntrospector handlerMappingIntrospector)
throws Exception {
RequestMatcher requestMatcher = new MvcRequestMatcher(handlerMappingIntrospector, "/api/**");
return http.authorizeHttpRequests(registry -> registry.requestMatchers(requestMatcher).permitAll()
.anyRequest().authenticated())
.formLogin(security -> security.loginPage("/admin/login")
.defaultSuccessUrl("/admin/messages")
.permitAll()).csrf(AbstractHttpConfigurer::disable)
.failureUrl("/admin/login?error=1")
.permitAll())
.logout(security -> security.logoutUrl("/admin/logout")
.logoutSuccessUrl("/admin/login"))
.csrf(AbstractHttpConfigurer::disable)
.build();
}

Expand Down
23 changes: 22 additions & 1 deletion src/main/java/ac/knu/likeknu/controller/AdminController.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
package ac.knu.likeknu.controller;

import ac.knu.likeknu.domain.MainHeaderMessage;
import ac.knu.likeknu.repository.MainHeaderMessageRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Comparator;
import java.util.List;

@RequestMapping("/admin")
@Controller
public class AdminController {

private final MainHeaderMessageRepository messageRepository;

public AdminController(MainHeaderMessageRepository messageRepository) {
this.messageRepository = messageRepository;
}

@GetMapping("/login")
public String loginForm() {
public String loginForm(@RequestParam(value = "error", required = false, defaultValue = "0") int error, Model model) {
if (error == 1) {
model.addAttribute("error", "๋กœ๊ทธ์ธ ์‹คํŒจ");
}

return "login";
}

@GetMapping("/messages")
public String registerMessageForm(Model model) {
List<MainHeaderMessage> messages = messageRepository.findAll()
.stream()
.sorted(Comparator.comparing(MainHeaderMessage::getRegisteredAt).reversed())
.toList();
model.addAttribute("messages", messages);
return "registerMessage";
}
}
6 changes: 6 additions & 0 deletions src/main/java/ac/knu/likeknu/domain/MainHeaderMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Getter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Getter
@Entity
Expand All @@ -30,4 +31,9 @@ public MainHeaderMessage(String message) {
this();
this.message = message;
}

public String formattedRegisteredDateTime() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
return registeredAt.format(dateTimeFormatter);
}
}
9 changes: 8 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
spring.profiles.active=dev
spring.datasource.url=jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_SCHEMA}?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}

spring.jpa.hibernate.ddl-auto=validate

admin.username=${ADMIN_USERNAME}
admin.password=${ADMIN_PASSWORD}
80 changes: 80 additions & 0 deletions src/main/resources/static/color-modes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*!
* Color mode toggler for Bootstrap's docs (https://getbootstrap.com/)
* Copyright 2011-2023 The Bootstrap Authors
* Licensed under the Creative Commons Attribution 3.0 Unported License.
*/

(() => {
'use strict'

const getStoredTheme = () => localStorage.getItem('theme')
const setStoredTheme = theme => localStorage.setItem('theme', theme)

const getPreferredTheme = () => {
const storedTheme = getStoredTheme()
if (storedTheme) {
return storedTheme
}

return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}

const setTheme = theme => {
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-bs-theme', 'dark')
} else {
document.documentElement.setAttribute('data-bs-theme', theme)
}
}

setTheme(getPreferredTheme())

const showActiveTheme = (theme, focus = false) => {
const themeSwitcher = document.querySelector('#bd-theme')

if (!themeSwitcher) {
return
}

const themeSwitcherText = document.querySelector('#bd-theme-text')
const activeThemeIcon = document.querySelector('.theme-icon-active use')
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href')

document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
element.classList.remove('active')
element.setAttribute('aria-pressed', 'false')
})

btnToActive.classList.add('active')
btnToActive.setAttribute('aria-pressed', 'true')
activeThemeIcon.setAttribute('href', svgOfActiveBtn)
const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel)

if (focus) {
themeSwitcher.focus()
}
}

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
const storedTheme = getStoredTheme()
if (storedTheme !== 'light' && storedTheme !== 'dark') {
setTheme(getPreferredTheme())
}
})

window.addEventListener('DOMContentLoaded', () => {
showActiveTheme(getPreferredTheme())

document.querySelectorAll('[data-bs-theme-value]')
.forEach(toggle => {
toggle.addEventListener('click', () => {
const theme = toggle.getAttribute('data-bs-theme-value')
setStoredTheme(theme)
setTheme(theme)
showActiveTheme(theme, true)
})
})
})
})()
15 changes: 15 additions & 0 deletions src/main/resources/static/headers.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.form-control-dark {
border-color: var(--bs-gray);
}
.form-control-dark:focus {
border-color: #fff;
box-shadow: 0 0 0 .25rem rgba(255, 255, 255, .25);
}

.text-small {
font-size: 85%;
}

.dropdown-toggle:not(:focus) {
outline: 0;
}
80 changes: 3 additions & 77 deletions src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<!DOCTYPE html>
<html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>๋กœ๊ทธ์ธ</title>
<link href="https://getbootstrap.com/docs/5.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="/color-modes.js"></script>
<style>
html,
body {
Expand Down Expand Up @@ -35,6 +36,7 @@
</head>
<body class="d-flex align-items-center py-4 bg-body-tertiary">
<main class="form-signin w-100 m-auto">
<div class="alert alert-danger" role="alert" th:if="${error}">๊ด€๋ฆฌ์ž ์•„๋‹ˆ๋ฉด ๋‚˜๊ฐ€์„ธ์š”!</div>
<form action="/admin/login" method="POST">
<img class="mb-4"
src="https://avatars.githubusercontent.com/u/144087727?s=400&u=0234d20a40888b3d67e1576444a2511e7f351c91&v=4"
Expand All @@ -52,81 +54,5 @@ <h1 class="h3 mb-3 fw-normal">๊ณต์ฃผ๋Œ€์ฒ˜๋Ÿผ ๊ด€๋ฆฌ์ž ๋กœ๊ทธ์ธ</h1>
<p class="mt-5 mb-3 text-body-secondary">ยฉ ๊ณต์ฃผ๋Œ€์ฒ˜๋Ÿผ</p>
</form>
</main>
<script>
(() => {
'use strict'

const getStoredTheme = () => localStorage.getItem('theme')
const setStoredTheme = theme => localStorage.setItem('theme', theme)

const getPreferredTheme = () => {
const storedTheme = getStoredTheme()
if (storedTheme) {
return storedTheme
}

return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}

const setTheme = theme => {
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-bs-theme', 'dark')
} else {
document.documentElement.setAttribute('data-bs-theme', theme)
}
}

setTheme(getPreferredTheme())

const showActiveTheme = (theme, focus = false) => {
const themeSwitcher = document.querySelector('#bd-theme')

if (!themeSwitcher) {
return
}

const themeSwitcherText = document.querySelector('#bd-theme-text')
const activeThemeIcon = document.querySelector('.theme-icon-active use')
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href')

document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
element.classList.remove('active')
element.setAttribute('aria-pressed', 'false')
})

btnToActive.classList.add('active')
btnToActive.setAttribute('aria-pressed', 'true')
activeThemeIcon.setAttribute('href', svgOfActiveBtn)
const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel)

if (focus) {
themeSwitcher.focus()
}
}

window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
const storedTheme = getStoredTheme()
if (storedTheme !== 'light' && storedTheme !== 'dark') {
setTheme(getPreferredTheme())
}
})

window.addEventListener('DOMContentLoaded', () => {
showActiveTheme(getPreferredTheme())

document.querySelectorAll('[data-bs-theme-value]')
.forEach(toggle => {
toggle.addEventListener('click', () => {
const theme = toggle.getAttribute('data-bs-theme-value')
setStoredTheme(theme)
setTheme(theme)
showActiveTheme(theme, true)
})
})
})
})()
</script>
</body>
</html>
Loading

0 comments on commit 9f8d9d0

Please sign in to comment.