小编Cof*_*sta的帖子

Hibernate Criteria使用IN子句和子选择查询多个列

我有一个与这个问题非常相似的问题.

我从table1中选择了table2中field3和field4的所有匹配唯一组合的所有数据.

这是我剥离的SQL:

select *
from table1 as t1
where (t1.field1, t1.field2) in (select distinct field3, field4
                                 from table2 as t2
                                 where t2.id=12345);
Run Code Online (Sandbox Code Playgroud)

我需要将我的SQL翻译成Hibernate Criteria.我让我的实体对象正确映射到表并将响应转换为正确的结果实体,但我无法正确翻译我的where子句.

是)我有的

Criteria criteria = getSession().createCriteria(Table1.class);

DetachedCriteria subquery = DetachedCriteria.forClass(Table2.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("field3"), "field3");
projectionList.add(Projections.property("field4"), "field4");
subquery.setProjection(Projections.distinct(projectionList));
subquery.add(Restrictions.eq("id", 12345));
Run Code Online (Sandbox Code Playgroud)

我希望我的where子句类似于:

criteria.add(Subqueries.in("field1, field2", subquery));
Run Code Online (Sandbox Code Playgroud)

但是Hibernate不允许这样做.

我已经尝试推出where子句以获得两个子查询,并根据结果检查field1和field2,但看起来子查询总是必须返回多个列.我使用group by做了这个,但是Hibernate会自动将组中的列添加到投影列表中,我找不到删除它们的方法.

以下是使用group by的相同查询:

select *
from table1 as t1
where t1.field1 in (select field3
                    from table2 as t2
                    where t2.id=12345
                    group by field3, field4)
  and …
Run Code Online (Sandbox Code Playgroud)

java sql hibernate hibernate-criteria

3
推荐指数
2
解决办法
2万
查看次数

Spring OAuth2 Security - 客户端凭据 - 自定义 AuthenticationProvider

我正在我们的服务架构中编写一个身份验证服务,它基本上是 Spring OAuth2 授权服务器的实现。由此,我需要根据许多不同的来源对提供的凭据进行身份验证,以支持遗留环境。

我主要集中于使用客户端凭据流程,其中用户(其他服务)将使用自己的凭据来获取令牌,并且暂时不需要 OAuth2 的授权或刷新。

我已经成功启动了一个受 Spring Security ( @EnableWebSecurity) 保护的 Spring Boot 应用程序。我还成功设置了授权服务器 ( @EnableAuthorizationServer),它提供了必要的端点 ( /oauth/token) 来提供令牌。我已经能够使用内存和自定义 ClientDetailsS​​ervice 配置授权服务器以成功获取令牌。这一切只是为了向自己证明我可以做一些事情。

我的问题是我需要根据自定义源验证凭据。我无法直接访问密码,当然也不知道它们是如何编码的。

在深入研究 Spring 代码后,我可以看到,通过DaoAuthenticationProvider,它通过调用 来完成身份验证PasswordEncoder.matches()。不幸的是,我没有从 中获得密码ClientDetailsService,也不知道密码是如何编码的,所以自定义PasswordEncoder对我没有多大好处(而且我需要以多种方式与只有一种方式匹配PasswordEncoder)。所以,我只能定义我自己的AuthenticationProvider(或AuthenticationManager)。

我能够实现我自己的AuthenticationProvider并将其提供给 Spring Security 配置。这工作得很好,我能够将身份验证推迟到我自己的提供程序,该提供程序可以执行我认为合适的任何操作(例如委托给另一个服务进行身份验证),但这仅适用于非 OAuth2 端点。

现在一切都开始崩溃了。无论出于何种原因,我无法让/oauth/token端点使用我提供的定义的AuthenticationManager或。AuthenticationProvider它始终默认为AuthenticationMangerwithAnonymousAuthenticationProviderDaoAuthenticationProvider

网络安全

这里没什么有趣的。我正在全局公开身份验证管理器,试图将其注册到 OAuth2 配置中。我留下了一些带注释的代码来展示我尝试过的其他一些事情,但它们几乎都完成了相同的事情:它适用于除 OAuth2 端点之外的所有内容。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter …
Run Code Online (Sandbox Code Playgroud)

java authentication spring spring-security spring-security-oauth2

3
推荐指数
1
解决办法
2548
查看次数