在JPA中使用Spring Security [英] Use Spring Security with JPA

查看:81
本文介绍了在JPA中使用Spring Security的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的新手。



我们正在使用弹簧安全功能。
数据库连接:JPA的eclipselink实现。
数据库:MySql



使用spring security时,身份验证提供程序的配置如下, -

 <认证提供商GT; 

< jdbc-user-service id =userDetailsS​​ervicedata-source-ref =Datasource/>

< / authentication-provider>

但是在JPA中我们没有定义数据源,我们使用持久性单元和提供者 jpa.PersistenceProvider。



那么我们如何配置身份验证提供程序以便使用JPA进行数据库连接?



data-source-ref字段究竟应包含什么才能使用数据库进行身份验证?



提前谢谢。

解决方案

基本上你可能需要实现 UserDetailsS​​ervice 你自己



<例如,您将拥有一个用户实体,以及您的 UserDetailsS​​ervice 实施将看用户并将其转换为 UserDetails 对象(或您的实体必须实现 UserDetails )。



示例实现:

 公共类MyUserDetailsS​​ervice实现UserDetailsS​​ervice {

private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager newEm){
this.entityManager = newEm;
}

public UserDetails loadUserByUsername(String username){

//假设您有一个实现UserDetails的User类
返回entityManager.createQuery( from user where username =:username,User.class)
.setParameter(username,username)
.getSingleResult();

}
}

然后将其添加到用户 spring-security.xml

 < authentication-manager> 
< authentication-provider user-service-ref =MyUserDetailsS​​ervice/>
< / authentication-manager>


I am new to Spring.

We are using spring security feature. Database connectivity: eclipselink implementation of JPA. Database: MySql

While using spring security, Configuration of authentication provider is as follows,-

<authentication-provider>

    <jdbc-user-service id="userDetailsService" data-source-ref="Datasource" />

    </authentication-provider>

But in JPA we do not define datasource, we use Persistence unit with provider jpa.PersistenceProvider.

So how do we configure authentication provider so as to use JPA for database connectivity?

What exactly should data-source-ref field contain to use database for authentication?

Thank you in advance.

解决方案

Basically you probably need to implement UserDetailsService yourself.

So you would for example have a User entity, and your UserDetailsService implementation would look up the user and convert it to a UserDetails object (or your entity would have to implement UserDetails).

Sample implementation:

public class MyUserDetailsService implements UserDetailsService{

    private EntityManager entityManager;
    @PersistenceContext
    public void setEntityManager(EntityManager newEm){
        this.entityManager = newEm;
    }

    public UserDetails loadUserByUsername(String username){

        // assuming that you have a User class that implements UserDetails
        return entityManager.createQuery("from User where username = :username", User.class)
                            .setParameter("username", username)
                            .getSingleResult();

    }
}

And you add this to user spring-security.xml

<authentication-manager>
   <authentication-provider user-service-ref="MyUserDetailsService" />
</authentication-manager>

这篇关于在JPA中使用Spring Security的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆