小编ika*_*ane的帖子

Spring-boot - 将 hibernate 映射文件添加到实体管理器

我正在将遗留应用程序迁移到 Spring-boot,并且必须集成一个 hibernate 命名查询映射文件(之前在 persistence.xml 文件中配置)。

我想出了一个解决方案

...
@Autowired
private DataSource dataSource;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);

    //...

    sessionFactoryBean.setMappingResources("META-INF/named-queries.hbm.xml");

    return sessionFactoryBean;
}   
Run Code Online (Sandbox Code Playgroud)

但我的应用程序中将不再有一个entityManager bean 和一个sessionFactory bean!

您认为这是一个好的解决方案吗?有没有办法以某种方式将休眠映射文件(named-query.hbm.xml)添加到entityManager而不使用sessionFactory bean?

预先感谢您的建议

** 编辑 ** JB Nizet 的建议,还提出了另一个解决方案

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactory.setDataSource(dataSource);

    // ...

    entityManagerFactory.setMappingResources("META-INF/named-queries.hbm.xml");

    return entityManagerFactory;
}
Run Code Online (Sandbox Code Playgroud)

在我的 DAO/服务中,我仍然可以通过以下方式获得休眠会话:

private Session getSession() {
        //return this.sessionFactory.getCurrentSession();
        return this.entityManager.unwrap(Session.class);
    }
Run Code Online (Sandbox Code Playgroud)

但是如果现在有人知道我们是否可以使用带有属性的 spring-boot 自动配置来做同样的事情,那么我们将受到欢迎!

java hibernate jpa spring-boot

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

Spring Security OAuth2 SSO 与自定义提供程序 + 注销

我正在尝试使用 Spring-boot 和 Dave Syer 示例通过 Spring Security Oauth2 实现 sso

我想使用我的自定义服务器提供商,它运行良好。

对于客户端,我希望用户在尝试访问客户端站点(例如 localhost:8080/)时进行身份验证(因此重定向到 OAuth2 url)并在身份验证后重定向回 index.html 文件。我还想在用户访问 index.html 文件中的链接时实现注销。

我想出了以下客户端 sso 客户端:

包 org.ikane;

导入 java.io.IOException;
导入 java.security.Principal;
导入 java.util.Arrays;

导入 javax.servlet.Filter;
导入 javax.servlet.FilterChain;
导入 javax.servlet.ServletException;
导入 javax.servlet.http.Cookie;
导入 javax.servlet.http.HttpServletRequest;
导入 javax.servlet.http.HttpServletResponse;

导入 org.apache.commons.lang3.StringUtils;
导入 org.slf4j.Logger;
导入 org.slf4j.LoggerFactory;
导入 org.springframework.boot.CommandLineRunner;
导入 org.springframework.boot.SpringApplication;
导入 org.springframework.boot.autoconfigure.SpringBootApplication;
导入 org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
导入 org.springframework.context.ConfigurableApplicationContext;
导入 org.springframework.core.env.ConfigurableEnvironment;
导入 org.springframework.security.config.annotation.web.builders.HttpSecurity;
导入 org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
导入 org.springframework.security.core.Authentication;
导入 org.springframework.security.core.context.SecurityContext;
导入 org.springframework.security.core.context.SecurityContextHolder;
导入 org.springframework.security.web.csrf.CsrfFilter;
导入 org.springframework.security.web.csrf.CsrfToken;
导入 org.springframework.security.web.csrf.CsrfTokenRepository;
导入 …

spring-security single-sign-on spring-boot spring-security-oauth2

5
推荐指数
1
解决办法
3262
查看次数