【问题标题】:Spring-Security : Optimizing get currently authenticated user…Spring-Security:优化获取当前经过身份验证的用户...
【发布时间】:2015-10-28 04:28:47
【问题描述】:

我正在开发一个使用 Spring-Security 进行身份验证的 Spring-MVC 应用程序。由于过度使用获取当前经过身份验证的用户机制,Profiler 将其显示为“分配热点”,单个用户消耗了近 9.5kb 的内存。有没有办法优化这个基础设施。

PersonServiceImpl :

 @Override
    public Person getCurrentlyAuthenticatedUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        } else {
            return personDAO.findPersonByUsername(authentication.getName());
        }
    }

如果我总能以某种方式将用户推送到某个缓存中,从第一次检索后检索它的位置,至少应该会提高性能,但我不知道该怎么做。欢迎任何建议。谢谢。

【问题讨论】:

  • 也许Spring Cache 可以在这里为您提供帮助。 Spring Cache 首先检查某个方法和键(可以自定义)的值是否可用,如果可用,将返回缓存命中而不输入方法。如果未命中,它将执行该方法并将返回值放入缓存中。挑战在于提供用户会话独有的密钥。
  • @RomanVottner 看起来很有希望。我现在刚刚放了cacheable注解,但是参数cacheNames不可用。我相信它在较新的版本中已被删除。注释应该够吗?
  • 您可以在类级别通过@CacheConfig(cacheNames="yourName", keyGenerator="customKeyGenerator", ...) 声明cacheNames,只需用@Cacheable注释每个应该使用缓存的方法

标签: java spring spring-mvc spring-security


【解决方案1】:

您可以使用一些变体。但您也可以尝试仅使用 Spring: 1. 扩展 org.springframework.security.core.userdetails.User 并添加到你的 UserEntity(Person)中

public class User extends org.springframework.security.core.userdetails.User {
    private Person sysUser; 

  public User(Person sysUser) {
        super(sysUser.getLogin(), sysUser.getPassword(), new ArrayList<>());
        this.sysUser = sysUser;
    }

public Person getYourUser(){
return sysUser;
}

}
  1. 使用您的用户实体和扩展的 org.springframework.security.core.userdetails.User 实现 UserDetailsS​​ervice

    @Service(value = "userService")
    public class UserService implements UserDetailsService {
    @Autowired
    SecurityDAO securityDAO;
    
    @Override
    public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
        User user = null;
        try {
            Person su = securityDAO.getUserOnlyByLogin(login);
            if (su != null)
                user = new User(Person);
    
        } catch (Exception e) {
    
            e.printStackTrace();
        }
    
    
        return user;
    }
    

    }

  2. 配置您的 Spring(xml 或 Annotation):

 <beans:bean id="userService" class="your.pack.UserService"/>
    <authentication-manager alias="authenticationManager">
        <authentication-provider user-service-ref="userService"/>
    </authentication-manager>
  1. 用你的方法

    @Override

       public Person getCurrentlyAuthenticatedUser() {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    
            if (authentication == null) {
                return null;
            } else {  User user = (User) authentication.getPrincipal();
                return user.getYourUser();
            }
        }
    

【讨论】:

  • 我不明白这是什么意思,我的登录代码已经在工作了。
  • 此代码允许使用 Spring 上下文来保存有关您的用户的信息。但是如果你需要每次对数据库做一个请求我可以建议EHCache
猜你喜欢
  • 2014-12-20
  • 2014-01-12
  • 2013-04-23
  • 2023-03-27
  • 2014-10-03
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
相关资源
最近更新 更多