Spring Security 上的 json 登录 [英] json login on spring security

查看:68
本文介绍了Spring Security 上的 json 登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基于 spring 的 REST 后端,我正在使用 spring 安全性来保护请求.但是我正在寻找通过在 json 中发送参数而不是 spring security 的默认登录页面发送的默认参数来登录的问题.

I'm building a REST back end based on spring and i'm using spring security to secure the requests. But i'm lookin for an issue to login by sending parameters in json rather than defaults parameters sent by the default login page of spring security.

我正在使用 spring security 4.0.1 和 spring 4.1

I'm working with spring security 4.0.1 and spring 4.1

请问有什么问题吗?

推荐答案

如果您只使用用户名和密码,您可以简单地向堆栈添加一个新过滤器,类似于现有的 UsernamePasswordAuthenticationFilter,它只会对特定的 URL 做出反应(就像默认的 URL 只对 j_spring_security_check 做出反应一样),解析 JSON 并创建与默认过滤器创建的完全相同的 UsernamePasswordAuthenticationToken.这使身份验证提供程序与令牌未更改相同.

If you're using just username and password, you can simply add a new filter to the stack, akin to the existing UsernamePasswordAuthenticationFilter, that would react to a specific URL only (just like the default one reacts to j_spring_security_check only), parse the JSON and create the very same UsernamePasswordAuthenticationToken that the default filter creates. This leaves the auth provider the same as the token didn't change.

如果除了用户名和密码之外,您还需要更多字段,请创建一个新的令牌类型(或使用现有的,如果有意义的话)和一个可以处理该令牌类型的新身份验证提供程序.您也可以使用 setDetails() 将额外的字段填充到 UsernamePasswordAuthenticationToken 中,但这有点麻烦.

If you need more fields in addition to username and password, either create a new token type (or use existing one if it makes sense) and a new auth provider that can deal with that token type. You can also just cram extra fields into UsernamePasswordAuthenticationToken using setDetails(), but this is a bit hacky.

这篇关于Spring Security 上的 json 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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