如何在 Spring 的 RESTful Web 应用程序中进行身份验证(登录/注销)? [英] How to authenticate (login / logout) in RESTful web application in Spring?

查看:61
本文介绍了如何在 Spring 的 RESTful Web 应用程序中进行身份验证(登录/注销)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过身份验证

http://myhost/login?user=...&password=...

然后退出

http://myhost/logout

我使用 Gradle、Spring Boot 和 Java 配置,所以没有 web.xml、没有上下文配置、没有 Web 表单等等.

I am using Gradle, Spring Boot and Java config, so no web.xml, no context configurations, no web forms and so on.

无法摆脱多页和多文件样本的谷歌噪音...

Can't escape from google noise on multipage and multifile samples...

推荐答案

一种方法是使用令牌.

登录服务将接受凭据,生成令牌(例如,UUID 类型 4,请参阅 https://en.wikipedia.org/wiki/Universally_unique_identifier),将令牌存储在表中并返回.

The login service would accept the credentials, generate a token (a UUID type 4 for example, see https://en.wikipedia.org/wiki/Universally_unique_identifier), store the token in a table and return it.

在每次调用中,客户端都必须在标头中或作为另一个参数发送令牌,因此过滤器或其他东西会检查它以允许访问.

In every call, the client would have to send the token in the header or as another parameter, so a filter or something would check it to allow access.

在注销时,令牌将被删除(您可能希望有一个在一定时间或类似时间后删除令牌的过程).

On logout, the token would be deleted (you may want to have a process that deletes the tokens after a certain amount of time or something like that).

这篇关于如何在 Spring 的 RESTful Web 应用程序中进行身份验证(登录/注销)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆