【问题标题】:Where to put the user information in the session将用户信息放在会话中的什么位置
【发布时间】:2025-12-24 05:00:17
【问题描述】:

我有 spring boot 代码来验证数据库的使用。它生成 x-auth 令牌或会话。外部 redis 服务器正在管理会话,我将如何将用户信息放入会话中,以便其他用户无法修改他们将被交叉检查的任何其他用户的数据会话属于谁。

这里是sn-p的代码:

public class SecurityConfig extends WebSecurityConfigurerAdapter {  

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    protected void configure(AuthenticationManagerBuilder builder) throws Exception {
          builder.jdbcAuthentication().dataSource(jdbcTemplate.getDataSource())
        .usersByUsernameQuery(
            "select username,password, enabled from users where username=?")
        .authoritiesByUsernameQuery(
            "select username, role from user_roles where username=?");          

    }

我还想将登录限制为仅一个 Web 服务,它将生成 x-auth 令牌,其他 Web 服务将被禁用以生成令牌。

【问题讨论】:

    标签: spring-security redis spring-boot


    【解决方案1】:

    由于您使用的是 Redis。假设用户有一个 id (user.id = toto12) 您应该在服务器端而不是客户端执行用户检查。

    检查服务器端的过程: 1. 像你一样检查 auth jdbcTemplate,

    1. 然后,让用户登录并与尝试登录 REDIS 的用户进行比较。如果为空,则在 Redis 中创建一条新记录 [例如 auth:users:toto12 = sessionid]。

    2. 如果所有这些测试都有效,则返回令牌,假设用户有权登录。

    【讨论】:

      【解决方案2】:

      你检查过Spring Cloud Security吗? User Account and Authentication Server 也可能有用。它可以处理您描述的情况:

      将登录限制为仅一个 Web 服务,这将生成 x-auth 令牌,其他 Web 服务将被禁用以生成令牌。

      对于会话用例,请查看 Spring Session 项目。它也支持 Redis。

      我有 spring boot 代码来验证数据库的使用。它 生成 x-auth 令牌或会话。外部 redis 服务器是 管理会话,我将如何将用户信息放入 session ,使其他用户不能修改任何其他用户的数据 他们将被交叉检查会话属于谁。

      【讨论】:

      【解决方案3】:

      我浏览了这个教程,http://docs.spring.io/spring-session/docs/current/reference/html5/guides/findbyusername.html,这个链接有用户识别教程。

      【讨论】:

        最近更新 更多