feat: /auth配下をMastodonと統一

This commit is contained in:
usbharu 2024-09-05 12:33:53 +09:00
parent 7b77b097ca
commit aa34fb0370
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
12 changed files with 111 additions and 16 deletions

View File

@ -97,6 +97,12 @@ class SecurityConfig {
} }
formLogin { formLogin {
loginPage = "/auth/sign_in" loginPage = "/auth/sign_in"
loginProcessingUrl = "/login"
defaultSuccessUrl("/home", false)
}
logout {
logoutUrl = "/auth/sign_out"
logoutSuccessUrl = "/auth/sign_in"
} }
} }
return http.build() return http.build()

View File

@ -16,20 +16,29 @@
package dev.usbharu.hideout.core.config package dev.usbharu.hideout.core.config
import dev.usbharu.hideout.core.infrastructure.springframework.SPAInterceptor
import dev.usbharu.hideout.generate.JsonOrFormModelMethodProcessor import dev.usbharu.hideout.generate.JsonOrFormModelMethodProcessor
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.http.converter.HttpMessageConverter import org.springframework.http.converter.HttpMessageConverter
import org.springframework.web.method.support.HandlerMethodArgumentResolver import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor
import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor
@Configuration @Configuration
class MvcConfigurer(private val jsonOrFormModelMethodProcessor: JsonOrFormModelMethodProcessor) : WebMvcConfigurer { class MvcConfigurer(
private val jsonOrFormModelMethodProcessor: JsonOrFormModelMethodProcessor,
private val spaInterceptor: SPAInterceptor
) : WebMvcConfigurer {
override fun addArgumentResolvers(resolvers: MutableList<HandlerMethodArgumentResolver>) { override fun addArgumentResolvers(resolvers: MutableList<HandlerMethodArgumentResolver>) {
resolvers.add(jsonOrFormModelMethodProcessor) resolvers.add(jsonOrFormModelMethodProcessor)
} }
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(spaInterceptor)
}
} }
@Configuration @Configuration

View File

@ -1,13 +0,0 @@
package dev.usbharu.hideout.core.config
import dev.usbharu.hideout.core.infrastructure.springframework.SPAInterceptor
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@Configuration
class WebMvcConfig(private val spaInterceptor: SPAInterceptor) : WebMvcConfigurer {
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(spaInterceptor)
}
}

View File

@ -0,0 +1,24 @@
package dev.usbharu.hideout.core.interfaces.web
import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.infrastructure.springframework.SpringSecurityFormLoginPrincipalContextHolder
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
@Controller
class IndexController(
private val applicationConfig: ApplicationConfig,
private val springSecurityFormLoginPrincipalContextHolder: SpringSecurityFormLoginPrincipalContextHolder
) {
@GetMapping("/")
suspend fun index(model: Model): String {
if (springSecurityFormLoginPrincipalContextHolder.getPrincipal().userDetailId != null) {
return "redirect:/home"
}
model.addAttribute("applicationConfig", applicationConfig)
return "top"
}
}

View File

@ -19,6 +19,7 @@ package dev.usbharu.hideout.core.interfaces.web.auth
import dev.usbharu.hideout.core.application.actor.RegisterLocalActor import dev.usbharu.hideout.core.application.actor.RegisterLocalActor
import dev.usbharu.hideout.core.application.actor.RegisterLocalActorApplicationService import dev.usbharu.hideout.core.application.actor.RegisterLocalActorApplicationService
import dev.usbharu.hideout.core.application.instance.GetLocalInstanceApplicationService import dev.usbharu.hideout.core.application.instance.GetLocalInstanceApplicationService
import dev.usbharu.hideout.core.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous import dev.usbharu.hideout.core.domain.model.support.principal.Anonymous
import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletRequest
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
@ -30,6 +31,7 @@ import org.springframework.web.bind.annotation.PostMapping
@Controller @Controller
class AuthController( class AuthController(
private val applicationConfig: ApplicationConfig,
private val registerLocalActorApplicationService: RegisterLocalActorApplicationService, private val registerLocalActorApplicationService: RegisterLocalActorApplicationService,
private val getLocalInstanceApplicationService: GetLocalInstanceApplicationService, private val getLocalInstanceApplicationService: GetLocalInstanceApplicationService,
) { ) {
@ -50,4 +52,15 @@ class AuthController(
request.login(signUpForm.username, signUpForm.password) request.login(signUpForm.username, signUpForm.password)
return "redirect:$uri" return "redirect:$uri"
} }
@GetMapping("/auth/sign_in")
suspend fun signIn(model: Model): String {
model.addAttribute("applicationConfig", applicationConfig)
return "sign_in"
}
@GetMapping("/auth/sign_out")
fun signOut(): String {
return "sign_out"
}
} }

View File

@ -1,3 +1,4 @@
auth-signIn.title=\u30ED\u30B0\u30A4\u30F3 - {0}
auth-signUp.password=\u30D1\u30B9\u30EF\u30FC\u30C9 auth-signUp.password=\u30D1\u30B9\u30EF\u30FC\u30C9
auth-signUp.register=\u767B\u9332\u3059\u308B auth-signUp.register=\u767B\u9332\u3059\u308B
auth-signUp.username=\u30E6\u30FC\u30B6\u30FC\u540D auth-signUp.username=\u30E6\u30FC\u30B6\u30FC\u540D

View File

@ -1,3 +1,4 @@
auth-signIn.title=Sign in - {0}
auth-signUp.password=Password auth-signUp.password=Password
auth-signUp.register=Register Account auth-signUp.register=Register Account
auth-signUp.username=Username auth-signUp.username=Username

View File

@ -1,3 +1,4 @@
auth-signIn.title=\u30ED\u30B0\u30A4\u30F3 - {0}
auth-signUp.password=\u30D1\u30B9\u30EF\u30FC\u30C9 auth-signUp.password=\u30D1\u30B9\u30EF\u30FC\u30C9
auth-signUp.register=\u767B\u9332\u3059\u308B auth-signUp.register=\u767B\u9332\u3059\u308B
auth-signUp.username=\u30E6\u30FC\u30B6\u30FC\u540D auth-signUp.username=\u30E6\u30FC\u30B6\u30FC\u540D

View File

@ -2,14 +2,16 @@
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <html lang="en" xmlns:th="http://www.thymeleaf.org">
<head prefix="og: http://ogp.me/ns#"> <head prefix="og: http://ogp.me/ns#">
<meta charset="UTF-8"> <meta charset="UTF-8">
<title th:title="${title}">Title</title> <title th:title="${title}">Index</title>
<meta property="og:url" th:content="${url}"> <meta property="og:url" th:content="${url}">
<meta property="og:title" th:content="${title}"> <meta property="og:title" th:content="${title}">
<meta property="og:description" th:content="${description}"> <meta property="og:description" th:content="${description}">
<th:block th:if="${image != null}"> <th:block th:if="${image != null}">
<meta property="og:image" th:content="${image}"> <meta property="og:image" th:content="${image}">
</th:block> </th:block>
<meta http-equiv="refresh" th:content="${'1; url='+nsUrl}"> <noscript>
<meta http-equiv="refresh" th:content="${'1; url='+nsUrl}">
</noscript>
</head> </head>
<body> <body>
<noscript> <noscript>

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:text="#{auth-signIn.title(${applicationConfig.url})}">Sign In - Hideout</title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form method="post" th:action="@{/login}">
<div>
<input name="username" placeholder="Username" type="text"/>
</div>
<div>
<input name="password" placeholder="Password" type="password"/>
</div>
<input type="submit" value="Log in"/>
</form>
</body>
</html>

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" th:action="@{/auth/sign_out}">
<input name="logout" type="submit" value="Sign out">
</form>
</body>
</html>

View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title th:title="${title}">Index</title>
<meta property="og:url" th:content="${url}">
<meta property="og:title" th:content="${title}">
<meta property="og:description" th:content="${description}">
<meta property="og:image" th:content="${image}">
</head>
<body>
<h1 th:text="${applicationConfig.url + ' - Hideout'}">Hideout</h1>
<div><a href="/auth/sign_up"></a></div>
</body>
</html>