Note that there are some explanatory texts on larger screens.

plurals
  1. POEntityManager null when implementing UserDetailsService with spring security
    primarykey
    data
    text
    <p>i am trying to include spring security in a web application. For this i wrote my own UserDetailsService implementation which looks like this:</p> <pre><code>import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service("userRepositoryImpl") public class UserRepositoryImpl implements UserDetailsService { @PersistenceContext private EntityManager entityManager; @Override @Transactional(readOnly = true) public User loadUserByUsername(String username) throws UsernameNotFoundException { User user = entityManager.find(User.class, username); if (user == null) throw new UsernameNotFoundException("Username not found: " + username); return user; } } </code></pre> <p>My problem is that the entityManager always is null when it calls the loadUserByUserName method. I tried some things suggested here in some similar answers, but nothing helped.</p> <p>This is my current security-app-context.xml:</p> <pre><code>&lt;http use-expressions="true" auto-config="true"&gt; &lt;intercept-url pattern="/task/" access="permitAll" /&gt; &lt;intercept-url pattern="/task/**" access="isAuthenticated()" /&gt; &lt;!-- &lt;intercept-url pattern="/**" access="denyAll" /&gt; --&gt; &lt;form-login /&gt; &lt;/http&gt; &lt;beans:bean id="userRepositoryImpl" class="de.sveri.jeiwomisa.model.UserRepositoryImpl" autowire="byType"&gt; &lt;/beans:bean&gt; &lt;beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"&gt; &lt;/beans:bean&gt; &lt;authentication-manager&gt; &lt;authentication-provider user-service-ref="userRepositoryImpl"&gt; &lt;password-encoder ref="passwordEncoder" /&gt; &lt;/authentication-provider&gt; &lt;/authentication-manager&gt; </code></pre> <p></p> <p>Any Ideas what might be wrong there?</p> <p>Regards, Sven</p> <p><strong>Edit:</strong> Trying a bit more i found out that i cannot use the entityManager in my generic DaoImpl during the login process. <strong>But</strong> i can use it sending and receiving data in another process. This is the Dao:</p> <pre><code>@Transactional public abstract class DaoImpl&lt;T&gt; implements Dao&lt;T&gt; { private Class&lt;T&gt; type; @PersistenceContext protected EntityManager em; public DaoImpl() { Type t = getClass().getGenericSuperclass(); ParameterizedType pt = (ParameterizedType) t; type = (Class) pt.getActualTypeArguments()[0]; } ... } </code></pre> <p>I wonder why it is like this. Obviously i dont understand much of how spring works, so it would be great if someone could shed a light.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload