问题描述
我在使用 Vaadin (v. 14.6.1) 和 Spring boot (v. 2.4.5) 时遇到了一个奇怪的问题。我已按照 https://vaadin.com/learn/tutorials/securing-your-app-with-spring-security/setting-up-spring-security 中的说明实施登录视图和安全设置。
当我加载应用程序并转到 localhost/8080 登录时,我输入了正确的凭据,但登录失败并显示消息
WARN 8184 --- [io-8080-exec-10] cvfscUidlRequestHandler:从 0:0:0:0:0:0:0:1 收到的安全密钥无效
当我尝试使用相同的凭据再次登录时,登录成功。如果我注销并再次尝试登录,它工作正常。
我可以通过重新启动服务器并尝试再次登录来重现此问题。我在开发模式和生产模式下测试了这个问题,并得到了相同的行为。
解决方法
我发现您的代码有两个问题:
- SecurityUtils
ServletHelper
类已弃用,我认为这会导致问题。我用 HandlerHelper
替换了它:
public class SecurityUtils {
/**
* Tests if the request is an internal framework request. The test consists of
* checking if the request parameter is present and if its value is consistent
* with any of the request types know.
*
* @param request
* {@link HttpServletRequest}
* @return true if is an internal framework request. False otherwise.
*/
static boolean isFrameworkInternalRequest(HttpServletRequest request) {
final String parameterValue = request.getParameter(ApplicationConstants.REQUEST_TYPE_PARAMETER);
return parameterValue != null && Stream.of(HandlerHelper.RequestType.values())
.anyMatch(r -> r.getIdentifier().equals(parameterValue));
}
- 缺少 Vaadin 插件(如果您使用 Maven 运行它,则必须运行prepare-frontend,只是附带说明)
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-maven-plugin</artifactId>
<version>${vaadin.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-frontend</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>