gpt4 book ai didi

java - 使用登录端点/Web服务成功登录后,如何发送额外的数据/信息?

转载 作者:行者123 更新时间:2023-12-02 02:32:39 26 4
gpt4 key购买 nike

成功登录后,我试图保存所有信息,或者要在调用端点或Web服务后保存客户端信息。

我正在为项目使用Spring Boot安全性。
从客户端调用/login端点后,
客户想要向我发送IP地址等数据,
市,
地区,
国家,
位置,
邮政编码,
用户名,
密码,
用户角色以及其他一些信息。
我正在使用Spring Security,因此我通过将用户名和用户角色组合为一个字符串和密码作为单独的字符串来发送用户名和用户角色。

我通过以下方式传递这三个参数。

public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res) throws AuthenticationException, IOException, ServletException {
AccountCredentials accountCredntials = new ObjectMapper().readValue(req.getInputStream(), AccountCredentials.class);

String userName=accountCredntials.getUsername() + "_" + accountCredntials.getRole();// + "_" +accountCredntials.getAppName();

return this.getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(
userName, accountCredntials.getPassword(), Collections.emptyList()));
}


在上面的代码中,
我创建了一个字符串名称作为用户名,该名称是两个字符串之间的下划线分隔。

如何使用此端点或上述方式发送大量数据?

还有其他发送上述方法的方法
登录端点中的数据,
弹簧启动安全性?

我通过将这些参数串联到一个字符串中来发送此数据。
我还有其他发送方式吗?

请建议如何使用Spring Boot安全性从客户端到服务器在一个端点中发送以上数据。

最佳答案

您可以通过实现UserDetails接口来实现。根据spring文档,此接口是针对您所遇到的情况而设计的:java doc


提供核心用户信息。实现不直接使用
出于安全目的,由Spring Security提供。他们只是存储用户
信息,该信息随后封装到身份验证对象中。
这允许非安全相关的用户信息(例如电子邮件)
地址,电话号码等)存储在方便的地方
位置。


在这里经过简单的实现:

public class UserDetailsImpl implements UserDetails {
private AccountCredentials account;

public UserDetailsImpl(final AccountCredentials account) {
this.account = account;
}

public UserDetailsImpl() {
}

// userDetails implementation
...
//getter and setter
...
}

关于java - 使用登录端点/Web服务成功登录后,如何发送额外的数据/信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57221674/

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