gpt4 book ai didi

java - 单击按钮时 Spring 安全性检查用户是否已登录

转载 作者:行者123 更新时间:2023-12-01 11:29:07 26 4
gpt4 key购买 nike

我正在使用 Spring MVC 和 Spring security 为在线航类预订系统创建一个简单的 Web 应用程序。我创建了下表来显示航类详细信息。

<table class="table table-bordered table-hover table-striped ">
<thead>
<tr>
<th>Flight No</th>
<th>Flight destination</th>
<th>Flight origin</th>
<th>Flight date</th>
<th>Flight time</th>
<th>Book now</th>
</tr>
</thead>

<tbody>
<form:form commandName="reserv" cssClass="form-horizontal">
<c:forEach items="${flightInfos}" var="flightInfo">
<tr>
<td>${flightInfo.flightNo}</td>
<td>${flightInfo.destination}</td>
<td>${flightInfo.origin}</td>
<td>${flightInfo.flightDate}</td>
<td>${flightInfo.flightTime}</td>
<td><input type="submit" value="Book now" class="btn btn-primary"></td>
</tr>
</c:forEach>
</form:form>
</tbody>
</table>

在此表中,我想在用户单击“立即预订”按钮时检查用户是否已登录。这意味着每个人都可以看到此页面,但他们需要登录系统才能预订航类。这是我的 security.xml 文件。

<http use-expressions="true">
<intercept-url pattern="/users**" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/users/**" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/account**" access="hasRole('ROLE_USER')" />
<intercept-url pattern="/reservation**" access="hasRole('ROLE_USER')" />
<form-login login-page="/login.html"/>
<csrf disabled="true"/>
<logout logout-url="/logout" />
</http>

当用户点击“立即预订”按钮时,如何检查用户是否已登录?有什么简单的方法可以做到这一点吗?

最佳答案

“当用户单击“立即预订”按钮时,如何检查用户是否已登录?有没有简单的方法可以做到这一点?”

当按下提交按钮时;该表单将发布到您提供的 URL(作为 POST)示例:

<form action="/myURL" method="POST">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

只需将 URL 更改为安全 xml 中限制的内容即可。像这样:

<intercept-url pattern="/postformURL" access="hasRole('ROLE_USER')" />

然后当它获取 formURL 的 Controller 映射时;只有通过了 Spring Security,它才会到达那里。

关于java - 单击按钮时 Spring 安全性检查用户是否已登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30564930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com