Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 通过向用户身份验证对象存储附加信息来最小化数据库开销(Spring Security)_Database_Spring_Spring Security - Fatal编程技术网

Database 通过向用户身份验证对象存储附加信息来最小化数据库开销(Spring Security)

Database 通过向用户身份验证对象存储附加信息来最小化数据库开销(Spring Security),database,spring,spring-security,Database,Spring,Spring Security,长话短说:我想尽量减少对已登录用户的user\u id之类的内容的数据库查找,但我不知道这样做的好方法是什么 我使用Spring Security来检查登录用户是否经过身份验证。但是,在身份验证之后,随着一些实际请求的传入,我希望尽可能减少数据库调用的数量 因此我的问题是:从SecurityContextHolder中,我可以使用Authentication对象的getPrincipal()获得User对象: @PreAuthorize("isAuthenticated()") public L

长话短说:我想尽量减少对已登录用户的
user\u id
之类的内容的数据库查找,但我不知道这样做的好方法是什么

我使用Spring Security来检查登录用户是否经过身份验证。但是,在身份验证之后,随着一些实际请求的传入,我希望尽可能减少数据库调用的数量

因此我的问题是:从
SecurityContextHolder
中,我可以使用
Authentication
对象的
getPrincipal()
获得
User
对象:

@PreAuthorize("isAuthenticated()")
public List<StoreDTO> getAvailableStores() {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();       
    User user = (User)auth.getPrincipal();      
    String username = user.getUsername();

    List<Store> storeList = this.storeAdminRepository.getStores(username);

    return Convert.toStoreDtoList(restaurantList);
}
@PreAuthorize(“isAuthenticated()”)
公共列表getAvailableStores(){
Authentication auth=SecurityContextHolder.getContext().getAuthentication();
User=(User)auth.getPrincipal();
字符串username=user.getUsername();
List storeList=this.storeAdminRepository.getStores(用户名);
返回Convert.toStoreDtoList(restaurantList);
}
使用自定义对象(例如,还存储数据库中的用户id)而不是将简单的
User
对象设置为主体,这会是“脏的”吗

我现在这样做需要我首先根据他的名字查找用户id,然后获取stores
where user.id=store.user_id
或类似的内容

假设有很多请求,这是一种最小化开销的方法吗?或者,这也可能是真的,我的担心是没有根据的,因为开销远没有我在这里假设的那么大