gpt4 book ai didi

java - 如何使用 Micronaut View 发送回参数并设置本地存储?

转载 作者:行者123 更新时间:2023-12-02 09:55:47 24 4
gpt4 key购买 nike

我正在尝试使用Micronaut Views annotation发送回一些参数并设置本地存储,然后重定向。我在 autoredirect.html 模板中使用了 script 标记,该标记应该设置本地存储,然后使用 window.location.replace 进行重定向。请帮我解决以下两个问题。

  1. 请求命中了 post 和 get 方法,但相应的 View 没有发回,而是在计算完所有内容后得到 404 Not Found

    GET 请求 -> http://localhost:8081/someapp/api/sso

  2. 这是设置本地存储并重定向到另一个相对路径的正确方法吗?

    带有计算角色和 JWT 所需参数的 POST 请求 -> http://localhost:8081/someapp/api/sso/saml

在 build.gradle 中添加了以下附加条目

compile "io.micronaut:micronaut-views"
runtime "org.thymeleaf:thymeleaf:3.0.11.RELEASE"

查看位置

enter image description here我的 Controller

@Controller('/someapp/api/sso')
@Slf4j
@CompileStatic
class SomeController {

@View("home")
@Get("/")
HttpResponse index() {
return HttpResponse.ok(CollectionUtils.mapOf("loggedIn", true, "username", "raka"))
}

@View("autoredirect")
@Post('/saml')
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
HttpResponse<String> samlLogin(@Nullable @Body LinkedHashMap payload) {
//... some operations, computing user roles & jwt
if (payload != null) {
String results = handler.call(payload)
return HttpResponse.ok(results).
header("JWT", jwt.toString())
} else {
return HttpResponse.badRequest()
}
}
}

home.html

<!DOCTYPE html>
<html lang="en" th:replace="~{layoutFile :: layout(~{::title}, ~{::section})}" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home</title>
</head>
<body>
<section>
<h1 th:if="${loggedIn}">username: <span th:text="${username}"></span></h1>
<h1 th:unless="${loggedIn}">You are not logged in</h1>
</section>
</body>
</html>

自动重定向.html

<!DOCTYPE html>
<html>
<head>
<title>Cassini</title>
</head>
<script type="text/javascript">
localStorage.setItem('username', '${username}');
localStorage.setItem('perm', '${perm}');
localStorage.setItem('userDetails', '${userDetails}')
window.location.replace('${redirectURL}');
</script>
<body>
</body>
</html>

更新

我尝试在 application.yml 中显式添加以下行,但仍然没有成功。提供 13:10:56.476 [pool-2-thread-3] DEBUG io.micronaut.views.ViewsFilter - view autoredirect not found 消息。

micronaut.views.enabled: true
micronaut.views.folder: views
micronaut.router.static-resources.*.enabled: true
micronaut.router.static-resources.*.paths: classpath:public

最佳答案

您应该只有一个端点来返回用户信息。然后客户端可以调用它并执行任何需要发生的操作。尝试将内容放入本地存储然后重定向到 HTML 文件对我来说似乎是一种黑客行为

关于java - 如何使用 Micronaut View 发送回参数并设置本地存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56022660/

24 4 0